def invert_dict(dic, sort=True, keymap={}, valmap={}):
    """Inverts a dictionary of the form
    key1 : [val1, val2]
    key2 : [val1]

    to a dictionary of the form

    val1 : [key1, key2]
    val2 : [key2]

    Parameters
    -----------
    dic : dict

    Returns
    -----------
    dict
    
    """

    dic_inv = {}
    for k, v_list in dic.items():
        k = keymap.get(k, k)
        for v in v_list:
            v = valmap.get(v, v)
            if v in dic_inv:
                dic_inv[v].append(k)
            else:
                dic_inv[v] = [k]

    if sort:
        for k in dic_inv.keys():
            dic_inv[k].sort()

    return dic_inv