from typing import Union
from typing import List
from typing import Any
import math


def rewrite_inf_nan(
    data: Union[float, int, List[Any]]
) -> Union[str, int, float, List[Any]]:
    """Replaces NaN and Infinity with string representations"""
    if isinstance(data, float):
        if math.isnan(data):
            return 'NaN'
        if math.isinf(data):
            return ('+' if data > 0 else '-') + 'inf'
        return data
    elif isinstance(data, list):
        return [rewrite_inf_nan(item) for item in data]
    else:
        return data