from functools import reduce
def count_permutations(counter):
    values = list(counter.values())
return factorial(sum(values)) / reduce(lambda a, v: a * factorial(v), values, 1
    )

def permutation(l, index):
    l = sorted(l)
if not index:
    return l
counter = Counter(l)
total_count = count_permutations(counter)
acc = 0
for i, v in enumerate(l):
    if i > 0 and v == l[i - 1]:
        continue
Algorithm for finding multiset permutation given lexicographic index