from typing import Optional


def flatten_dict(
        d: dict,
        s: str = '',
        exc: Optional[list] = None
) -> dict:
    """Reformat a multi-layer dictionary into a flat one.

    :param d: Input dict.
    :param s: Prefix to be added to keys.
    :param exc: List of keys to exclude from the resulting dict.
    :return: Reformatted dictionary.
    """

    if exc is None:
        exc = []
    u = {}
    for k, v in d.items():
        if k in exc:
            u[k] = v
        else:
            if len(s):
                k = s + '_' + k
            if isinstance(v, dict):
                u |= flatten_dict(v, k)
            elif isinstance(v, list):
                u |= {k+'_'+str(i): vv for i, vv in enumerate(v)}
            else:
                u[k] = v
                print('')

    return u