Problem:
 Score : 300 points    Problem Statement We have two permutations P and Q of size N (that is, P and Q are both rearrangements of (1,~2,~...,~N) ).  There are N! possible permutations of size N . Among them, let P and Q be the a -th and b -th lexicographically smallest permutations, respectively. Find |a - b| .      Notes For two sequences X and Y , X is said to be lexicographically smaller than Y if and only if there exists an integer k such that X_i = Y_i~(1 \leq i < k) and X_k < Y_k .      Constraints  2 \leq N \leq 8  P and Q are permutations of size N .         Input Input is given from Standard Input in the following format:  N  P_1  P_2  ...  P_N  Q_1  Q_2  ...  Q_N       Output Print |a - b| .        Sample Input 1 3
1 3 2
3 1 2      Sample Output 1 3  There are 6 permutations of size 3 : (1,~2,~3) , (1,~3,~2) , (2,~1,~3) , (2,~3,~1) , (3,~1,~2) , and (3,~2,~1) . Among them, (1,~3,~2) and (3,~1,~2) come 2 -nd and 5 -th in lexicographical order, so the answer is |2 - 5| = 3 .       Sample Input 2 8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1      Sample Output 2 17517       Sample Input 3 3
1 2 3
1 2 3      Sample Output 3 0

Solution (Python):
import itertools
N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]

for i, lst in enumerate(itertools.permutations(range(1, N+1))):
    if list(lst) == P:
        a = i
    if list(lst) == Q:
        b = i


print(abs(a-b))

Solution (Python):
from itertools import permutations 

n = int(input())
seq1 = tuple((int(x) for x in input().split()))
seq2 = tuple((int(x) for x in input().split()))

a = 0
b = 0
for i, combo in enumerate(permutations(range(1,n+1))):
  if combo == seq1:
    a = i
  if combo == seq2:
    b = i
    
print(abs(a-b))

Solution (Python):
import itertools

n = int(input())
num = [i+1 for i in range(n)]
lis = list(itertools.permutations(num))
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
print(abs(lis.index(p) - lis.index(q)))

Solution (Python):
import itertools
n=int(input())
a=tuple(map(int,input().split()))
b=tuple(map(int,input().split()))
seq=range(1,n+1)
l=list(itertools.permutations(seq))


a_num=0
b_num=0
for i in range(len(l)):
  if l[i]==a:
    a_num=i
  if l[i]==b:
    b_num=i

print(abs(a_num-b_num))

Solution (Python):
import itertools
N=int(input())
P=list(map(str,input().split()))
Q=list(map(str,input().split()))
S=""
for i in range(1,N+1):
    S+=str(i)
pidx=-1
qidx=-1
for i,m in enumerate(itertools.permutations(S,N)):
    if P==list(m):
        pidx=i
    if Q==list(m):
        qidx=i
A=pidx-qidx if pidx-qidx > 0 else -(pidx-qidx )
print(A)

Solution (Python):
import math

N = int(input())
P = list(map(int,input().split()))
S = list(map(int,input().split()))
Psum = 0
Ssum = 0
Pma = []
Sma = []
for i in range(N):
    p = P[i]
    s = S[i]
    for j in Pma:
        if P[i] > j:
            p -= 1
    for k in Sma:
        if S[i] > k:
            s -= 1
    Psum += math.factorial(N-(i+1))*(p-1)
    Ssum += math.factorial(N-(i+1))*(s-1)
    Pma.append(P[i])
    Sma.append(S[i])
print(abs(Psum-Ssum))

Solution (Python):
import itertools

n = int(input())
order = list(itertools.permutations(range(1,n+1)))

index_p = order.index(tuple([int(i) for i in input().split()]))
index_q = order.index(tuple(int(i) for i in input().split()))
print(abs(index_p - index_q))

Solution (Python):
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

n_list = list(itertools.permutations(range(1, n+1)))

def func1(lst, value):
    return [i for i, x in enumerate(lst) if x == value]

p_n = func1(n_list, p)
q_n = func1(n_list, q)

print(abs(p_n[0] - q_n[0]))

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))


d = {}
for i, junretsu in enumerate(permutations(i + 1 for i in range(N))):
    d[junretsu] = i

print(abs(d[P] - d[Q]))

Solution (Python):
import itertools

N = int(input())
P = tuple([int(p) for p in input().split()])
Q = tuple([int(q) for q in input().split()])
a, b = -1, -1
count = 1
for seq in itertools.permutations(tuple(range(1, N+1))):
    if seq == P:
        a = count
    if seq == Q:
        b = count
    if a != -1 and b != -1:
        break
    count += 1
print(abs(a-b))

Solution (Python):

import numpy as np

def fnSolv0(iNum, l, lFact):
  iRet = 1
  lNums= list(range(1,1+iNum))
  for iDigt, i in enumerate(l):
    i0 = lNums.index(i)
    lNums.remove(i)
    iRet += i0 * lFact[len(lNums)]
  return iRet
  

def fnSolv():
  iNum = int(input())
  lP = list(map(int, input().split()))
  lQ = list(map(int, input().split()))
  lFact = [1]
  for i in range(iNum):
    lFact.append(lFact[i]*(i+1))
  
  iP = fnSolv0(iNum, lP,lFact)
  iQ = fnSolv0(iNum, lQ,lFact)
  print(abs(iP - iQ))

if __name__ == '__main__':
  fnSolv()

Solution (Python):
import math

n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))


p_n=0
q_n=0
for i in range(n):
    count_p=p[i]-1
    count_q=q[i]-1
    for j in range(i):
        if p[j]<p[i]:
            count_p-=1
        if q[j]<q[i]:
            count_q-=1
    p_n+=math.factorial(n-i-1)*count_p
    q_n+=math.factorial(n-i-1)*count_q

print(abs(p_n-q_n))

Solution (Python):
import itertools

N = int(input())
P_list = [int(e) for e in input().split()]
Q_list = [int(e) for e in input().split()]

seq = [i for i in range(1,N+1)]
all = list(itertools.permutations(seq))
all.sort()

for i in range(len(all)):
  check_target = list(all[i])
  if check_target == P_list:
    a = i
  if check_target == Q_list:
    b = i

print(abs(a-b))

Solution (Python):
n = int(input())
p = list(map(int ,input().split()))
q = list(map(int ,input().split()))

p_o = 0
p_s = set()
q_o = 0
q_s = set()

fact = [1, 1]
for i in range(2,n):
  fact += [fact[i-1]*i]

for i in range(n):
  k = 0
  for j in range(1, p[i]):
    if not j in p_s:
      k += 1
  p_o += k * fact[n-i-1]
  p_s.add(p[i])

for i in range(n):
  k = 0
  for j in range(1, q[i]):
    if not j in q_s:
      k += 1
  q_o += k * fact[n-i-1]
  q_s.add(q[i])
  
print(abs(p_o - q_o))

Solution (Python):
import itertools
N = int(input())
P = ''.join(input().split())
Q = ''.join(input().split())
tmp = itertools.permutations([str(i+1) for i in range(N)])
target = []
for a in tmp:
    target.append(''.join(a))
print(abs(target.index(P)-target.index(Q)))

Solution (Python):
import itertools
N = int(input())

P = list(map(int, input().split()))
Q = list(map(int, input().split()))

p = ""
q = ""
for i in P:
    p += str(i)
p = int(p)
for i in Q:
    q += str(i)
q = int(q)
tmp = itertools.permutations([i+1 for i in range(N)])
target = []
for a in tmp:
    t = ""
    for s in a:
        t += str(s)
    target.append(int(t))
qidx = 0
pidx = 0
for i, t in enumerate(target):
    if p == t:
        pidx = i
    if q == t:
        qidx = i

print(abs(pidx-qidx))

Solution (Python):
import itertools

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))





p_count = -1
q_count = -1

base = [i for i in range(1,n + 1)]

all_data = list(itertools.permutations(base))

for i, chk in enumerate(all_data):
   
   if list(chk) == p:
      p_count = i

   if list(chk) == q:
      q_count = i

   if p_count >= 0 and q_count >= 0:
      break


print(str(abs(p_count - q_count)))

Solution (Python):
import math
import numpy as np
N = int(input())
A = np.array(list(input().split()))
B = np.array(list(input().split()))
a = 0
b = 0 
for i in range(N):
    arank = np.where(A[i:].argsort()==0)[0]
    brank = np.where(B[i:].argsort()==0)[0]
    a += arank*math.factorial(N-(i+1))
    b += brank*math.factorial(N-(i+1))
    
print(int(abs(a-b)))

Solution (Python):
import math

def count(n, target):
    c = 0
    check_list = [i + 1 for i in range(n)] 
    index = n
    for item in target: 
        check_list.remove(item)
        c = c + len([i for i in check_list if i < item]) * math.factorial(index - 1)
        index = index - 1
    return c + 1


if __name__ == '__main__':
    n = int(input())
    p = list(map(int, input().split()))
    q = list(map(int, input().split()))

    n_list = [i + 1 for i in range(n)]

    p_count = count(n, p)
    q_count = count(n, q)

    print(abs(p_count - q_count))

Solution (Python):
n = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

from itertools import permutations as pr
nums = range(1, n+1)
for i, num in enumerate(pr(nums)):
  lis = list(num)
  
  if lis == P:
    n_p = i
  if lis == Q:
    n_q = i
print(abs(n_p - n_q))

Solution (Python):
n=int(input())
p = list(input().split())
q = list(input().split())


from itertools import permutations, combinations,combinations_with_replacement,product
a=list(map(str,(range(1,n+1))))
t = sorted([''.join(i) for i in permutations(a)])
print(abs(t.index(''.join(p)) - t.index(''.join(q))))

Solution (Python):
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

num_list = list(itertools.permutations(range(1,n+1)))

print(abs(num_list.index(p)-num_list.index(q)))

Solution (Python):
import itertools

n = int(input())
p_list = list(map(int, input().split()))
q_list = list(map(int, input().split()))
num_list = []
num = 0
a = 0
b = 0

for i in itertools.permutations(range(1,n+1)):
    num_list = list(i)
    num += 1
    if num_list == p_list:
        a = num
    if num_list == q_list:
        b = num

print(abs(a-b))

Solution (Python):
import itertools

n = int(input())
p = tuple(map(int, input().split(" ")))
q = tuple(map(int, input().split(" ")))
l = [i for i in range(n, 0, -1)]

p_i = 0
q_i = 0

for i, v in enumerate(itertools.permutations(l, n)):
  if v == p:
    p_i = i + 1
  if v == q:
    q_i = i + 1

print(abs(p_i - q_i))

Solution (Python):
import itertools
import math
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
tmp = [ i for i in range(1,1+N)]
a = b = 0

for li,i in zip(itertools.permutations(tmp),range(1,math.factorial(N)+1)):
    if P==list(li):
        a = i
    if Q==list(li):
        b = i
    if a!=0 and b!=0:
        break

print(abs(a-b))

Solution (Python):
from itertools import permutations
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

i = 0

for x in permutations(range(1, n+1)):
	i += 1
	if list(x) == p:
		a = i
	if list(x) == q:
		b = i
print(abs(a-b))

Solution (Python):
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

import itertools as it

z = list(it.permutations(list(range(1, n+1))))
ans = abs(z.index(tuple(p)) - z.index(tuple(q)) )

print(ans)

Solution (Python):

import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
permutation = itertools.permutations(range(1, n+1))

cnt_p = 0
cnt_q = 0
finish_p = False
finish_q = False
for per in permutation:
    if per == p:
        finish_p = True
        cnt_p += 1
    if per == q:
        finish_q = True
        cnt_q += 1

    if finish_p and finish_q:
        break
    cnt_p += 1 if not finish_p else 0
    cnt_q += 1 if not finish_q else 0
print(max(cnt_p-cnt_q, cnt_q-cnt_p))

Solution (Python):
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
count=0

per=[x for x in range(1,n+1)]
per_list=itertools.permutations(per)

for c in per_list:
    d=list(c)
    count+=1
    if d==p:
        a=count

    if d==q:
        b=count
        

print(abs(a-b))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    p = tuple(LI())
    q = tuple(LI())
    a = b = -1
    for i,t in enumerate(itertools.permutations(range(1,n+1))):
        if t == p:
            a = i
        if t == q:
            b = i

    return abs(a-b)


print(main())

Solution (Python):
import itertools
N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(range(1,N+1,1))
a_cnt = 0
b_cnt = 0
for i in itertools.permutations(c,N):
  a_cnt += 1
  for j in range(len(c)):
    if a[j] == i[j]:
      if j == N-1:
        a_ans = a_cnt
        break
    else:
      break
for i in itertools.permutations(c,N):
  b_cnt += 1
  for j in range(len(c)):
    if b[j] == i[j]:
      if j == N-1:
        b_ans = b_cnt
        break
    else:
      break
print(abs(a_ans - b_ans))

Solution (Python):
import itertools
n = int(input())
t_p = tuple(map(int, input().split()))
t_q = tuple(map(int, input().split()))

l_p = list(itertools.permutations(range(1, n + 1), n))

print(abs((l_p.index(t_p) + 1) - (l_p.index(t_q) + 1)))

Solution (Python):
from itertools import permutations

N = int(input())
Pn = tuple(map(int, input().split()))
Qn = tuple(map(int, input().split()))

p, q = 0, 0
for i, t in enumerate(permutations(range(1, N+1), N)):
    if t == Pn:
        p = i
    if t == Qn:
        q = i

print(abs(p - q))

Solution (Python):
import itertools

def main():
    n = int(input())
    p_list = list(map(int, input().split()))
    q_list = list(map(int, input().split()))

    l = []
    for i in range(1,n+1):
        l.append(i)

    l = list(itertools.permutations(l, n))

    p_tu = tuple(p_list)
    q_tu = tuple(q_list)

    p_count = l.index(p_tu)

    q_count = l.index(q_tu)

    print(abs(p_count-q_count))

if __name__ == '__main__':
    main()

Solution (Python):

import math
import functools
import operator

N=int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))

A = list(range(1, N+1))

def permutation(n, r):
  if(n < r):
    return 0
  return int(math.factorial(n) / math.factorial(n - r))

def permutationList(data, r):
  length = len(data)
  total = permutation(length, r)
  result = []
  for i in range(total):
    element = []
    copyData = data[:]
    for digit in range(r):
      l = len(copyData)
      countUp = total / functools.reduce(operator.mul, range(l, length + 1))
      index = int(i / countUp) % l
      element.append(copyData.pop(index))
    result.append(element)
  return result

n_A = permutationList(A, N)
n_A = sorted(n_A)

index_p = n_A.index(P)
index_q = n_A.index(Q)

print(abs(index_q - index_p))

Solution (Python):
import itertools
num = int(input())
array1 = tuple(map(int, input().split()))
array2 = tuple(map(int, input().split()))
all_list = list(itertools.permutations(range(1, num + 1)))
 
array1_index = all_list.index(array1) + 1
array2_index = all_list.index(array2) + 1
 
if array1_index > array2_index:
  print(array1_index-array2_index)
else:
  print(array2_index-array1_index)

Solution (Python):
import itertools

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

junretu= list(itertools.permutations(range(1, n + 1)))
p_num = junretu.index(tuple(p)) + 1
q_num = junretu.index(tuple(q)) + 1

print(abs(p_num - q_num))

Solution (Python):
from itertools import*;a=lambda l:l.index(tuple(map(int,input().split())));c=list(permutations([i+1 for i in range(int(input()))]));print(abs(a(c)-a(c)))

Solution (Python):
from itertools import*
a=lambda:tuple(map(int,input().split()));c=list(permutations([i+1 for i in range(int(input()))]));print(abs(c.index(a())-c.index(a())))

Solution (Python):
from itertools import*
c=list(permutations([i+1 for i in range(int(input()))]));print(abs(c.index(tuple(map(int,input().split())))-c.index(tuple(map(int,input().split())))))

Solution (Python):
import itertools

N = int(input())
P = tuple(map(int, input().split())) 
Q = tuple(map(int, input().split())) 


orig = [i for i in range(1,N+1)]
a = 0
for x in itertools.permutations(orig):
    a += 1
    if x == P: break


orig = [i for i in range(1,N+1)]
b = 0
for x in itertools.permutations(orig):
    b += 1
    if x == Q: break
    
print(abs(a-b))

Solution (Python):
from itertools import permutations

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

allp = p.copy()
allp.sort()
allperms = list(map(list, permutations(allp)))
print(abs(allperms.index(p) - allperms.index(q)))

Solution (Python):
import itertools

N = input()
A = list(map(int, input().split()))
B = list(map(int, input().split()))

li = list(itertools.permutations(sorted(A)))

a = li.index(tuple(A))
b = li.index(tuple(B))

print(abs(a-b))

Solution (Python):
import copy


N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))


def f(l):
    if len(l) == 2:
        return [l, [l[1],l[0]]]

    result = []

    for i in l:
        l2 = copy.copy(l)
        l2.remove(i)
        for j in f(l2):
            result.append([i] + j)
    
    return result

l=[]
for i in range(1, N+1):
    l.append(i)

ll = f(l)


print(abs(ll.index(P)-ll.index(Q)))

Solution (Python):
import itertools


def t(n, p, q):
    np = tuple(map(int, p.split()))
    nq = tuple(map(int, q.split()))
    a = list(itertools.permutations(range(1, n + 1), n))
    return abs(a.index(np) - a.index(nq))


if __name__ == '__main__':
    n = int(input())
    p = input()
    q = input()
    print(t(n, p, q))

Solution (Python):
import itertools
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
nList=[]
for i in range(N):
  nList.append(i+1)

nAllList=[]
for i in itertools.permutations(nList):
  nAllList.append(i)
nAllList.sort()

pNum=0
qNum=0

for i in range(len(nAllList)):
  if(nAllList[i]==P):
    pNum=i+1
  if(nAllList[i]==Q):
    qNum=i+1

print(abs(pNum-qNum))

Solution (Python):
N = int(input())
P = input().split()
P = [int(t) for t in P]
Q = input().split()
Q = [int(t) for t in Q]


def factorial(n):
    val = 1
    fact = [1]
    for i in range(1, n + 1):
        val *= i
        fact.append(val)
    return fact


factArray = factorial(N)


def position(a):
    used = [False] * N
    pos = 0
    for i in range(N):
        notUsedCount = 0
        for j in range(a[i] - 1):
            if not used[j]:
                notUsedCount += 1
        pos += (notUsedCount * factArray[N - i - 1])
        used[a[i] - 1] = True
    return pos + 1


print(abs(position(P) - position(Q)))

Solution (Python):
from itertools import permutations
n = int(input())
x = ''.join([str(i) for i in range(1,  n + 1)])
p = tuple(input().split())
q = tuple(input().split())
pers = list(permutations(x, n))
pers.sort()
print(abs(pers.index(p) - pers.index(q)))

Solution (Python):
import itertools

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

realnums = p.copy()
realnums.sort()
perm = list(map(list, itertools.permutations(realnums)))

print(abs(perm.index(q) - perm.index(p)))

Solution (Python):
import itertools

if __name__ == '__main__':
    
    
    
    n = int(input())
    p = list(map(int, input().split(' ')))
    q = list(map(int, input().split(' ')))

    realnums = p.copy()

    realnums.sort()

    perm = list(map(list, itertools.permutations(realnums)))

    print(abs(perm.index(q) - perm.index(p)))

Solution (Python):
import itertools

if __name__ == '__main__':
    
    
    
    n = int(input())
    p = input().split(' ')
    q = input().split(' ')

    for i in range(0, len(q)):
        p[i] = int(p[i])
        q[i] = int(q[i])

    realnums = p.copy()

    realnums.sort()

    perm = list(itertools.permutations(realnums))

    it = 0

    qplace = 0
    pplace = 0

    for i in perm:
        if list(i) == q:
            qplace = it
        if list(i) == p:
            pplace = it
        it += 1
    print(abs(qplace - pplace))

Solution (Python):
import itertools

if __name__ == "__main__":
    n = int(input())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    x = list(itertools.permutations(range(1, n + 1)))
    print(abs(x.index(a) - x.index(b)))

Solution (Python):
import itertools

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
a = 0
b = 0
ans = [list(v) for v in itertools.permutations(sorted(p))]

print(abs(ans.index(p) - ans.index(q)))

Solution (Python):
import itertools
N = int(input())

P = list(map(int, input().split()))
Q = list(map(int, input().split()))

lis = [i for i in range(1, N+1)]

all_permutations = [item for item in itertools.permutations(lis, N)]

all_numeric_permutations = []
for item in all_permutations:
  num = 0
  i = 10 ** (N - 1)
  for letter in item:
    num += i * letter
    i /= 10
  all_numeric_permutations.append(num)
  
num_P = 0
i = 10 ** (N - 1)
for item in P:
  num_P += i * item
  i /= 10
  
num_Q = 0
i = 10 ** (N - 1)  
for item in Q:
  num_Q += i * item
  i /= 10

a = all_numeric_permutations.index(num_P) + 1
b = all_numeric_permutations.index(num_Q) + 1

print(abs(a - b))

Solution (Python):
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
ans = [list(v) for v in itertools.permutations(sorted(P))]
print(abs(ans.index(P)-ans.index(Q)))

Solution (Python):
import itertools
n = int(input())
p = tuple(i for i in input().split())
q = tuple(i for i in input().split())
pi = 0
qi = 0

for i,j in enumerate(itertools.permutations(str(i) for i in range(1,n+1))):
    if p == j:
        pi = i
    if q == j:
        qi = i

print(abs(pi-qi))

Solution (Python):
n=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
def sub_junretu(num,sub_arr,arr):
    
    
    
    
    for i in range(1,num+1):
        if i in sub_arr:
            continue
        tmp=sub_arr.copy()
        
        
        
        tmp.append(i)
        
        sub_junretu(num,tmp,arr)
    
    
    if len(sub_arr)==num:
        arr.append(sub_arr)
    
def junretu(num,arr):
    for i in range(1,num+1):
        sub_junretu(num,[i],arr)
    return arr

arr=junretu(n,[])
for num,k in enumerate(arr):
    if P==k:
        p=num
    if Q==k:
        q=num
print(abs(p-q))

Solution (Python):
def fact(n):
    if n <= 0:
        return 0
    ans = 1
    for i in range(n):
        ans *= i + 1
    return ans


def getNum(N, a):
    num = 1
    for i in range(N):
        dig = a[i]
        cnt = 0
        for j in range(i):
            if dig > a[j]:
                cnt += 1
        num += (dig - cnt - 1) * fact(N - i - 1)
    return num


N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
print(abs(getNum(N, P) - getNum(N, Q)))

Solution (Python):
import math
import calendar
import fractions
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
seq = [i for i in range(1, n + 1)]
all_iter = list(itertools.permutations(seq))


ans = 0
for i in range(len(all_iter)):
    item = all_iter[i]
    if item == p and item == q:
        ans = 0
    elif item == p:
        ans += i
    elif item == q:
        ans -= i
print(abs(ans))

Solution (Python):
import itertools
import sys

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

if P == Q:
    print(0)
else:
    M = [0]*N
    for i in range(N):
        M[i] = i+1

    ans = []
    for i, v in enumerate(itertools.permutations(M, N)):
        if v in [P, Q]:
            ans.append(i)

    print(abs(ans[0] - ans[1]))

Solution (Python):
import itertools
N = int(input())
P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

X = list(range(1, N + 1))
Y = list(itertools.permutations(X))

a = Y.index(tuple(P)) + 1

b = Y.index(tuple(Q)) + 1

if a > b:
    print(a - b)
else:
    print(b - a)

Solution (Python):
import itertools

N = int(input())
P = ''.join(input().split())
Q = ''.join(input().split())

x = [''.join(map(str, a)) for a in itertools.permutations([x for x in range(1, N+1)])]

print(abs(x.index(P) - x.index(Q)))

Solution (Python):
from itertools import permutations

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))

a = b = None
for i, xs in enumerate(permutations(range(1, N + 1))):
    if xs == P:
        a = i
    if xs == Q:
        b = i
print(abs(a - b))

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))


li = []
li2 = []
count1 = 0
count2 = 0
kaijyou = 1
for i in range(1, N):
    kaijyou *= i
kaijyoucpy = kaijyou
for i in range(1, N+1):
    li.append(i)
    li2.append(i)
for i in range(N-1):

    count1 += (li.index(P[i])) * kaijyou
    li.remove(P[i])
    kaijyou  = kaijyou // (N - 1- i)

kaijyou = kaijyoucpy
for i in range(N-1):
    count2 += (li2.index(Q[i]))  * kaijyou
    li2.remove(Q[i])
    kaijyou = kaijyou // (N - 1 - i)

print(abs(count1 - count2))

Solution (Python):
import itertools

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
l = [i for i in range(1,n+1)]
ans_l = []
for i in itertools.permutations(l,n):
    ans_l.append(list(i))

ans = (ans_l.index(a)+1) - (ans_l.index(b)+1)
print(abs(ans))

Solution (Python):
import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
length = [i+1 for i in range(n)]
permut = list(itertools.permutations(length))
print(max((permut.index(p)) - (permut.index(q)) , (permut.index(q)) - permut.index(p)))

Solution (Python):
import math
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
A=[]
a=b=0
for i in range(1,n+1):
  A.append(i)
B=A.copy()
for i in range(n):
  a=a+A.index(p[i])*math.factorial(n-i-1)
  b=b+B.index(q[i])*math.factorial(n-i-1)
  A.remove(p[i])
  B.remove(q[i]) 
print(abs(a-b))

Solution (Python):
from itertools import permutations

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
a, b = 0, 0
for i, x in enumerate(permutations(list(range(1, n+1)))):
    if list(x) == p:
        a = i
    if list(x) == q:
        b = i
print(abs(a-b))

Solution (Python):
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

ans = []
l = range(1, N + 1)
import itertools

c = 0
for v in itertools.permutations(l, N):
    if list(v) == P:
        a = c
    if list(v) == Q:
        b = c
    c += 1

print(abs(b - a))

Solution (Python):
import sys
from itertools import permutations

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N = INT()
    P = tuple(LIST())
    Q = tuple(LIST())

    for i, tup in enumerate(list(permutations(range(1, N+1)))):
        if P == tup: a = i
        if Q == tup: b = i

    print(abs(a - b))


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))

P="".join(map(str,P))
Q="".join(map(str,Q))
for i,v in enumerate(permutations(range(1,N+1))):
  v="".join(map(str,v))
  if P==v:
    a=i
  if Q==v:
    b=i
print(abs(a-b))

Solution (Python):
import sys
import itertools

def main():
	
    
	
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))

    permutations = list(itertools.permutations(range(1, n+1)))

    a = permutations.index(p)
    b = permutations.index(q)

    print(abs(a - b))


if __name__ == '__main__':
    main()

Solution (Python):
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))

A = list(range(1, N + 1))
for i, x in enumerate(itertools.permutations(A)):
    if list(x) == P:
        pn = i
    if list(x) == Q:
        qn = i
print(abs(pn - qn))

Solution (Python):
import math


N = int(input())
list1 = list(map(int, input().split()))
list2 = list(map(int, input().split()))

def CO(l):
    n = len(l)
    if n != 1:
        m = sorted(l)
        a = l[0]
        s = m.index(a)
        l.remove(a)
        return((s)*math.factorial(n-1)+CO(l))
    else:
        return 1

print(abs(CO(list1)-CO(list2)))

Solution (Python):
from itertools import permutations
n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
l = [i+1 for i in range(n)]
cnt = 0
for v in permutations(l, n):
    cnt += 1
    if v == P:
        p = cnt
    if v == Q:
        q = cnt
print(abs(p-q))

Solution (Python):

from math import factorial
def tempcalc(lst, N):
    templst = [i for i in range(1, N + 1)]
    ord = (lst[0] - 1) * factorial(N - 1) + 1
    templst.remove(lst[0])

    for i in range(N - 1):
        ord += (templst.index(lst[i + 1]) - 1) * factorial(N - i - 2)
        templst.remove(lst[i + 1])
    return ord

def CountOrder():
    N = int(input())
    P = []
    Q = []
    for _ in range(1):
        P = list(map(int, input().split()))
        Q = list(map(int, input().split()))


    ord = abs(tempcalc(P, N) - tempcalc(Q, N))

    print(ord)

CountOrder()

Solution (Python):
def factorial(n):
    return n * factorial(n - 1) if n > 0 else 1

def rank(P):
    N = len(P)
    r = list(range(N))
    result = 0
    for i in range(N):
        result += r[P[i]] * factorial(N - 1 - i)
        for j in range(P[i] + 1, N):
            r[j] -= 1
    return result

N = int(input())
P = list(map(lambda s: int(s) - 1, input().split()))
Q = list(map(lambda s: int(s) - 1, input().split()))

print(abs(rank(P) - rank(Q)))

Solution (Python):
import itertools

n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))


l=[i+1 for i in range(n)]
t = list(itertools.permutations(l, n))

a=t.index(p)
b=t.index(q)
 
print(abs(a-b))

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
Factorial_list = [1,1]
for i in range(N-2):
    Factorial_list.append(Factorial_list[-1]*(i+2))
Factorial_list.reverse()
P_cnt = [0 for i in range(N)]
Q_cnt = [0 for i in range(N)]
for i in range(N):
    cnt = 0
    for j in range(i+1,N):
        if P[i]>P[j]:
            cnt+=1
    P_cnt[i]=cnt
for i in range(N):
    cnt = 0
    for j in range(i+1,N):
        if Q[i]>Q[j]:
            cnt+=1
    Q_cnt[i]=cnt
a = 0
b = 0
for i in range(N):
    a+=P_cnt[i]*Factorial_list[i]
    b+=Q_cnt[i]*Factorial_list[i]
if a-b>=0:
    print(a-b)
else:
    print(b-a)

Solution (Python):
from itertools import permutations

n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
cnt=1
for i in permutations(range(1,n+1),n):
    i=list(i)
    if i==p:
        a=cnt
    if i==q:
        b=cnt
    cnt+=1
print(abs(a-b))

Solution (Python):
import itertools

N = int(input())

P = input().split(" ")
Q = input().split(" ")

P = int(''.join(P))
Q = int(''.join(Q))

nums = []
for i in itertools.permutations(range(1, N + 1)):
    num = ''
    for j in i:
        num += str(j)
    num = int(num)
    nums.append(num)

print(abs(nums.index(P) - nums.index(Q)))

Solution (Python):


import math
import itertools

N = int(input())

P = [int(x) for x in input().split()]
Q = [int(x) for x in input().split()]

n = list(itertools.permutations(range(1, N + 1)))

a = n.index(tuple(P))
b = n.index(tuple(Q))

print(abs(a - b))