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):
from itertools import permutations


def main():
    N = int(input())
    P = list(map(int, input().split()))
    Q = list(map(int, input().split()))
    dic = list(permutations(range(1, N+1), N))
    print(abs(dic.index(tuple(P)) - dic.index(tuple(Q))))


if __name__ == "__main__":
    main()

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

from itertools import permutations

l = sorted(list(permutations(range(1, n+1))))

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

Solution (Python):
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
cands = itertools.permutations(range(1,N+1))
cands = [list(e) for e in cands]
print(abs(cands.index(P) - cands.index(Q)))

Solution (Python):
import itertools
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
t=[i+1 for i in range(N)]
S=list(itertools.permutations(t))
a=0
b=0
for i in range(len(S)):
    if list(S[i])==P:
        a=i
    if list(S[i])==Q:
        b=i
print(abs(a-b))

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

import math
import itertools

if P == Q:
  print(0)
  exit(0)
count = 0
a,b = 0,0
l = list(range(1,N+1))
for v in itertools.permutations(l):
  count += 1
  if v == P:
    a = count
  elif v == Q:
    b = count

print(abs(a-b))

Solution (Python):
import itertools
N = int(input())
P_list = tuple(map(int,input().split()))
Q_list = tuple(map(int,input().split()))

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

check_list = list(itertools.permutations(tem_list,N))

for i in range(0,len(check_list)):
    if check_list[i] == P_list:
        tem_1 = i
    if check_list[i] == Q_list:
        tem_2 = i

print(abs(tem_2-tem_1))

Solution (Python):
import itertools
N = int(input())
P_list = tuple(map(int,input().split()))
Q_list = tuple(map(int,input().split()))

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

check_list = list(itertools.permutations(tem_list,N))

for i in range(0,len(check_list)):
    if check_list[i] == P_list:
        tem_1 = i
for j in range(0,len(check_list)):
    if check_list[j] == Q_list:
        tem_2 = j

print(abs(tem_2-tem_1))

Solution (Python):
def f(x):
    return x-1
import itertools
import math
N = int(input())
P = list(map(int, input().split()))
P = list(map(f, P))
Q = list(map(int, input().split()))
Q = list(map(f, Q))
S = list(range(N))
permutation = list(itertools.permutations(S))
a,b = 0,0
for i in range(len(permutation)):
    if P == list(permutation[i]):
        a = i
    if Q == list(permutation[i]):
        b = i
print(int(math.fabs(a-b)))

Solution (Python):
import itertools
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
nums = list(range(1,n+1))
d= [ list(i) for i in itertools.permutations(nums)]
a=0
b=0
for i in range(len(d)):
  if p==d[i]:
    a=i
  if q==d[i]:
    b=i
print(abs(a-b))

Solution (Python):
import itertools

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

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

print(solve())

Solution (Python):
def perm(N):
    if N==0:return 0
    a=1
    for i in range(1,N+1):
        a*=i
    return a

def getposi(P):
    N=len(P)
    l=list(range(1,N+1))
    a = 0
    for i,num in enumerate(P):
        a+=perm(N-i-1)*l.index(num)
        l.remove(num)
    return a

stdin = open(0).read().split('\n')
N = int(stdin[0])
P = list(map(int,stdin[1].split()))
Q = list(map(int,stdin[2].split()))

a=getposi(P)
b=getposi(Q)

print(abs(a-b))

Solution (Python):
from itertools import permutations
stdin = open(0).read().split('\n')
N = int(stdin[0])

P=int("".join(list(stdin[1].split())))
Q=int("".join(list(stdin[2].split())))

l = []
for pp in permutations(range(1, N+1)):
    ll = []
    for _p in pp:
        ll.append(str(_p))
    l.append(int("".join(ll)))
a = l.index(P)
b = l.index(Q)
print(abs(a - b))

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

def kai(n):
  if(n==1 or n==0):
    return 1
  return n*kai(n-1)

op = 1
oq = 1

for i in range(N):
  tp = P[i] - 1
  tq = Q[i] - 1
  for j in range(i+1):
    if (P[j]<P[i]):
      tp -= 1
    if (Q[j]<Q[i]):
      tq -= 1

  op += tp*kai(N-(i+1))
  oq += tq*kai(N-(i+1))

print (abs(oq-op))

Solution (Python):
import itertools

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

a = -1
b = -1
ind = 0
for i in itertools.permutations(list(range(1, N+1))):

    if p == i:
        a = ind
    if q == i:
        b = ind
    ind += 1

if a == -1 or b == -1:
    print(a, b)
else:
    print(abs(a-b))

Solution (Python):
import itertools
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    a=tuple(map(int, input().split()))
    b=tuple(map(int,input().split()))
    l=list(itertools.permutations(range(1,n+1)))
    aa=l.index(a)
    bb=l.index(b)
    print(abs(aa-bb))
resolve()

Solution (Python):
import itertools
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(range(1,n+1))
    a=tuple(map(int, input().split()))
    b=tuple(map(int,input().split()))
    cnt=0
    aa=0
    bb=0
    for i in itertools.permutations(l,n):
        cnt+=1
        if a==i:
            aa=cnt
        if b==i:
            bb=cnt
    print(abs(aa-bb))
resolve()

Solution (Python):
from functools import reduce
def dfs(num):
    global count
    if len(str(num)) != len(set(str(num))):
        return

    if len(str(num)) == N:
        if len(set(str(num))) == len(str(num)):
            count += 1
        if num == P:
            ans.append(count)
        if num == Q:
            ans.append(count)
        return

    else:
        for i in range(1, N + 1):
            dfs(num * 10 + i)


N = int(input())
P = int("".join(list(input().split())))
Q = int("".join(list(input().split())))
ans = []
count = 0

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

Solution (Python):
import itertools
def I(): return int(input())
def LI(): return list(map(int,input().split()))
N = I()
P = LI()
Q = LI()
sequence = [list(x) for x in itertools.permutations(range(1,N+1))]
print(abs(sequence.index(P)-sequence.index(Q)))

Solution (Python):
import itertools
import sys
input = sys.stdin.readline

n = int(input())
pn = list(map(int, input().split()))
qn = list(map(int, input().split()))

n_list = [x for x in range(1, n+1)]
count = 0 
ans = 0
for i in itertools.permutations(n_list, n):
    count += 1
    if list(i) == pn:
        ans += count
    if list(i) == qn:
        ans -= count

print(abs(ans))

Solution (Python):
import itertools


def main():
    
    n = int(input())

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

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

    ap = -1
    aq = -1
    count = 0
    for perlist in permutations_lis:
        count += 1
        is_p = True
        is_q = True
        for i in range(n):
            pi = perlist[i]
            if is_p and pi != p[i]:
                is_p = False
            if is_q and pi != q[i]:
                is_q = False
            if not is_p and not is_q:
                break

        if is_p:
            ap = count
        if is_q:
            aq = count

        if ap != -1 and aq != -1:
            break

    print(abs(ap-aq))


main()

Solution (Python):
import itertools
def I(): return int(input())
def TI(): return tuple(map(int,input().split()))
N = I()
P = TI()
Q = TI()
sequence = [x for x in itertools.permutations(range(1,N+1))]
print(abs(sequence.index(P)-sequence.index(Q)))

Solution (Python):
import itertools

n= int(input())

p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]


a= [i for i in range(1,n+1)]
index =0
b=0
c=0
for i in itertools.permutations(a):
    index += 1    
    if list(i)==p:
       b= index
    if list(i)==q:
        c =index
print(abs(b-c))

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


n_new=list(itertools.permutations(n,N))
map(list,n_new)
a=n_new.index(P)
b=n_new.index(Q)
print(abs(a-b))

Solution (Python):
cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))
q = list(map(int, cin[2].split(' ')))

import itertools

a = 0
b = 0
for idx, case in enumerate(itertools.permutations(range(1, n+1))):
    if case == tuple(p):
        a = idx+1
    if case == tuple(q):
        b = idx+1

print(abs(a-b))

Solution (Python):
import itertools


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

    if P == Q:
        print(0)
        return

    i = 0
    a, b = 0, 0
    for v in itertools.permutations(range(1, N+1)):
        i += 1
        if v == P:
            a = i
        elif v == Q:
            b = i

    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()))
Nl =[]
for i in range(N):
    Nl.append(i+1)

P_answer = 0
Q_answer = 0
for i, permu in enumerate(itertools.permutations(Nl)):
    if list(permu) == P:
        P_answer = i+1
    if list(permu) == Q:
        Q_answer = i+1

print(abs(P_answer-Q_answer))

Solution (Python):
import math
import itertools
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
total = 1
cnt = 0
p = 0
q = 0
for v in itertools.permutations(range(1, N+1)):
    cnt += 1
    if v == P:
        p = cnt
    if v == Q:
        q = cnt
print(abs(p - q))

Solution (Python):
import itertools
def main(n,p,q):
    d = sorted(list(itertools.permutations(p)))
    return abs(d.index(tuple(p)) - d.index(tuple(q)))
    
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))

print(main(n,p,q))

Solution (Python):
import itertools
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
permutation_list = list(itertools.permutations(p, n))
sort_lst = sorted(permutation_list)
x = tuple(p)
y = tuple(q)
a = sort_lst.index(x)
b = sort_lst.index(y)
print(abs((a+1)-(b+1)))

Solution (Python):

import math

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

ans = 0


def calc(data):
    ans = 0
    flag = [1]*n
    for i in range(len(data)):
        num = data[i]
        sum_ = sum(flag[:max(0, num-1)])
        ans += sum_*math.factorial(len(data)-i-1)
        flag[num-1] = 0
    return ans


ans = calc(p)-calc(q)

print(abs(ans))

Solution (Python):
from itertools import permutations

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

perms = []
for order in permutations(list(map(lambda x:x + 1, range(N)))):
    order = map(str, list(order))
    perms.append(int(''.join(order)))

def binary_search(x):
    left_index = 0
    right_index = len(perms)
    while right_index - left_index > 1:
        mid_index = (left_index + right_index) // 2
        if perms[mid_index] <= x:
            left_index = mid_index
        else:
            right_index = mid_index
    return left_index + 1 
            
print(abs(binary_search(P) - binary_search(Q)))

Solution (Python):
import math
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
def count(m):
    num = 1
    for i in range(n-1):
        num += (m[i]-1)*(math.factorial(n-i-1))
        for j in range(i+1,n):
            if m[i] < m[j]:
                m[j] -= 1
    return num
print(abs(count(p)-count(q)))

Solution (Python):
from itertools import permutations
N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
pi = 0
qi = 0
cnt = 0
for p in permutations(range(1,1+N)):
    cnt += 1
    
    if p == P:
        pi = cnt
    if p == Q:
        qi = cnt
        
print(abs(qi-pi))

Solution (Python):
from itertools import *
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
a=0
b=0
cnt=0
for i in permutations(range(1,n+1)):
  cnt+=1
  if p==i:
    a=cnt
  if q==i:
    b=cnt
print(abs(a-b))

Solution (Python):
import itertools
n=int(input())
p_tuple=tuple(map(int,input().split()))
q_tuple=tuple(map(int,input().split()))
sort_list=list(range(1,n+1))
all_list=list(itertools.permutations(sort_list, n))
p_index=all_list.index(p_tuple)
q_index=all_list.index(q_tuple)
print(abs(p_index-q_index))

Solution (Python):
n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
g = [i for i in range(1,n+1)]
m = [i for i in range(1,n+1)]
ansp = 0
ansq = 0
def kai(n):
    k = 1
    for i in range(1,n+1):
        k = k*i
    return k
for i in range(n-1):
    r = g.index(p[i])
    ansp += r*kai(n-1-i)
    g.pop(r)
for i in range(n-1):
    r = m.index(q[i])
    ansq += r*kai(n-1-i)
    m.pop(r)
print(abs(ansp-ansq))

Solution (Python):
import itertools

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


    cnt = 1
    ans = 0
    for i in itertools.permutations(range(1, n+1)):
        if i == p:
            ans += cnt
        if i == q:
            ans -= cnt
        cnt += 1


    return abs(ans)

    
print(solve())

Solution (Python):

'''
全パターン作ってソートで順番みる
'''
import itertools
N = int(input())
P = tuple(list(map(int,input().split())))
Q = tuple(list(map(int,input().split())))

l = [i+1 for i in range(N)]
target =  itertools.permutations(l)
cnt=0
pcnt = 0
qcnt = 0
for i in target:
    cnt += 1
    if i == P:
        pcnt=cnt
    if i == Q:
        qcnt=cnt

print(abs(pcnt-qcnt))

Solution (Python):
import itertools as itr
n = int(input())
first = list(range(1, n + 1))
p = list(map(int,input().split()))
q = list(map(int,input().split()))
posp, posq = 0, 0
num = 0
for i in itr.permutations(first):
  num += 1
  i = list(i)
  if i == p:
    posp = num
  if i == q:
    posq = num
print(abs(posp - posq))

Solution (Python):
N=int(input())
P=[int(i) for i in input().split()]
Q=[int(i) for i in input().split()]
from itertools import permutations as perm
lis=[list(i) for i in perm(range(1,N+1),N)]
print(abs(lis.index(P)-lis.index(Q)))

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

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

ans = 0
k = 0
for A in permutations(l):
    a = list(A)
    k += 1
    pf = True
    qf = True
    for i in range(n):
        if a[i] != p[i]:
            pf = False
        if a[i] != q[i]:
            qf = False
    if pf:
        ans = abs(ans - k)
    if qf:
        ans = abs(ans - k)

print(ans)

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

from itertools import permutations
from math import factorial

X=[i+1 for i in range(N)]
X_list = list(permutations(X))

P=tuple(P)
Q=tuple(Q)

ans=[0,0]
for j in range(factorial(N)):
    if X_list[j]==P:
        ans[0] = j
        break
for k in range(factorial(N)):
    if X_list[k]==Q:
        ans[1] = k
        break
print(abs(ans[0]-ans[1]))

Solution (Python):



def solve():
    import itertools
    import bisect

    N = int(input())
    Ps = tuple(map(int, input().split()))
    Qs = tuple(map(int, input().split()))

    lst = [i for i in range(1, N+1)]
    origins = list(itertools.permutations(lst, r=N))
    origins.sort()

    for i, p in enumerate(origins):
        if p == Ps:
            a = i
        if p == Qs:
            b = i

    print(abs(a-b))



if __name__ == "__main__":
    solve()

Solution (Python):
from itertools import permutations

N = int(input())

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

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

numbers = list(permutations(N_list,N))
sor_numbers = sorted(numbers)

P_answer = sor_numbers.index(P)
Q_answer = sor_numbers.index(Q)


print(abs(P_answer - Q_answer ))

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

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

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

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

import itertools

A = []
for i in range(1,n+1):
    A.append(i)
    
p = itertools.permutations(A,n)
p = list(p)

for i in range(len(p)):
    p[i] = list(p[i])


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

Solution (Python):


import math

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

d1=[i+1 for i in range(n)]
d2=[i+1 for i in range(n)]
mp=[math.factorial(i) for i in reversed(range(n))]

np=0
nq=0
for i in range(n):
    np+=d1.index(p[i])*mp[i]
    d1.remove(p[i])
    nq+=d2.index(q[i])*mp[i]
    d2.remove(q[i])
print(abs(np-nq))

Solution (Python):
import  itertools

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

p = sorted(list(itertools.permutations(P)))
print(abs(p.index(tuple(P)) - p.index(tuple(Q))))

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

from itertools import groupby, accumulate, product, permutations, combinations

def solve(P,Q):
    if P==Q:
        return 0
    ans = []
    for i,p in enumerate(permutations(range(1,N+1),N)):
        if list(p)==P or list(p)==Q:
            ans.append(i)
    ans = ans[1]-ans[0]
    return ans
print(solve(P,Q))

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

a=0
b=0
k=0
for j in itertools.permutations(L):
    
    if list(j)==P:
        a=k
    if list(j)==Q:
        b=k
    k+=1

print(abs(a-b))

Solution (Python):
import itertools

count = int(input())

l1 = tuple(map(int, input().split()))
l2 = tuple(map(int, input().split()))


diff = []
for i in range(count):
    diff.append(i + 1)
data = itertools.permutations(diff)

q1, q2 = 1, 1
ans = 0
for i in data:
    if i == l1:
        ans += q1
    else:
        q1 += 1
    
    if i == l2:
        ans -= q2
    else:
        q2+= 1
    
print(abs(ans))

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

p = list(map(int, input().split()))
q = list(map(int, input().split()))
nums = []
for i in range(n):
    nums.append(i + 1)
p_index, q_index = 0, 0
data = itertools.permutations(nums)
index = 0
for i in data:
    
    if list(i) == p:
        p_index = index
    if list(i) == q:
        q_index = index
    index += 1
    
print(abs(p_index - q_index))

Solution (Python):

import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip() 


def getOrder(a: list, n):
    numbers = [i for i in range(1,n+1)]
    
    factorial = 1
    for i in range(1,n):
        factorial *= i

    ret = 0
    for i in range(n):
        ai = a[i]
        idx = numbers.index(a[i])
        ret += idx * factorial
        numbers.pop(idx)
        factorial //= max(1,(n-1-i))
    return ret


def main():
    n = ni()
    ps = na()
    qs = na()
    print(abs(getOrder(qs,n) - getOrder(ps,n)))
    return


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
n = int(input())
p_li = int("".join(input().split()))
q_li = int("".join(input().split()))

all_list=itertools.permutations(range(1,n+1))
cnt_p,cnt_q=0,0

for i in all_list:
    i_li = int("".join(map(str, i)))
    if p_li > i_li:
        cnt_p += 1
    if q_li > i_li:
        cnt_q += 1
    

print(abs(cnt_p-cnt_q))

Solution (Python):
from itertools import permutations
 
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
 
def count(x):
    i = 0
    for perm in permutations(range(1, n+1)):
        if perm == x:
            return i
        i += 1
 
print(abs(count(p) - count(q)))

Solution (Python):
import itertools
n = int(input())
s = list(map(int,input().split()))
t = list(map(int,input().split()))
ls=list(itertools.permutations(range(1,n+1)))
for i in range(len(ls)):
    a = map(str,ls[i])
    ls[i] = "".join(a)
a = "".join(map(str,s))
b = "".join(map(str,t))
print(abs(ls.index(b)-ls.index(a)))

Solution (Python):
from math import factorial
import copy
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Mp = copy.copy(P)
Mq = copy.copy(Q)
Lp = 0
Lq = 0
for i in range(N):
  if P[i] == min(Mp) and i != N:
    Mp.remove(P[i])
  else:
    Lp += factorial(len(Mp)-1)*(sum(x<P[i] for x in Mp))
    Mp.remove(P[i]) 
  if Q[i] == min(Mq) and i != N:
    Mq.remove(Q[i])
  else:
    Lq += factorial(len(Mq)-1)*(sum(x<Q[i] for x in Mq))
    Mq.remove(Q[i])
print(abs(Lp-Lq))

Solution (Python):
import math
import copy
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
Mp = copy.copy(P)
Mq = copy.copy(Q)
Lp = 0
Lq = 0
for i in range(N):
  if P[i] == min(Mp) and i != N:
    Mp.remove(P[i])
  else:
    Lp += math.factorial(len(Mp)-1)*(sum(x<P[i] for x in Mp))
    Mp.remove(P[i]) 
  if Q[i] == min(Mq) and i != N:
    Mq.remove(Q[i])
  else:
    Lq += math.factorial(len(Mq)-1)*(sum(x<Q[i] for x in Mq))
    Mq.remove(Q[i])
print(abs(Lp-Lq))

Solution (Python):
import itertools

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

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

cnt_1=0
cnt_2=0
k=0

for i in itertools.permutations(a):
    if list(i)==p:
        cnt_1=k
    if list(i)==q:
        cnt_2=k
    k+=1

print(abs(cnt_1-cnt_2))

Solution (Python):
import numpy as np
n = int(input())
p = np.array(input().split(),dtype=int)
q = np.array(input().split(),dtype=int)
kaijo = [1,1,2,6,24,120,720,5040]
pn = 0
qn = 0
ans = 0
for i in range(n-1):
    pi = p[i]-1
    qi = q[i]-1
    for j in range(i):
        if p[i] > p[j]:
            pi -= 1
    for j in range(i):
        if q[i] > q[j]:
            qi -= 1
    pn += pi*kaijo[n-i-1]
    qn += qi*kaijo[n-i-1]

print(abs(pn-qn))

Solution (Python):
import itertools

n = int(input())
p = [int(x)-1 for x in input().split()]
q = [int(x)-1 for x in input().split()]

L = list(itertools.permutations(range(n)))

P = L.index(tuple(p))
Q = L.index(tuple(q))
print(abs(P-Q))

Solution (Python):
import math
import copy
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
P.append(N+1)
Q.append(N+1)
Mp = copy.copy(P)
Mq = copy.copy(Q)
Lp = 0
Lq = 0
for i in range(N):
  if P[i] == min(Mp) and i != N-1:
    Mp.remove(P[i])
  elif i == N-1:
    Lp += 1
  else:
    Lp += math.factorial(len(Mp)-2)*(sum(x<P[i] for x in Mp))
    Mp.remove(P[i]) 
for i in range(N):
  if Q[i] == min(Mq) and i != N-1:
    Mq.remove(Q[i])
  elif i == N-1:
    Lq += 1
  else:
    Lq += math.factorial(len(Mq)-2)*(sum(x<Q[i] for x in Mq))
    Mq.remove(Q[i])
print(abs(Lp-Lq))

Solution (Python):
def kai(a):
    x=a
    for i in range(a-1,1,-1):
        x*=i
    return x

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

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

cnt_1=0
cnt_2=0

for i in range(n-2):
    cnt_1+=kai(n-i-1)*(a.index(p[i]))
    cnt_2+=kai(n-i-1)*(b.index(q[i]))
    a.remove(p[i])
    b.remove(q[i])

if p[-2]>p[-1]:
    cnt_1+=1

if q[-2]>q[-1]:
    cnt_2+=1

print(abs(cnt_1-cnt_2))

Solution (Python):
import itertools
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
data = list(range(1,n+1))
x = list(itertools.permutations(data, n))
c1 = 0
c2 = 0
for i in range(len(x)):
    if list(x[i]) == a:
        c1 = i
    if list(x[i]) == b:
        c2 = i
print(abs(c1-c2))

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools








N = int(input())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
ans = 0
a = 0
b = 0
ac = 0
bc = 0
NL = [0]*N
for i in range(N):
    NL[i] = i+1

N_list = list(itertools.permutations(NL, N))

for i in range(math.factorial(N)):
    for j in range(N):
        if(N_list[i][j]==P[j]):
            ac += 1
        if(N_list[i][j]==Q[j]):
            bc += 1
    if(ac == N):
        a = i+1
    if(bc == N):
        b = i+1
    ac = bc = 0

ans = abs(a-b)


print(ans)

Solution (Python):
import itertools

def main():
    n = int(input())
    nums = []
    for i in range(2):
        nums.append(list(map(str, input().split())))
    kumi = list(itertools.permutations([str(i) for i in range(1, n+1)], n))
    f, t = 0, 0
    for i, s in enumerate(kumi):
        if ''.join(nums[0]) == ''.join(s):
            f = i
        if ''.join(nums[1]) == ''.join(s):
            t = i
    print(abs(f - t))

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 i in range(1,N+1):
    fact[i] = fact[i-1] *i
a = 1
b = 1
P2 = sorted(P)
Q2 = sorted(Q)
for i,(p,q) in enumerate(zip(P,Q)):
    a += P2.index(p)*fact[N-i-1]
    b += Q2.index(q)*fact[N-i-1]
    P2.remove(p)
    Q2.remove(q)
print(abs(b-a))

Solution (Python):
import itertools
N = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
a = 0
b = 0
s = 1
for i in ite:
    if i == p:
        a = s
    if i == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

Solution (Python):

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

a = []
for i in range(1, n+1):
    a.append(i)
a = list(itertools.permutations(a))
b = 0
c = 0
for i, v in enumerate(a):
    if list(v) == P:
        b = i
    if list(v) == Q:
        c = i

print(abs(b-c))

Solution (Python):
import itertools
s = 1
a = 0
b = 0
N = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == p:
        a = s
    s += 1
s = 1
ite = [x for x in range(1, N + 1)]
ite = itertools.permutations(ite)
for i in ite:
    if list(i) == q:
        b = s
    s += 1
if a > b:
    total = a - b
else:
    total = b - a
print(total)

Solution (Python):
import itertools

N = int(input())
P_list = list(map(int, input().split()))
Q_list = list(map(int, input().split()))

all_comb_list = []

for i, comb in enumerate(itertools.permutations(range(1, N + 1))):
    comb = list(comb)
    if P_list == comb:
        P_index = i
    if Q_list == comb:
        Q_index = i

ans = abs(P_index - Q_index)

print(ans)

Solution (Python):
from itertools import permutations

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

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

p = permutations.index(P)
q = permutations.index(Q)

print(abs(p-q))

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

p = per.index(P)
q = per.index(Q)

print(abs(p-q))

Solution (Python):
import itertools

n = int(input())
p = tuple(int(i) for i in input().split())
q = tuple(int(i) for i in input().split())

l = sorted(map(tuple,list(itertools.permutations(p))))
count  = []

for num, i in enumerate(l):
  if(i == p or i == q):
    count.append(num + 1)
if(len(count) == 1):
  print(0)
else:
  print(abs(count[0] - count[1]))

Solution (Python):
import math
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
def num(R):
  _R = R
  ans = 0
  for i in range(N):
    if _R[i] != 1:
      ans += (_R[i]-1)*math.factorial(N-i-1)
    if i == N-1:
      ans += _R[-1]
    _R = [_R[j]-1 if R[j]>R[i] else _R[j] for j in range(N)]
    
  return ans
print(abs(num(P)-num(Q)))

Solution (Python):
import itertools

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

st = sorted(p)
x = list(itertools.permutations(st, n))

num1 = x.index(tuple(p))
num2 = x.index(tuple(q))
print(abs(num1 - num2))

Solution (Python):
import itertools
n = int(input())
P = tuple(map(int,input().split()))
Q = tuple(map(int,input().split()))
n = list(range(1,n+1))

flag = 0
for i, p in enumerate(itertools.permutations(n), start=1):
    if p == P:
        a = i
        flag += 1
    if p == Q:
        b = i
        flag += 1
    if flag == 2:
        break
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()))

r=list(permutations(range(1,n+1)))				
																					
print(abs(r.index(p) - r.index(q)))

Solution (Python):
N=int(input())
P=list(map(int, input().split()))
Q=list(map(int, input().split()))
base=sorted(Q.copy())
import itertools
count=1
for L in itertools.permutations(base):
    isp=True
    isq=True
    for i in range(N):
        if L[i]!=P[i]:
            isp=False
        if L[i]!=Q[i]:
            isq=False
    if isp:
        p=count
    
    if isq:
        q=count
    
    count+=1

print(abs(p-q))

Solution (Python):
import itertools

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

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

p_num = permutations.index(P) + 1
q_num = permutations.index(Q) + 1

print(abs(p_num - q_num))

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)))

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

Solution (Python):
import itertools
import math

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

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

a = lists.index(p)
b = lists.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()))

st = sorted(P)
x = list(itertools.permutations(st, N))
num_p = x.index(P)
num_q = x.index(Q)
print(abs(num_p - num_q))