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 as it
n=int(input())
p=tuple(map(int, input().split()))
q=tuple(map(int, input().split()))
dic=[i+1 for i in range(n)]
dic=list(it.permutations(dic,n))
a=dic.index(p)
b=dic.index(q)
print(abs(a-b))

Solution (Python):
import itertools
n=int(input())
p=tuple(map(int, input().split()))
q=tuple(map(int, input().split()))
x=list(itertools.permutations(range(1,n+1)))

a=x.index(p)+1
b=x.index(q)+1
print(abs(a-b))

Solution (Python):
import itertools
N = int(input())
num = [i for i in range(1,N+1)]
num_list = list(itertools.permutations(num))
ser_A = tuple(map(int,input().split()))
ser_B = tuple(map(int,input().split()))
A = 0
B = 0
for i in range(len(num_list)):
	if(num_list[i] == ser_A ):
		A = i+1
	if(num_list[i] == ser_B ):
		B = i+1
print(abs(A-B))

Solution (Python):
from math import factorial

def lexicographic_index(p):
    result = 0
    for j in range(len(p)):
        k = sum(1 for i in p[j + 1:] if i < p[j])
        result += k * factorial(len(p) - j - 1)
    return result

n = int(input())
p1 = "".join(input().split(" "))
p2 = "".join(input().split(" "))

print(abs(lexicographic_index(p1)- lexicographic_index(p2)))

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

p=set()
q=set()
r=[1]*N
r[0]=0
for j in range(N-2):
  i=j+1
  r[i+1]=r[i]*(i+1)
pnum=0
qnum=0
for i in range(N):
  k=0
  for j in range(P[i]):
    if {j+1}<=p:
      k=k
    else:
      k+=1
  pnum+=r[N-1-i]*(k-1)
  p.add(P[i])
  
  k=0
  for j in range(Q[i]):
    if {j+1}<=q:
      k=k
    else:
      k+=1
  qnum+=r[N-1-i]*(k-1)
  q.add(Q[i])
  
print(abs(pnum-qnum))

Solution (Python):
import itertools
_n = int(input())
n = range(1, _n + 1)
p = "-".join(list(map(str, input().split())))
q = "-".join(list(map(str, input().split())))
a = 0
b = 0
for i, x in enumerate(itertools.permutations(n)):
    _x = "-".join(list(map(str, x)))
    if p == _x:
        a = i + 1
    if q == _x:
        b = i + 1
    if a * b != 0:
        break
print(abs(a - b))

Solution (Python):
import sys, math
from math import factorial
def input():
    return sys.stdin.readline()[:-1]
from itertools import permutations, combinations
from collections import defaultdict, Counter
from bisect import bisect_left

sys.setrecursionlimit(10**7)
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))


orderP = 1
chk = []
for i, p in enumerate(P):
    chk_tmp = [1 if val < p else 0 for val in chk]
    cnt = sum(chk_tmp)
    orderP += (p - 1 - cnt) * factorial(N-(i+1))
    chk.append(p)    
    
orderQ = 1
chk = []
for i, q in enumerate(Q):
    chk_tmp = [1 if val < q else 0 for val in chk]
    cnt = sum(chk_tmp)
    orderQ += (q - 1 - cnt) * factorial(N-(i+1))
    chk.append(q)    

print(abs(orderP-orderQ))

Solution (Python):
import math

def counter(l, n):
    num = list(range(1,n+1))
    index = 0
    order = 0
    for i in range(n):
        index = num.index(l[i])
        order += math.factorial(n-i-1)*index
        del num[index]
    
    return order


n =int(input())
ps = list(map(int, input().split()))
qs = list(map(int, input().split()))

print(abs(counter(ps,n)-counter(qs,n)))

Solution (Python):
from itertools import permutations

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

cnt_p = 0
cnt_q = 0

for i, v in enumerate(permutations(range(1, N + 1), N)):
    if v == P:
        cnt_p = i
    if v == Q:
        cnt_q = i
        
print(abs(cnt_p - cnt_q))

Solution (Python):
import itertools

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

ans1 = []
ans2 = []
for i in itertools.permutations(p):
    ans1.append(i)
for i in itertools.permutations(q):
    ans2.append(i)   
    
ans1 = sorted(ans1)
ans2 = sorted(ans2)

p = tuple(p)
q = tuple(q)

for i in range(len(ans1)):
    if ans1[i] == p:
        break
for j in range(len(ans2)):
    if ans2[j] == q:
        break

print(abs(i - j))

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

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

for i in range(len(l)):
  if list(l[i]) == p:
    a = i
  if list(l[i]) == q:
    b = i    
  
print(abs(a-b))

Solution (Python):
import itertools

if __name__ == '__main__':
    n = int(input())
    s1 = [int(i) for i in input().split()]
    s2 = [int(i) for i in input().split()]
    l = []
    for i in range(n):
        l.append(i+1)
    g = []

    for v in itertools.permutations(l, n):
        h = ""
        for i in v:
            h=h+str(i)
        g.append(h)
    g.sort()

    index1 = ""
    index2 = ""
    for i in s1:
        index1 = index1 +str(i)
    for i in s2:
        index2 = index2 +str(i)

    print(abs(g.index(index1) - g.index(index2)))

Solution (Python):
import itertools
N = int(input())
P = list(map(int,(input().split())))
Q = list(map(int,(input().split())))
X = list(itertools.permutations(range(1,N+1)))
Y = 1 
for i in range(1,N+1):
    Y *= i
for i in range(Y):
    if list(X[i]) == P:
        p = i
    elif list(X[i]) == Q:
        q = i
if P == Q:
    ans = 0
else:
    ans = abs (p - q)
print(ans)

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

set_p = set()
set_q = set()
factorial = [1, 1, 2, 6, 24, 120, 720, 5040]
for j in range(N):
    set_p.add(j + 1)
    set_q.add(j + 1)

order_p = 0
order_q = 0
for i in range(N):
    set_p.discard(P[i])
    set_q.discard(Q[i])
    new_set_p = set()
    new_set_q = set()
    new_set_p = set() | set_p
    new_set_q = set() | set_q
    numofbig_p = 0
    numofbig_q = 0

    for k in new_set_p:
        if k > P[i]:
            numofbig_p += 1
    for h in new_set_q:
        if h > Q[i]:
            numofbig_q += 1
    order_p += numofbig_p * factorial[N - i - 1]
    order_q += numofbig_q*factorial[N-i-1]

ans = abs(order_p-order_q)
print(ans)

Solution (Python):
import itertools

n=int(input())

a="".join(input().split())
b="".join(input().split())


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

ary=[]
cnt=1
a1=0
b1=0
for balls in itertools.permutations(nums, n):
    
    if str(a) == str(int("".join(list(balls)))):
        a1=cnt
        
    if str(b) == str(int("".join(list(balls)))):
        b1=cnt
        
    cnt+=1

print(abs(a1-b1))

Solution (Python):
import math
import numpy as np

N = int(input())
list_p = list(map(int, input().split(" ")))
list_q = list(map(int, input().split(" ")))
a = 0
b = 0
for i in range(N) :
  a += math.factorial(N-i-1) * list(np.argsort(list_p)).index(0)
  b += math.factorial(N-i-1) * list(np.argsort(list_q)).index(0)
  list_p = list_p[1:]
  list_q = list_q[1:]
  
  
print(abs(a-b))

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

def fact(n):
    x = 1
    for i in range(1,n+1):
        x *= i
    return x

a = [1,2,3,4,5,6,7,8]
pj = 0
for i in range(0,n-1):
    pj += fact(n-i-1)*a[p[i]-1]
    for j in range(p[i]-1,8):
        a[j] -= 1

a = [1,2,3,4,5,6,7,8]
qj = 0
for i in range(0,n-1):
    qj += fact(n-i-1)*a[q[i]-1]
    for j in range(q[i]-1,8):
        a[j] -= 1


print(abs(pj - qj))

Solution (Python):
import math

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
li=list(range(1,n+1))
def f(id,i):
    idd = li.index(id)
    li.pop(li.index(id))
    ar = n - 1 - i
    return idd * math.factorial(ar)

sum1=1
sum2=1
for i in range(n):
    sum1+=f(p[i],i)
li=list(range(1,n+1))
for i in range(n):
    sum2+=f(q[i],i)
print(abs(sum1-sum2))

Solution (Python):
import itertools

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

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

result = list(map(lambda x: list(x), result))
a = result.index(p_list)
b = result.index(q_list)
print(abs(a-b))

Solution (Python):
def permutation_rank(p):
    n = len(p)
    fact = 1
    for i in range(2, n):
        fact *= i
    r = 0
    digits = list(range(n))
    for i in range(n-1):
        q = digits.index(p[i])
        r += fact * q
        del digits[q]
        fact //= (n - 1 - i)
    return r


N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P = [x - 1 for x in P]
Q = [x - 1 for x in Q]

print(abs(permutation_rank(P) - permutation_rank(Q)))

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

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

p = list(itertools.permutations(l, N)) 

print(abs((p.index(tuple(P)) + 1) - (p.index(tuple(Q)) + 1 )))

Solution (Python):
import sys, math
ip = sys.stdin.readline

def get_key(L:list):
    if len(L) == 0:
        return 1
    else:
        s = L[0]
        l_sorted = sorted(L, reverse=False)
        p = l_sorted.index(s)
        return p * math.factorial(len(l_sorted)-1) + get_key(L[1:])

N = int(ip().rstrip())
P = list(map(int, ip().rstrip().split()))
Q = list(map(int, ip().rstrip().split()))
key_P, key_Q = get_key(P), get_key(Q)
print(abs(key_P-key_Q))

Solution (Python):
import itertools

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

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

index_P = 0
index_Q = 0    

tmp = 1
for i in itertools.permutations(N_arr):

    if list(i) == P:
        index_P = tmp
    if list(i) == Q:
        index_Q = tmp 
    tmp += 1  
print(abs(index_P-index_Q))

Solution (Python):
import itertools

n = int(input())
a, b = tuple(map(int, input().split())), tuple(map(int, input().split()))
table = [x for x in range(1, n + 1)]
table2 = list(itertools.permutations(tuple(table)))


print(abs(table2.index(a) - table2.index(b)))

Solution (Python):
import math

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

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

p = 0
for i in range(N):
  p += v.index(P[i]) * math.factorial(N-i-1)
  v.remove(P[i])

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

q = 0
for i in range(N):
  q += v.index(Q[i]) * math.factorial(N-i-1)
  v.remove(Q[i])

print(abs(p-q))

Solution (Python):
import itertools
n=int(input())
a=[]
for i in range(1,n+1):
  a.append(i)
aa=list(itertools.permutations(a))

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

ap=aa.index(p)
aq=aa.index(q)

print(abs(ap-aq))

Solution (Python):
import itertools
n = int(input())
p = tuple(input().split())
q = tuple(input().split())
count_p = 0
count_q = 0
count = 1
for i in itertools.permutations(sorted(p)):
  if i == p:
    count_p = count
  if i == q:
    count_q = count
  count += 1
print(abs(count_p - count_q))

Solution (Python):
import math


def index(array, indexes):
    if not len(array):
        return 0
    s = array.pop()
    count = 0
    for i in range(s - 1):
        if indexes[i]:
            count += 1
    indexes[s - 1] = False
    return count * math.factorial(len(array)) + index(array, indexes)


n = int(input())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
p.reverse()
q.reverse()
pi = index(p, [True for _ in range(n)])
qi = index(q, [True for _ in range(n)])
print(abs(pi - qi))

Solution (Python):
import itertools

n = int(input())
p_L = list(map(int, input().split()))
q_L = list(map(int, input().split()))
p_val = 0
q_val = 0

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

for i in range(len(all_list)):
    if all_list[i] == p_L:
        p_val = i
        
    if all_list[i] == q_L:
        q_val = i


        
print(max(p_val-q_val, q_val-p_val))

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

fac = [0] * 8
fac[1] = 1
for i in range(2, N):
    fac[i] = i * fac[i - 1]

a = 0
l = list(range(1, N + 1))
for i in range(N):
    a += l.index(P[i]) * fac[len(l) - 1]
    l.remove(P[i])

b = 0
l = list(range(1, N + 1))
for i in range(N):
    b += l.index(Q[i]) * fac[len(l) - 1]
    l.remove(Q[i])

print(abs(a - b))

Solution (Python):
from itertools import permutations 

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

perm = list(permutations(range(1, N+1), N))
print(abs(perm.index(P) - perm.index(Q)))

Solution (Python):
import itertools
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = sorted(p)
i = 0
for v in itertools.permutations(r):
  i+=1
  if list(v) == p:
    a = i
  if list(v) == q:
    b = i
print(abs(a-b))

Solution (Python):
import itertools

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

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

a = set.index(P)
b = set.index(Q)
print(abs(a-b))

Solution (Python):

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)

def calc(xs):
    used = [False] * len(xs)
    ans = 0
    for i in range(len(xs)-1):
        
        j = 0
        for k in range(xs[i]):
            
            if not used[k]:
                j+=1
        
        ans += factorial(len(xs)-i-1) * (j-1)
        used[xs[i]-1] = True
    return ans

words = lambda t : list(map(t, input().split()))
n = int(input())
a = words(int)
b = words(int)

print(abs(calc(b) - calc(a)))

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

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

pord=0
qord=0
for i in range(n):
    pi=p[i]
    pord+=pjun.index(pi)*math.factorial(len(pjun)-1)
    pjun.remove(pi)

    qi=q[i]
    qord+=qjun.index(qi)*math.factorial(len(qjun)-1)
    qjun.remove(qi)

print(abs(pord-qord))

Solution (Python):
import itertools

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

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

per = itertools.permutations(list, n)

cnt = 0
for i in per:
	now = [i[j] for j in range(n)]
	if now == p:
		a = cnt
	if now == q:
		b = cnt
	cnt += 1

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()))
x=set([n for n in range(1,N+1)])
x=list(permutations(x,N))

a,b=0,0
for num in range(len(x)):
    if p==list(x[num]):
        a=num+1
        if p==q:
            b=num+1
            print(abs(a-b))
            exit()
    elif q==list(x[num]):
        b=num+1

print(abs(a-b))

Solution (Python):
n = int(input())
arrp = tuple(map(int,input().split()))
arrq = tuple(map(int,input().split()))

import itertools
for i,j in enumerate(itertools.permutations(range(1,n+1),n)):
    if j == arrp:
        a = i
    if j == arrq:
        b = i
        
print(abs(a-b))

Solution (Python):
import itertools
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
num=list(range(1,n+1))
c=list(itertools.permutations(num))
aa=c.index(tuple(a))
bb=c.index(tuple(b))
Ans=(aa+1)-(bb+1)
if Ans>=0:
  print(Ans)
else:
  print(-Ans)

Solution (Python):
import itertools 

def main():
    N = int(input())
    P = [int(i) for i in input().split()]
    Q = [int(i) for i in input().split()]
    P = tuple(P)
    Q = tuple(Q)

    num = [i for i in range(1,N+1)]
    num_ = list(itertools.permutations(num, N))
    
    a = num_.index(P)
    b = num_.index(Q)

    print(abs(a - b))


if __name__ == "__main__":
    main()

Solution (Python):

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

import itertools

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

for i in range(len(n)):
    if list(n[i]) == P:
        p = i+1
    if list(n[i]) == Q:
        q = i+1

print(abs(p-q))

Solution (Python):
import itertools
n = list(range(1, int(input())+1))
p, q = input(), input()
a = [str(x) for x in n]
new_list = []
for x in itertools.permutations(a):
    new_list.append(" ".join(x))
print(abs(new_list.index(p) - new_list.index(q)))

Solution (Python):
from itertools import permutations 

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

perm = list(permutations(range(1, N+1), N))
print(abs(perm.index(P) - perm.index(Q)))

Solution (Python):
import itertools
import numpy as np

N = int(input())
P = tuple(int(x) for x in input().split())
Q = tuple(int(x) for x in input().split())

A = [int(x+1) for x in range(N)]

array = list(itertools.permutations(A))

a = array.index(P)+1
b = array.index(Q)+1

print(abs(a-b))

Solution (Python):
import itertools

n = int(input())

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

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

a = 0
b = 0

for i, j in enumerate(itertools.permutations(n_list)):
    if j == p:
        a = i
    if q == j:
        b = i
        
print(abs(a - b))

Solution (Python):
import itertools

N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
table = list(itertools.permutations(sorted(P), N))
a = -1
b = -1
for i in range(len(table)):
    index = i + 1
    target = list(table[i])
    if target == P:
        a = index
    if target == Q:
        b = index
    if a > 0 and b > 0:
        break
print(abs(a - b))

Solution (Python):
import math
import itertools


def main():
    N = int(input())
    P = list(input().split())
    Q = list(input().split())

    
    

    combinations = []
    for c in itertools.permutations(range(1, N+1), N):
        c_s = map(str, c)
        combinations.append("".join(c_s))

    a = combinations.index("".join(P))
    b = combinations.index("".join(Q))

    print(abs(a-b))


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations

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

ptnp = list(permutations(p))
ptnq = list(permutations(q))

ptnp.sort()
ptnq.sort()

a = 0

for i in range(len(ptnp)):
    if ptnp[i] == p:
        a = i
        break

b = 0

for i in range(len(ptnq)):
    if ptnq[i] == q:
        b = i
        break

print(abs(a-b))

Solution (Python):
import itertools

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

if p == q:
  print(0)
  exit()

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

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

Solution (Python):
import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
y = [num for num in range(1, n+1)]
permutation_n = list(itertools.permutations(y, n))
a=b=0
count=1
for permu in permutation_n:
  if permu == p:
    a = count
  if permu == q:
    b = count
  if a > 0 and b > 0 :
    break
  count+=1
print(abs(a-b))

Solution (Python):
import itertools
import math

N = int(input())
S = tuple(map(int, input().split()))
T = tuple(map(int, input().split()))

input_list = [x + 1 for x in range(N)]


per = list(itertools.permutations(input_list))
f = 0
s = 0
for i, v in enumerate(per):
    if v == S:
        f = i + 1
    if v == T:
        s = i + 1

print(int(math.fabs(f - s)))

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

if N == 2:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            count += 1
            if [i+1, j+1] == P:
                a = count
            if [i+1, j+1] == Q:
                b = count
elif N == 3:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                count += 1
                if [i+1, j+1, k+1] == P:
                    a = count
                if [i+1, j+1, k+1] == Q:
                    b = count
elif N == 4:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    count += 1
                    if [i+1, j+1, k+1, l+1] == P:
                        a = count
                    if [i+1, j+1, k+1, l+1] == Q:
                        b = count
elif N == 5:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        count += 1
                        if [i+1, j+1, k+1, l+1, m+1] == P:
                            a = count
                        if [i+1, j+1, k+1, l+1, m+1] == Q:
                            b = count
elif N == 6:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        for n in range(N):
                            if n == m or n == l or n == k or n == j or n == i: continue
                            count += 1    
                            if [i+1, j+1, k+1, l+1, m+1, n+1] == P:
                                a = count
                            if [i+1, j+1, k+1, l+1, m+1, n+1] == Q:
                                b = count
elif N == 7:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        for n in range(N):
                            if n == m or n == l or n == k or n == j or n == i: continue
                            for o in range(N):
                                if o == n or o == m or o == l or o == k or o == j or o == i: continue
                                count += 1
                                if [i+1, j+1, k+1, l+1, m+1, n+1, o+1] == P:
                                    a = count
                                if [i+1, j+1, k+1, l+1, m+1, n+1, o+1] == Q:
                                    b = count
elif N == 8:
    for i in range(N):
        for j in range(N):
            if j == i: continue
            for k in range(N):
                if k == j or k == i: continue
                for l in range(N):
                    if l == k or l == j or l == i: continue
                    for m in range(N):
                        if m == l or m == k or m == j or m == i: continue
                        for n in range(N):
                            if n == m or n == l or n == k or n == j or n == i: continue
                            for o in range(N):
                                if o == n or o == m or o == l or o == k or o == j or o == i: continue
                                for p in range(N):
                                    if p == o or p == n or p == m or p == l or p == k or p == j or p == i: continue
                                    count += 1
                                    if [i+1, j+1, k+1, l+1, m+1, n+1, o+1, p+1] == P:
                                        a = count
                                    if [i+1, j+1, k+1, l+1, m+1, n+1, o+1, p+1] == Q:
                                        b = count

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()))
l=list(p)
l.sort()
pp,qq='',''
for i in range(n):
  pp+=str(p[i])
  qq+=str(q[i])
ll=permutations(l)
arr=[]
for i in ll:
  x=''
  for j in i:
    x+=str(j)
  arr.append(x)
print(abs(arr.index(pp)-arr.index(qq)))

Solution (Python):
import itertools
n = list(range(1, int(input())+1))
p, q = input(), input()
a = [str(x) for x in n]
new_list = []
for x in itertools.permutations(a):
    new_list.append(" ".join(x))
print(abs(new_list.index(p) - new_list.index(q)))

Solution (Python):
import math

n = input()

p = input().split(" ")
q = input().split(" ")

n = int(n)
p = [int(i) for i in p]
q = [int(i) for i in q]

def dictoder(x):
  r = 0
  t = list(range(len(x)))
  t = [i + 1 for i in t]
  
  for i in range(len(x)):
    r = r + math.factorial(n - 1 - i) * t.index(x[i])
    t.remove(x[i])
    
  return(r + 1)

print(abs(dictoder(p) - dictoder(q)))

Solution (Python):

import itertools

N = int(input())
P_tuple = tuple(list(map(int, input().split())))
Q_tuple = tuple(list(map(int, input().split())))

N_list = [i for i in range(1, N + 1)]
ans = 0
counter = 0
if P_tuple == Q_tuple:
    print(0)
else:
    for v_tuple in itertools.permutations(N_list):
        counter += 1
        if P_tuple == v_tuple:
            ans -= counter
        elif Q_tuple == v_tuple:
            ans += counter
    print(abs(ans))

Solution (Python):
from itertools import permutations

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

L=[_+1 for _ in range(n)]

a=1
for i in list(permutations(L)):
    if i==P:
        break
    a+=1

b=1
for i in list(permutations(L)):
    if i==Q:
        break
    b+=1

print(abs(a-b))

Solution (Python):
from itertools import permutations

N = int(input())
A = tuple(( str(i) for i in range(1, N+1) ))
P = tuple(input().split())
Q = tuple(input().split())

X = list(permutations(A))
count = 0

for x in X:
    if x >= min(P, Q) and x < max(P, Q):
        count = count+1
    elif x >= max(P, Q):
        break

print(count)

Solution (Python):
import itertools

n = int(input())
ps = input()
qs = input()

p = list(map(int,ps.split(' ')))
q = list(map(int,qs.split(' ')))
pn = 0
qn = 0

for x,a in enumerate(list(itertools.permutations([i+1 for i in range(n)],n))):
  if list(a)==p:
    pn = x
  if list(a)==q:
    qn = x

print(abs(pn-qn))

Solution (Python):
n=int(input())
pl=list(map(int,input().split()))
ql=list(map(int,input().split()))

import itertools
seq=[i for i in range(1,n+1)]

li=list(itertools.permutations(seq))
p=tuple(pl)
q=tuple(ql)

a=li.index(p)+1
b=li.index(q)+1

print(abs(a-b))

Solution (Python):
def main():
    import sys
    from itertools import permutations
    input = sys.stdin.readline

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

    P = ''.join(map(str, P))
    Q = ''.join(map(str, Q))

    l = list(range(1, N+1))
    a = b = 0
    s_list = []
    for s in permutations(l):
        s_list.append(''.join(map(str, s)))
    s_list.sort()

    for i, s in enumerate(s_list):
        if s == P:
            a = i
        if s == Q:
            b = i
    print(abs(a-b))


if __name__ == '__main__':
    main()

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


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

def BIT_update(tree, x):
    while x <= N:
        tree[x] += 1
        x += x & (-x)

def BIT_sum(tree, x):
    s = 0
    while x:
        s += tree[x]
        x -= x & (-x)
    return s


tree = [0] * (N + 1)
ans1 = 0
for i, x in enumerate(P):
    rank = x - BIT_sum(tree, x)
    ans1 += fact[N - i - 1] * (rank - 1)
    BIT_update(tree, x)

tree = [0] * (N + 1)
ans2 = 0
for i, x in enumerate(Q):
    rank = x - BIT_sum(tree, x)
    ans2 += fact[N - i - 1] * (rank - 1)
    BIT_update(tree, x)
print(abs(ans1-ans2))

Solution (Python):
import copy
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
a=0
b=0
pp=copy.copy(p)
qq=copy.copy(q)
pp.sort()
qq.sort()

def fact(x):
    if x<1:
        return 1
    return x*fact(x-1)

for i in range(n):
    f=fact(n-(i+1))
    ppp=p[i]
    qqq=q[i]
    ap=1
    bq=1
    for j in range(len(pp)):
        if j<len(pp):
            if ppp==pp[j]:
                pp.pop(j)
                ap=j+1
        if j<len(qq):
            if qqq==qq[j]:
                qq.pop(j)
                bq=j+1
    a+=f*ap
    b+=f*bq
print(abs(a-b))

Solution (Python):
import math
n = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

def count(r):
    c = 0
    li = list(range(1,n+1))
    for i in range(n-1):
        c+=li.index(r[i])*math.factorial(n-i-1)
        li.remove(r[i])
    return c

print(abs(count(p)-count(q)))

Solution (Python):
import itertools
n = list(range(1, int(input())+1))
p, q = input(), input()
a = [str(x) for x in n]
new_list = []
for x in itertools.permutations(a):
    new_list.append(" ".join(x))
print(abs(new_list.index(p) - new_list.index(q)))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import itertools

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

permutations = list(itertools.permutations(range(1,N+1)))
p = permutations.index(P)
q = permutations.index(Q)

answer = abs(p - q)
print(answer)

Solution (Python):
import math
n = int(input())
count_p = 1
count_q = 1
p_list = [i+1 for i in range(n)]
q_list = [i+1 for i in range(n)]
p = list(map(int, input().split()))
q = list(map(int, input().split()))
for i in range(n):
    count_p += p_list.index(p[i])*math.factorial(n-i-1)
    p_list.remove(p[i])
    count_q += q_list.index(q[i])*math.factorial(n-i-1)
    q_list.remove(q[i])
print(abs(count_p-count_q))

Solution (Python):
import itertools
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = list(map(list,list(itertools.permutations(list(i for i in range(1,N+1)),N))))
a = L.index(P)
b = L.index(Q)
print(abs(a-b))

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

Solution (Python):
import itertools
import math
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
L = list(map(list,list(itertools.permutations(list(i for i in range(1,N+1)),N))))
a = L.index(P)
b = L.index(Q)
print(abs(a-b))