def permutation_rank(seq):
    ref = sorted(seq)
counts = Counter(ref)
if ref == seq:
    return 0
else:
    rank = 0
f = math.factorial(len(seq) - 1)
for x in sorted(set(ref)):
    if x < seq[0]:
        counts_copy = counts.copy()
Ранг перестановки