from collections import defaultdict
n = 3
h = defaultdict(int)
xs = list(range(1, n + 1))
q = [(xs, 0)]
while q:
e, c = q.pop()
if c == n:
h[tuple(e)] += 1
if c < n:
for i in range(n):
x = e[:]
x[c], x[i] = x[i], x[c]
q.append((x, c + 1))
print(h)
To embed this project on your website, copy the following code and paste it into your website's HTML: