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
cnt = 0
n = int(input())
p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
    
combination = list(itertools.permutations(range(1,n+1)))
combination_list = [list(x) for x in combination]

p_i = combination_list.index(p_list)
q_i = combination_list.index(q_list)


print(abs(p_i - q_i))

Solution (Python):
import itertools

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



n = [int(x) for x in range(1,N+1)]
itr = list(itertools.permutations(n))


for i in range(len(itr)):



    if (list(itr[i]) == P):
        a = i+1
    if (list(itr[i]) == Q):
        b = i+1




ans = abs(a-b)
print(ans)

Solution (Python):
import itertools
def main():
    N = int(input())
    P = input()
    Q = input()
    per = [ "".join(map(str, p)) for p in itertools.permutations(range(1, N+1))]
    print(abs(per.index(P.replace(" ", "")) - per.index(Q.replace(" ", ""))))
if __name__ == "__main__":
    main()

Solution (Python):
from itertools import permutations as perms

n = int(input().strip())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
lis = [(i + 1) for i in range(n)]
a = -1
b = -1
cnt = 0
for elem in perms(lis, n):
    cnt += 1
    if elem == p:
        a = cnt
    if elem == q:
        b = cnt
    if a != -1 and b != -1:
        break
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))
num1 = x.index(p)
num2 = x.index(q)
print(abs(num1 - num2))

Solution (Python):
import sys
input = sys.stdin.readline
ma = lambda : map(int,input().split())
ni = lambda : int(input())
import itertools
n = ni()
l = [i+1 for i in range(n)]
P = list(ma())
Q = list(ma())
cp = 0
cq = 0
c = 0
for per in itertools.permutations(l):
    c+=1
    pf =True
    qf =True
    for i in range(n):
        if per[i] == P[i]:
            pass
        else:pf =False
        if per[i]==Q[i]:
            pass
        else:
            qf=False
    if pf:
        cp=c
    if qf:cq=c
print(abs(cp-cq))

Solution (Python):
import itertools

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

permutation_list = []


for i in range(1, n+1):
    permutation_list.append(int(i))



permutations_pair = list(itertools.permutations(permutation_list))

for index, pair in enumerate(permutations_pair):
    
    pair = list(pair)
    if pair == p:
        index_p = index
    
    if pair == q:
        index_q = index

print(abs(index_p - index_q))

Solution (Python):







from itertools import *

N = int(input())
P = tuple([int(x) for x in input().split()])
Q = tuple([int(x) for x in input().split()])
li = list(permutations(list(range(1, N+1))))

for i in range(len(li)):
    if li[i] == P:
        p = i
    if li[i] == Q:
        q = i

print(abs(p-q))

Solution (Python):
import itertools

N=int(input())
l=[int(x) for x in range(1,N+1)]
P=[int(x) for x in input().rstrip().split()]
Q=[int(x) for x in input().rstrip().split()]

p=itertools.permutations(l,N)
full=[]
for v in p:
  v=list(v)
  full.append(v)

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

Solution (Python):
from itertools import permutations, combinations,combinations_with_replacement,product
import sys
input = sys.stdin.readline

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

dic = list(permutations(p))
dic.sort()

tp = tuple(p)
tq = tuple(q)

print(abs(dic.index(tp)-dic.index(tq)))

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())
data = input().split()
p = [int(s) for s in data]
data = input().split()
q = [int(s) for s in data]
combination = list(itertools.permutations(range(1,n+1)))

combination_list = [list(x) for x in combination]

p_i = combination_list.index(p)
q_i = combination_list.index(q)

ans = abs(p_i - q_i)

print(ans)

Solution (Python):
from itertools import permutations
n = int(input())
p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
dum_list = [i for i in range(1,n+1)]
count = 0
ans = 0
for i in permutations(dum_list,n):
    count += 1
    if p_list == list(i):
        ans += count
    if q_list == list(i):
        ans -= count


print(abs(ans))

Solution (Python):
import math

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

List = [i for i in range(N)]

for i in range(N-1):
    if i == 0:
        countP += (P[i]-1) * math.factorial(N-1)
    elif i == 1:
        if P[0]<P[1]:
            countP += (P[i]-2) * math.factorial(N-2)
        else:
            countP += (P[i]-1) * math.factorial(N-2)
    else:
        count = 0
        for j in range(i):
            if P[i]>P[j]:
                count+=1
        countP += (P[i]-count-1) * math.factorial(N-1-i)

for i in range(N-1):
    if i == 0:
        countQ += (Q[i]-1) * math.factorial(N-1)
    elif i == 1:
        if Q[0]<Q[1]:
            countQ += (Q[i]-2) * math.factorial(N-2)
        else:
            countQ += (Q[i]-1) * math.factorial(N-2)
    else:
        count = 0
        for j in range(i):
            if Q[i]>Q[j]:
                count+=1
        countQ += (Q[i]-count-1) * math.factorial(N-1-i)


if countP > countQ:
    print(countP-countQ)
else:
    print(countQ-countP)

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

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

dict = (list(itertools.permutations(arr)))
print(abs(dict.index(a)-dict.index(b)))

Solution (Python):
from itertools import permutations

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

perm = list(permutations(range(1, N + 1), N))
i = 0
a = 0; b = 0;
for i in range(len(perm)):
    if a > 0 and b > 0: break
    if perm[i] == P:  a = i + 1
    if perm[i] == Q:  b = i + 1

print(abs(b - a))

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

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

p_list = itertools.permutations(L)

a = -1
b = -1

for i, p in enumerate(p_list):
    if P == list(p):
        a = i
    if Q == list(p):
        b = i

print(abs(a-b))

Solution (Python):
from collections import deque

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

st = deque([[i] for i in range(N, 0, -1)])
ord = 0
a = 0
b = 0
while(len(st) > 0):
    if a > 0 and b > 0: break
    now = st.pop()
    if len(now) == N:
        ord += 1
    if P == now:
        a = ord
    if Q == now:
        b = ord
    for i in range(N, 0, -1):
        if i in now: continue
        st.append(now + [i])

print(abs(b - a))

Solution (Python):
import itertools

N = int(input())
p_list = tuple((map(int, input().split())))
q_list = tuple((map(int, input().split())))

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

cnt = 1
for i in itertools.permutations(tmp, N):
    if i == p_list:
        a = cnt

    if i == q_list:
        b = cnt
    cnt += 1

print(abs(a-b))

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

table = list(itertools.permutations([i for i in range(1, n+1)]))
a = table.index(p)
b = table.index(q)

ans = abs(a-b)
print(ans)

Solution (Python):
import itertools
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
N=sorted(P)
c=0
a=0
b=0
for i in itertools.permutations(N):
    c+=1
    if i==P:
        a=c
    if i==Q:
        b=c
print(abs(a-b))

Solution (Python):
import itertools

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

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

L_list = list(itertools.permutations(L))

a = L_list.index(P)
b = L_list.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()))
l = [i+1 for i in range(n)]
r = list(itertools.permutations(l))
j = 0
flagp = True
flagq = True
while flagp:
  if r[j] == p:
    x = j+1
    flagp = False
  j += 1
j = 0
while flagq:
  if r[j] == q:
    y = j+1
    flagq = False
  j += 1
print(abs(y-x))

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

Solution (Python):
import itertools

n = int(input());
x = list(map(int, input().split()));
y = list(map(int, input().split()));

new = sorted(x);
kaijo = list(itertools.permutations(new, n));

x_num = kaijo.index(tuple(x))
y_num = kaijo.index(tuple(y))

print(abs(x_num - y_num));

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

import itertools as it
mylist = []
a = 0
for i in it.permutations(range(1,n+1)):
  a+=1
  mylist.append(i)

ans1 = mylist.index(p)
ans2 = mylist.index(q)
print(abs(ans1-ans2))

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

Solution (Python):

from itertools import permutations as p
N = int(input())
A = [i for i in range(1, N+1)]
P = tuple(list(map(int, input().split())))
Q = tuple(list(map(int, input().split())))
cntp = 1
cntq = 1
for i in sorted(p(A)):
    if i != P:
        cntp += 1
    if i == P:
        cntp += 1
        break
for i in sorted(p(A)):
    if i != Q:
        cntq += 1
    if i == Q:
        cntq += 1
        break
print(abs(cntp-cntq))

Solution (Python):
import itertools

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

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

number = 0
for pattern in patterns:
    number += 1
    if list(pattern) == A:
        tmp1 = number

    if list(pattern) == B:
        tmp2 = number

    
print(abs(tmp1 - tmp2))

Solution (Python):
import math
import itertools
num = int(input())
lis = [ i for i in range(1,num+1)]
k = tuple(map(int,input().split()))
t = tuple(map(int,input().split()))
count_k = 0
count_t = 0
count = 0
for i in itertools.permutations(lis):
    count+=1
    if i == k:
        count_k = count
    if i == t:
        count_t = count
    if count_k != 0 and count_t != 0:
        break
print(abs(count_k -count_t))

Solution (Python):
import itertools

n = int(input())
l = [i for i in range(1, n + 1)]
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
cnt = 0
for check in itertools.permutations(l, n):
    cnt += 1
    if a == check:
        cnta = cnt
    if b == check:
        cntb = cnt

print(abs(cnta - cntb))

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


def place(a):
  R=S.copy()
  count=1
  for i in range(0,N):
    count += (R.index(a[i]))*math.factorial(N-1-i)
    del R[R.index(a[i])]
    
  return(count)

print(abs(place(P)-place(Q)))

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

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

a = [i+1 for i in range(n)]
cnt = 0
cp = 0
cq = 0
for x in itertools.permutations(a):
    cnt += 1
    if list(x) == p: cp = cnt
    if list(x) == q: cq = cnt
    if cq != 0 and cp !=0: break
print(abs(cp - cq))

Solution (Python):
import itertools













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

def calculate(n,p,q):
    x = 0
    y = 0

    for index,ar in enumerate(itertools.permutations([i for i in range(1,n+1)],n)):
        if list(ar) == list(p):
            x = index

        if list(ar) == list(q):
            y = index

    if x >= y:
        print(x-y)
    else:
        print(y-x)
calculate(N,P,Q)

Solution (Python):
import itertools

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

Solution (Python):
import math
import itertools
N = int(input())
P = list(map(int,input().split()))
p = ""
for i in range(N):
  p += str(P[i])
Q = list(map(int,input().split()))
q = ""
for i in range(N):
  q += str(Q[i])

s = ""
for i in range(1,N+1):
  s += str(i)
l = [''.join(v) for v in itertools.permutations(s)]

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

Solution (Python):
import sys
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
P = rl()
Q = rl()

import itertools
a, b = 0, 0
for i, p in enumerate(itertools.permutations(list(range(1,N+1)))):
    if a > 0 and b > 0: break
    if list(p) == P:
        a = i+1
    if list(p) == Q:
        b = i+1
print(abs(a-b))

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

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

def cul(n_, lis_):
  l=[]
  for i in range(1,n+1):
  	l.append(i)
  sum_=0
  for i in range(1,n_):
    lenl=len(l)
    
    
    
    sum_+=l.index(lis_[i-1])/lenl*num_[-i]
    l.remove(lis_[i-1])
    
  return sum_
  
k = abs(cul(n,p)-cul(n,q))
print(int(k))

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

from itertools import permutations 
def main():
    n = int(readline())
    p = tuple([int(i) for i in readline().split()])
    q = tuple([int(i) for i in readline().split()])
    num = [i for i in range(1, n+1)]

    c1 = 0
    c2 = 0
    cnt = 0
    for perm in permutations(num):
        cnt += 1
        if perm == p:
            c1 = cnt
        if perm == q:
            c2 = cnt
    print(abs(c1-c2))   
if __name__ == '__main__':
    main()

Solution (Python):
import itertools

N = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = -1
b = -1

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

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

for i in range(len(permutations)):
    if P == permutations[i]:
        a = i + 1

for j in range(len(permutations)):
    if Q == permutations[j]:
        b = j + 1

print(abs(a-b))

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

from itertools import permutations

for i, p in enumerate(permutations(sorted(P))):
    if p == P:
        a = i

    if p == Q:
        b = i

print(abs(a - b))

Solution (Python):

from itertools import permutations as perm

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

a = 0
b = 0

for Pi in sorted(perm(P)):
    if P != Pi:
        a += 1
    if P == Pi:
        a += 1
        break
    
for Qi in sorted(perm(Q)):
    if Q != Qi:
        b += 1
    if Q == Qi:
        b += 1
        break

print(abs(a-b))

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

cnt_p, cnt_q = 1, 1
p = tuple(P)
q = tuple(Q)
for a in sorted(itertools.permutations(P)):
  if a != p:
    cnt_p += 1
  else:
    break
for b in sorted(itertools.permutations(Q)):
  if b != q:
    cnt_q += 1
  else:
    break
print(abs(cnt_p-cnt_q))

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

L=list(map(list,itertools.permutations(range(1,N+1))))
for a,x in enumerate(L):
    for b,y in enumerate(x):
        L[a][b]=str(y)
L=[int(''.join(x)) for x in L]
L=sorted(L,reverse=False)

print(int(math.fabs((L.index(P)+1)-(L.index(Q)+1))))

Solution (Python):
import itertools

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

P_dict = itertools.permutations(sorted(P), N)
P_dict = [i for i in list(P_dict)]
Q_dict = itertools.permutations(sorted(Q), N)
Q_dict = [i for i in list(Q_dict)]

ans = 0
for i in range(len(P_dict)):
  if list(P_dict[i]) == P:
    ans += (i+1)
  if list(Q_dict[i]) == Q:
    ans -= (i+1)

print(ans if ans >= 0 else -ans)

Solution (Python):
import itertools

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

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

p = itertools.permutations(n, N)

ans1 = 0
ans2 = 0
for i, v in enumerate(p):
    if (P == v):
        ans1 = i + 1
    if (Q == v):
        ans2 = i + 1

print(abs(ans1 - ans2))

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

n_str = ''
for i in range(n):
    n_str += str(i+1)

n_list = []
for order in itertools.permutations(n_str):
    int_str = int(''.join(order))
    n_list.append(int_str)

print(abs(n_list.index(int(''.join(p)))-n_list.index(int(''.join(q)))))

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

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

print(abs(a - b))

Solution (Python):
from collections import deque
import itertools

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

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

D = deque(itertools.permutations(l))

a = 0
b = 0
count = 0
for d in D:
    count += 1
    if d == P:
        a = count
    if d == Q:
        b = count

if a - b < 0:
    print(b - a)
else:
    print(a - b)

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

import math 
def dict_order(l): 
    n = len(l)
    order = 0 
    used = [0]*n
    for i in range(n):
        order += (l[i]-1-sum(used[:l[i]]))*math.factorial(N-1-i)
        used[l[i]-1] += 1
    return order 
print(abs(dict_order(P)-dict_order(Q)))

Solution (Python):
from itertools import permutations as per
n = int(input())
p = tuple(int(i) for i in input().split())
q = tuple(int(i) for i in input().split())

a = b = 0
for i,x in enumerate(per(range(1,n+1)),1):
    if x == p: a = i
    if x == q: b = i
    if a and b: break
print(abs(a-b))

Solution (Python):
import itertools
n =int(input())
p =list(map(str ,input().split()))
q =list(map(str ,input().split()))
l =[]
for i in range(1 ,n+1):
    l.append(str(i))
cnt =1
for v in itertools.permutations(l,n):
    if p == list(v):
        a =cnt
    if q == list(v):
        b =cnt
    cnt +=1
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)))
 
for i, pe in enumerate(perm, start=1):
    if P == pe:
        a = i
    if Q == pe:
        b = 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()))

data = P.copy()
data.sort()

for idx, i in enumerate(list(permutations(data, N))):
    if list(i) == P:
        a = idx
    if list(i) == Q:
        b = idx

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()))
ans=sorted([i for i in permutations(range(1, n + 1))])
print(abs(ans.index(p) - ans.index(q)))

Solution (Python):
import itertools
import numpy as np

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

length = len(p)

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

index_dict = {}
for index, v in enumerate(sorted(itertools.permutations(n_array, length))):
    index += 1
    if list(v) == list(p):
        index_dict[str(p)] = index
    if list(v) == list(q):
        index_dict[str(q)] = index

    if str(q) in index_dict and str(p) in index_dict:
        break

print(np.abs(index_dict[str(p)] - index_dict[str(q)]))

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

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

    d = {i: j for j, i in enumerate(t)}
    print(abs(d[p] - d[q]))

solve()

Solution (Python):
import itertools

N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
X=[i for i in range(1,N+1)]


c = list(itertools.permutations(X,N))

a=0
b=0
for i in range(len(c)):
    if c[i]==P:
        a=i+1
    if c[i]==Q:
        b=i+1

print(abs(a-b))

Solution (Python):
N=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
a=1
b=1
def josu(x):
  if x==1 or x==0:
    return 1
  else:
    return x*josu(x-1)
A=list(range(1,N+1))
B=list(range(1,N+1))
for i in range(N-1):
  if X[i]!=min(A):
    z=A.index(X[i])
    a+=z*josu(N-(i+1))
  A.remove(X[i])
for j in range(N-1):
  if Y[j]!=min(B):
    z=B.index(Y[j])
    b+=z*josu(N-(j+1))
  B.remove(Y[j])
if a-b>=0:
  print(a-b)
else:
  print(b-a)

Solution (Python):
import math
import itertools
n = int(input())
s = tuple(map(int,input().split()))
t = tuple(map(int,input().split()))

ari = [i for i in range(1,n+1)]
l = list(itertools.permutations(ari))
for i in range(math.factorial(n)):
    if s == l[i]:
        a = i
    if t == l[i]:
        b = i
if a >= b:
    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_p = 1
for case in permutations(range(1, n+1)):
    if p == list(case):
        p_n = cnt_p
    cnt_p += 1

cnt_q = 1
for case in permutations(range(1, n+1)):
    if q == list(case):
        q_n = cnt_q
    cnt_q += 1
print(abs(p_n - q_n))

Solution (Python):
from itertools import permutations

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

cnt_p = 1
for case in permutations(range(1, n+1)):
    if p == list(case):
        p_n = cnt_p
    cnt_p += 1

cnt_q = 1
for case in permutations(range(1, n+1)):
    if q == list(case):
        q_n = cnt_q
    cnt_q += 1
print(abs(p_n - q_n))

Solution (Python):
from itertools import permutations as perm
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)


args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v, yes="Yes", no="No", upper=False):
    if upper:
        print([yes.upper(), no.upper()][not v])
    else:
        print([yes, no][not v])


def main():
    """エントリーポイント"""
    N = input(int)
    P = input([int, N])
    Q = input([int, N])
    p, q = tuple(P), tuple(Q)

    base = [x for x in range(1, N + 1)]
    base_list = sorted(perm(base))
    pn, qn = -1, -1
    for c, x in enumerate(base_list):
        if x == p:
            pn = c
        if x == q:
            qn = c
    print(abs(pn - qn))


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

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

a = [i+1 for i in range(n)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print(abs(x-y))

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

a = [i+1 for i in range(n)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print(abs(x-y))

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

a = [i+1 for i in range(n)]
for i,per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print(abs(x-y))

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

















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

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

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

cnt = 1
for v in itertools.permutations(l, n):
    if p == list(v):
        a = cnt

    if q == list(v):
        b = cnt
    cnt = cnt + 1

print(abs(a-b))

Solution (Python):
from itertools import permutations
n=int(input())
v=list(permutations(range(1,n+1),n))
print(abs(v.index(tuple(map(int,input().split())))-v.index(tuple(map(int,input().split())))))

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

ans = sorted([i for i in permutations(range(1, n + 1))])
print(abs(ans.index(p) - ans.index(q)))

Solution (Python):
import math
N = int(input())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
X_ans = 0
Y_ans = 0
used_numX = list()
used_numY =list()
for i in range(N-1):
    if(i == N-2):
        if(X[i]>X[i+1]):
            X_ans +=2
        else:
            X_ans +=1
        if(Y[i]>Y[i+1]):
            Y_ans +=2
        else:
            Y_ans +=1


    else:
        used_x = used_y =0
        for j in range(len(used_numX)):
            if(used_numX[j]<=X[i]):
                used_x +=1
            if(used_numY[j]<=Y[i]):
                used_y +=1
    
        X_ans += math.factorial(N-1-i)*(X[i]-1-used_x)
        Y_ans += math.factorial(N-1-i)*(Y[i]-1-used_y)
        used_numX.append(X[i])
        used_numY.append(Y[i])

print(abs(X_ans-Y_ans))

Solution (Python):
def factorial(n):
    res = 1
    for i in range(n):
        res*=(i+1)
    return res

def order(n, li):
    res=0
    for i in range(n):
        j=sorted(li[n-i-1:n]).index(li[n-i-1])
        res+=factorial(i)*j
    return res

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

p_ord=order(n,p)
q_ord=order(n,q)

print(abs(p_ord-q_ord))

Solution (Python):
import sys
import itertools

readline = sys.stdin.readline

def main():
    N = int(input())
    P = tuple(map(int, readline().split()))
    Q = tuple(map(int, readline().split()))
    
    dictN = list(itertools.permutations(range(1, N+1)))
    p = dictN.index(P)
    q = dictN.index(Q)
    ans = abs(p-q)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import itertools

n = int(input())
perm_a = [int(x) for x in input().split()]
perm_b = [int(x) for x in input().split()]
perm_a = tuple(perm_a)
perm_b = tuple(perm_b)


ind_a = -1
ind_b = -1
for i, perm in enumerate(itertools.permutations(range(1, n+1))):
    if perm == perm_a:
        ind_a = i
    if perm == perm_b:
        ind_b = i

print(abs(ind_b - ind_a))

Solution (Python):
import sys
import itertools
input = sys.stdin.readline
n=int(input())
p=list(input().split())
q=list(input().split())
a=0

for i in range(1,n+1):
    a=a*10+i
b=list(itertools.permutations(str(a),n))
d=b.index(tuple(q))
e=b.index(tuple(p))
c=abs(d-e)
print(c)

Solution (Python):
from itertools import permutations
n = int(input())
v = list(permutations(range(1,n+1),n))
print(abs(v.index(tuple(map(int,input().split())))-v.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()))

tmp=list()
for i in range(n):
    tmp.append(i+1)
perm=list(itertools.permutations(tmp))

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=[list(i) for i in itertools.permutations(range(1,n+1))]
print(abs(r.index(p)-r.index(q)))

Solution (Python):
def kaijo(x):
    if(x <= 2):
        return x
    else:
        return x*kaijo(x-1)

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

count_p = 0
count_q = 0
for i in range(n):
    count_p = count_p + (p[i]-1) * kaijo(n-i-1)
    for j in range(i+1,n):
        if(p[i] < p[j]):
            p[j] = p[j] - 1
    count_q = count_q + (q[i]-1) * kaijo(n-i-1)
    for j in range(i+1,n):
        if(q[i] < q[j]):
            q[j] = q[j] - 1

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

a = [i+1 for i in range(n)]
for i, per in enumerate(list(permutations(a))):
    if per == p:
        x = i
    if per == q:
        y = i
print(abs(x-y))