# ITP2_5_D# 1桁なら一意# 2桁は入れ替え# 3桁は先頭を決めて2桁の入れ替え# 4桁は先頭を決めて3桁の順列# ...# 再帰関数defpermutation(n,A):if n ==1:return[A]elif n ==2:
swaped_list = A[:]
swaped_list[-1]= A[-2]
swaped_list[-2]= A[-1]if swaped_list < A:return[swaped_list,A]else:return[A,swaped_list]# 先頭を決めて n-1 の順列を考えるelse:
result =[]for i inrange(1,n+1):
tmp = A[i-1]
B = A[:]
B.remove(tmp)
swaped_list = permutation(n-1,B)for x in swaped_list:
result.append([tmp]+x)return result
n =int(input())
result = permutation(n,[(i+1)for i inrange(n)])for x in result:print(*x)
from itertools import permutations
n =int(input())# 1からnまでの整数の順列を生成
permutation_list =list(permutations(range(1,n+1)))# 順列を表示for perm in permutation_list:print(*perm)