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

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

l = list(int(i) for i in range(1,n+1))
test = []
cnt = 0
for i in itertools.permutations(l,n):
	i = list(i)
	cnt += 1
	if p == i:
		cntp = cnt
	if q == i:
		cntq = cnt

print(abs(cntp - cntq))

Solution (Python):
import itertools

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

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

a = 0
b = 0
i = 1

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

print(abs(a-b))

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

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

a = 0
b = 0
for i in range(len(ans)) :
  if ans[i] == p:
    a = i+1
  if ans[i]== q:
    b = i+1
      
print(abs(a-b))

Solution (Python):
import itertools

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

a = 0
b = 0

for i, x in enumerate(itertools.permutations(range(1, N+1))):
    if x == P:
        a = i
    if x == Q:
        b = 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()))

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

print (abs(a-b))

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def SS(): return map(str,sys.stdin.readline().rstrip().split())
def II(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N=I()
P=LS()
Q=LS()

l = ["".join([str(n) for n in list(i)]) for i in itertools.permutations(range(1,N+1),N)]

ans = abs(l.index("".join(P)) - l.index("".join(Q)))
print(ans)

Solution (Python):
from collections import deque
n = int(input())
P = int(''.join(input().split()))
Q = int(''.join(input().split()))


def dfs(s,tmp):
    
    if len(s)>n+1 or int(s)>max(P,Q):
        return tmp
    
    if len(s)==n+1:
        tmp+=1
        if int(s)==P:
            global a
            a = tmp
        if int(s)==Q:
            global b 
            b = tmp
        return tmp
    for c in [str(i) for i in range(1,n+1)]:
        if c not in s:
            tmp = dfs(s+c,tmp)
    return tmp

tmp = 0
a,b = 0,0
dfs('0',tmp)
print(abs(a-b))

Solution (Python):
from collections import deque
n = int(input())
P = int(''.join(input().split()))
Q = int(''.join(input().split()))

def dfs(s,ans):
    if len(s)>n+1 or int(s)>max(P,Q):
        return ans
    if len(s)==n+1:
        ans+=1
        if int(s)==P:
            global a
            a = ans
        if int(s)==Q:
            global b 
            b = ans
        return ans
    for c in [str(i) for i in range(1,n+1)]:
        if c not in s:
            ans = dfs(s+c,ans)
    return ans

ans = 0
a,b = 0,0
dfs('0',ans)
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 = []
for pp in permutations(range(1,n+1)):
	l.append(list(pp))

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

Solution (Python):
import sys
from math import pi, cos, sqrt
from collections import defaultdict
readline = sys.stdin.buffer.readline

from itertools import permutations


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n = gete(int)
    p = "".join(list(geta()))
    q = "".join(list(geta()))

    base = "".join(map(str, range(1, n + 1)))
    for i, it in enumerate(permutations(base)):
        if p == "".join(it):
            a = i

        if q == "".join(it):
            b = i
    print(abs(b - a))


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

all_combination = permutations(range(1, N + 1), N)
dict_order = {}
for i, c in enumerate(all_combination):
    dict_order[c] = i
print(abs(dict_order[P] - dict_order[Q]))

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

l=[ i+1 for i in range(N) ]
Per=itertools.permutations(l,N)
Per=list(Per)
num=[-1,-1]
for i in range(math.factorial(N)):
  if Per[i]==P:
    num[0]=i
  if Per[i]==Q:
    num[1]=i
print(abs(num[0]-num[1]))

Solution (Python):
from itertools import permutations
from collections import defaultdict

N = int(input())

P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
res = 0
loop = 0

d = defaultdict(int)

for perm in permutations(range(1,N+1)):
    d[perm] = loop
    loop += 1

res = abs(d[P] - d[Q])

print(res)

Solution (Python):
from itertools import permutations
n=int(input())
p=tuple(map(lambda x:int(x)-1,input().split()))
q=tuple(map(lambda x:int(x)-1,input().split()))
perms=list(permutations(range(n)))
for i in range(len(perms)):
    if perms[i]==p:
        pidx=i
    if perms[i]==q:
        qidx=i
print(abs(pidx-qidx))

Solution (Python):
import itertools
N=int(input())
P=list(map(int,input().split()))
Q=list(map(int,input().split()))
a=list(itertools.permutations(range(1,N+1)))
ans=abs(a.index(tuple(P))-a.index(tuple(Q)))
print(ans)

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

Solution (Python):
from itertools import permutations
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
a=list(permutations(range(1,n+1)))
A=a.index(p)
B=a.index(q)
ans=abs(A-B)
print(ans)

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

from collections import deque
tuples=[]
def f(que):
    if len(que)==n:
        tuples.append(tuple(que))
        return
    
    for v in range(1,n+1):
        if v not in que:
            que.append(v)
            f(que)
            que.pop()
f(deque([]))



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

Solution (Python):

import sys
import itertools

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    N = int(readline())
    P = ''.join(readline().decode().split()).rstrip()
    Q = ''.join(readline().decode().split()).rstrip()
    str_num = ''.join([str(i) for i in range(1, N+1)])
    all_list = list(itertools.permutations(list(str_num), N))
    all_list = [''.join(a) for a in all_list]
    all_list.sort()
    a = all_list.index(P)
    b = all_list.index(Q)
    print(abs(a - b))


if __name__ == '__main__':
    main()

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

a = 0
b = 0
for i, x in  enumerate(itertools.permutations(range(1, N + 1))):
    
    if x == p:
        a = i
    if x == q:
        b = i
print(abs(a - b))

Solution (Python):
import sys
from itertools import permutations

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


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

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


if __name__ == '__main__':
    main()

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

def nextPermutations(X):
  buf = X[-1]
  for i in range(N):
    if X[N-i-1]<buf:
      break
    buf = X[N-i-1]
  pov = N-i-1
  v = X[pov]
  mina = 8
  answer = 0
  for i in range(pov,N):
    if X[i]>v and X[i]<=mina:
      answer = i
      mina = X[i]
  X[pov],X[answer] = X[answer],X[pov]
  X = X[:pov+1]+sorted(X[pov+1:])
  return X
  
if P[::]==Q[::]:
  print(0)
else:
  for i in range(N):
    if P[i]<Q[i]:
      min = P
      max = Q
      break
    if Q[i]<P[i]:
      min = Q
      max = P
      break
  count = 0
  while min[::]!=max[::]:
    min = nextPermutations(min[::])
    count+=1
  print(count)

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

from itertools import permutations

zembu=list(permutations(range(1,N+1)))

a=b=-1
for i in range(len(zembu)):
  if zembu[i]==P:
    a=i
  
  if zembu[i]==Q:
    b=i
  
  if a!=-1 and b!=-1:
    print(abs(a-b))
    break

Solution (Python):
import itertools
n=int(input())
ap=0
aq=0
p=list(map(int,input().split()))
q=list(map(int,input().split()))
list1=list(itertools.permutations(list(range(1,n+1))))
list1.sort()
for i,w in enumerate(list1):
    v=list(w)
    if p==v:
        ap=i+1
    if q==v:
        aq=i+1
print(abs(ap-aq))

Solution (Python):
import itertools

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

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

Solution (Python):
import itertools

N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
num_p = 0
num_q = 0
num = [0]*N

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

seq = list(itertools.permutations(num))

for j in range(len(seq)):
  if list(seq[j]) == P:
    num_p = j+1
  if list(seq[j]) == Q:
    num_q = j+1
    
print(abs(num_p - num_q))

Solution (Python):
import itertools
N = int(input())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
X = list(itertools.permutations(range(1, N+1)))
a = X.index(A)
b = X.index(B)
print(abs(a-b))

Solution (Python):
import math
import numpy as np

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

num_P, num_Q = 0, 0
for i in range(N):
  val = P[0]
  tmp_P = sorted(P)
  idx_P = tmp_P.index(val)
  del P[0]
  if i == N-1:
    num_P += 1
  else:
    num_P += idx_P * math.factorial(N-(i+1))	
    
for i in range(N):
  val = Q[0]
  tmp_Q = sorted(Q)
  idx_Q = tmp_Q.index(val)
  del Q[0]
  if i == N-1:
    num_Q += 1
  else:
    num_Q += idx_Q * math.factorial(N-(i+1))

print(abs(num_P-num_Q))

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

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

Solution (Python):
N = int(input())
def kaijou(x):
    h = 1
    for i in range(1,x+1):
        h *= i
    return h
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
R = [0 for _ in range(N)]
S = [0 for _ in range(N)]
for i in range(N):
    R[P[i]-1] = 1
    s = P[i]-1-sum(R[:(P[i]-1)])
    a += s*kaijou(N-1-i)
for j in range(N):
    S[Q[j]-1] = 1
    t = Q[j]-1-sum(S[:(Q[j]-1)])
    b += t*kaijou(N-1-j)
print(abs(a-b))

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

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

for l in pm([i+1 for i in range(n)],n):
    if l == p:
        a = i
    if l == q:
        b = i
    i += 1
print(abs(a-b))

Solution (Python):

import itertools
N = int(input())
LIST = [str(n) for n in range(1, N+1)]
LIST = list(itertools.permutations(LIST, N))

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


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

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

n = int(input())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
c = list(itertools.permutations(p,n))
c.sort()
for i in range(len(c)):
    if list(c[i]) == p:
        one = i
    if list(c[i]) == q:
        two = i
print(abs(one-two))

Solution (Python):
import itertools

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

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

def turn_num(list_num, arr, m):
    for num ,v in enumerate(itertools.permutations(list_num, m)):
        if arr == list(v):
            res = num + 1
            break
    return res

ans = abs(turn_num(list_, p, n) - turn_num(list_, q, n))

print(ans)

Solution (Python):
import itertools

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

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

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        break
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1
        break
ans = abs(res_p - res_q)

print(ans)

Solution (Python):
import itertools

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

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

for num ,v in enumerate(itertools.permutations(list_, n)):
    if p == list(v):
        res_p = num + 1
        
for num ,v in enumerate(itertools.permutations(list_, n)):
    if q == list(v):
        res_q = num + 1

ans = abs(res_p - res_q)

print(ans)

Solution (Python):
import itertools as iter

def main():
    n = int(input())
    p = tuple(map(int, input().split()))
    q = tuple(map(int, input().split()))
    l = list(iter.permutations(range(1,n+1)))
    print(abs(l.index(p)-l.index(q)))

if __name__ == '__main__':
    main()

Solution (Python):
import itertools as it
n = int(input())
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
A = (range(1, n + 1))
V = list(it.permutations(A))
print(abs(V.index(P) - V.index(Q)))

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

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

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

P = tuple(P)


permuts = list(permutations(copy))

p = 0
q = 0

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

print(abs(p - q))

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

for j in range(len(all)):
    if all[j] == P:
        ans1 = j
    if all[j] == Q:
        ans2 = j
print(abs(ans1 - ans2))

Solution (Python):
from itertools import permutations

N=int(input())
A=tuple(map(int,input().split()))
B=tuple(map(int,input().split()))
a=[i for i in range(1,N+1)]
tmp=list(permutations(a))

x=0
y=0

for i in range(len(tmp)):
    if tmp[i]==A:
        x=i
        break
for i in range(len(tmp)):
    if tmp[i]==B:
        y=i
        break
print(abs(x-y))

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

ls = [i+1 for i in range(N)]
for i, p in enumerate(itertools.permutations(ls, N)):
    if p == P:
        a = i
    if p == Q:
        b = i

print(abs(a-b))

Solution (Python):
import itertools

n = int(input())
keep = []
p = tuple((map(int, input().split())))
q = tuple((map(int, input().split())))
for i in range(1, n + 1):
    keep.append(i)
all = list(itertools.permutations(keep))
for j in range(len(all)):
    if all[j] == p:
        ans1 = j
    if all[j] == q:
        ans2 = j
print(abs(ans1 - ans2))

Solution (Python):
import itertools

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

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

Pi = lst.index(P)
Qi = lst.index(Q)
print(abs(Pi-Qi))

Solution (Python):

import itertools
n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
l = list(itertools.permutations(range(1,n+1)))
print(abs(l.index(p) - l.index(q)))

Solution (Python):
import itertools

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


for i, v in enumerate(itertools.permutations(range(1,N+1))):
    if list(v) == P_list:
        a = i + 1
    if list(v) == Q_list:
        b = i + 1

print(abs(a-b))

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

m=list(itertools.permutations(p))
m.sort()

p=tuple(p)
q=tuple(q)
a=m.index(p)
b=m.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())) 
seq = [0]*n
for i in range(1,n+1):
    seq[i-1] = i
nnn = list(itertools.permutations(seq, n))
print(abs(nnn.index(q) - nnn.index(p)))

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

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

import itertools
itertools.permutations(P)
l=list(itertools.permutations(P))
l.sort()
P=tuple(P)
Q=tuple(Q)

p=l.index(P)
q=l.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()))
dict=sorted(permutations(range(1,N+1)))
print(abs(dict.index(P)-dict.index(Q)))

Solution (Python):
import sys
input = sys.stdin.readline
import bisect
from collections import deque


def read():
    N = int(input().strip())
    P = list(map(int, input().strip().split()))
    Q = list(map(int, input().strip().split()))
    return N, P, Q


def next_permutation(a):
    l = -1
    for i in range(len(a)-1):
        if a[i] < a[i+1]:
            l = i
    if l == -1:
        return False
    for j in range(len(a)-1, -1, -1):
        if a[l] < a[j]:
            break
    tmp = a[l]
    a[l] = a[j]
    a[j] = tmp
    j = l + 1
    k = len(a) - 1
    while j < k:
        tmp = a[k]
        a[k] = a[j]
        a[j] = tmp
        j += 1
        k -= 1
    return True


def solve(N, P, Q):
    a, b = 0, 0
    while next_permutation(P):
        a += 1
    while next_permutation(Q):
        b += 1
    return abs(b - a)
            
    
if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))

Solution (Python):
from itertools import permutations

n = int(input())
p_list = list(map(int,input().split()))
q_list = list(map(int,input().split()))
p = tuple(p_list)
q = tuple(q_list)
l = list(range(1,n+1))
s = list(permutations(l))
ans = abs(s.index(p)-s.index(q))
print(ans)

Solution (Python):

import itertools
n = int(input())
p = tuple(map(int,input().split()))
q = tuple(map(int,input().split()))
ls = list(itertools.permutations(range(1,n+1)))
print(abs(ls.index(p)-ls.index(q)))

Solution (Python):

import itertools
n = int(input())
ls = [i+1 for i in range(n)]
p = list(map(int,input().split()))
q = list(map(int,input().split()))
v = [ list(v) for v in itertools.permutations(ls, n)]
a, b = v.index(p), v.index(q)
print(abs(a-b))

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
numP = 1
numQ = 1
for i in range(N-1):
    cou = 1
    time = P[i]-1
    for j in range(1,N-i):
        cou = cou*j
    for j in range(0,i):
        if P[j]< P[i]:
            time -= 1
    numP += time*cou
for i in range(N-1):
    cou = 1
    time = Q[i]-1
    for j in range(1,N-i):
        cou = cou*j
    for j in range(0,i):
        if Q[j]< Q[i]:
            time -= 1
    numQ += time*cou
print(abs(numP-numQ))

Solution (Python):
import itertools
import math
n=int(input())
p=tuple(map(int,input().split()))
q=tuple(map(int,input().split()))
dictionary=list(itertools.permutations(range(1,n+1)))
P=dictionary.index(p)
Q=dictionary.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()))
l = list(permutations(range(1,n+1)))
print(abs(l.index(p)-l.index(q)))

Solution (Python):
import itertools
N=int(input())
candi=[str(i) for i in range(1,N+1)]
P="".join(input().split())
Q="".join(input().split())
count=0

for j in itertools.permutations(candi,N):
  k="".join(list(j))
  count+=1
  if k==P:
    a=count
  if k==Q:
    b=count
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()))
l = list(permutations(range(1,n+1)))
print(abs(l.index(p)-l.index(q)))

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

Solution (Python):
import math 
n = int(input())
lp = list(map(int,input().split()))
lq = list(map(int,input().split()))
def fun(l):
    a = 1 
    count = 1
    li = []
    for i in range(n):
        ika = 0
        for j in li:
            if j < l[i]:
                ika += 1
        if l[i] == i+1 and ika == l[i]:
                c = 0
                a = l[i]
        else:
                c = (l[i]-ika-1)*math.factorial(n-i-1)
                a = l[i]
            
        count += c
        li.append(l[i])
    return count
   

print(abs(fun(lp)-fun(lq)))

Solution (Python):
n=int(input())
p=input()
q=input()

from itertools import permutations as per

i=0
for nn in per(map(str,range(1,n+1))):
    i+=1
    st=' '.join(nn)
    if p==st:a=i
    if q==st:b=i

print(abs(b-a))

Solution (Python):
import itertools

N = int(input())
num_list = [list(map(int, input().split())) for _ in range(2)]

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

ans = abs(all_list.index(tuple(num_list[0])) - all_list.index(tuple(num_list[1])))
print(ans)

Solution (Python):
import itertools
N=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
lis=list(itertools.permutations(list(range(1,N+1))))
tmp1=0
tmp2=0
a=0
b=0
for v in lis:
    tmp=list(v)
    if tmp==p:
        a=tmp1+1
    if tmp==q:
        b=tmp2+1
    tmp1+=1
    tmp2+=1
print(abs(a-b))

Solution (Python):
import itertools


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

l=[i for i in range(1,N+1)]
p =list(itertools.permutations(l, N))
P_n=p.index(P)
Q_n=p.index(Q)
print(abs(P_n-Q_n))

Solution (Python):


import itertools














def main():

    N = int(input())
    P = tuple(map(int, input().split()))
    Q = tuple(map(int, input().split()))
    comb = list(itertools.permutations(range(1, N + 1), N))

    a, b = -1, -1
    for i, c in enumerate(comb):
        if c == P:
            a = i
        if c == Q:
            b = i

    print(abs(a - b))


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import product, permutations,combinations
def main():
    N = int(input())
    P = input()
    Q = input()
    Nlis = list(range(1,N+1))
    for i in range(len(Nlis)):
        Nlis[i] = str(Nlis[i])

    lis = []
    for i in permutations(Nlis):
        lis.append(' '.join(i))
    sorted(lis)

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


if __name__ == '__main__':
    main()

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

pl=list(map(str,input().split()))
ql=list(map(str,input().split()))


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

import itertools
p=("".join(pl))
q="".join(ql)

per=[]

for tup in itertools.permutations(l):
    per.append("".join(list(tup)))

per.sort()

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

Solution (Python):
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
a = 0
b = 0
A = [0] * (N - 1)
A[N - 2] = 1
for i in range(N):
    for j in range(i,N):
        if P[j] > P[i]:
            P[j] -= 1
    for j in range(i,N):
        if Q[j] > Q[i]:
            Q[j] -= 1
for i in range(1,N -1):
    A[N - 2 - i] = (i + 1)* A[N - 2 - i + 1]
for i in range(N -1):
    a += P[i]*A[i]
    b += Q[i]*A[i]
print(abs(a-b))

Solution (Python):
import itertools

n = int(input().strip())
p = int(''.join(input().strip().split()))
q = int(''.join(input().strip().split()))

cnt_l = 0
cnt_r = 0
for it in itertools.permutations(list(map(str,range(1, n+1)))):
  tmp = int(''.join(it))
  if tmp<=p:
    cnt_l += 1
  if tmp<=q:
    cnt_r += 1
print(abs(cnt_r-cnt_l))

Solution (Python):
import itertools

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

seq = [i for i in range(1,N+1)]
perm = [int(''.join(map(str,p))) for p in itertools.permutations(seq)]
perm.sort()

a = perm.index(P) + 1
b = perm.index(Q) + 1
print(abs(a-b))

Solution (Python):
n = int(input())
p = int(input().replace(" ",""))
q = int(input().replace(" ",""))
dic = []

import itertools
n_dic = range(1,n+1)
for v in itertools.permutations(n_dic, n):
    a = ""
    for i in range(n):
        a += str(v[i])
    dic.append(int(a))

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

Solution (Python):
import itertools

n=int(input())
s=tuple(map(int,input().split()))
t=tuple(map(int,input().split()))

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

num1=(y.index(s))
num2=(y.index(t))

print(abs(num1-num2))

Solution (Python):
import itertools
N = int(input())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
a = 0
b = 0
cnt = 0
for n in itertools.permutations(range(1, N+1)):
    if P == list(n):
        a = cnt
    if Q == list(n):
        b = cnt
    cnt += 1
total = abs(a-b)
print(total)

Solution (Python):
import collections
n = int(raw_input())
p,q = map(int,raw_input().split(' ')), map(int,raw_input().split(' '))
res = []

def perms(i, elems,res):

	if i == len(elems):
		res.append(tuple(elems)) 
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, range(1, n+1), res)
tp,tq = tuple(p),tuple(q)
res.sort()
for i in range(len(res)):
	if res[i] == tp: ii = i
	if res[i] == tq: jj = i

print abs(jj - ii)

Solution (Python):
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))


elems = range(1, n+1)
res = []
c = [0]
def perms(i, elems,res):

	if i == len(elems):
		res.append(tuple(elems)) 
		return 
	for j in range(i, len(elems)):
		elems[j], elems[i] = elems[i], elems[j]
		perms(i+1, elems, res)
		elems[j], elems[i] = elems[i], elems[j]

perms(0, elems, res)

tp = tuple(p)
tq = tuple(q)
res.sort()
for i in range(len(res)):
	if res[i] == tp:
		ii = i

	if res[i] == tq:
		jj = i

print abs(jj - ii)

Solution (Python):
import collections

n = int(raw_input())
p = map(int,raw_input().split(' '))
q = map(int,raw_input().split(' '))

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


def f(p,n):

	if len(p) == 1:
		return 0
	sp = sorted(p)
	for i,e in enumerate(sp):
		if e == p[0]:
			hm = i
			break 

	return i * factorial(len(p) -1) + f(p[1:],n)


print abs(f(p,n) - f(q,n))

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


import itertools
import math
A=[x for x in range(1,N+1)]
lis=itertools.permutations(A)

c=0
d=0
for s in lis:

  judge=True
  c+=1
  for i in range(N):
    if P[i] != s[i]:
      judge=False
  if judge:
    break
    
li=itertools.permutations(A)
  
for s in li:
  judge=True
  d+=1
  for i in range(N):
    if Q[i] != s[i]:
      judge=False
  if judge:
    break

print(abs(c-d))

Solution (Python):
import itertools

N = int(input())
l = list(range(1,N+1))
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
i=0
a=0
b=0
for p in itertools.permutations(l,N):
	i+=1
	if a==0:
		for j in range(N):
			if P[j]!=p[j]:break
		else:
			a=i
	if b==0:
		for j in range(N):
			if Q[j]!=p[j]:break
		else:
			b=i
print(abs(a-b))

Solution (Python):

import sys
from itertools import *


def S(): return sys.stdin.readline().rstrip()


def I(): return int(sys.stdin.readline().rstrip())


def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))


def LS(): return list(sys.stdin.readline().rstrip().split())


N = I()
p = tuple(LI())
q = tuple(LI())

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

all = list(permutations(all, N))


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

print(abs(a-b))

Solution (Python):
import itertools

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

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

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

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

p = 0
q = 0
if P == Q:
  ans = 0
  print(ans)
  quit()
  
x = 0
for one_case in permutations_lis:
  x += 1
  if P == list(one_case):
    p = x
  elif Q == list(one_case):
    q = x

print(abs(q-p))

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

Solution (Python):
import itertools

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

a=list(itertools.permutations(p))
aa=sorted(a)
b=abs(aa.index(tuple(p))-aa.index(tuple(q)))
print(b)

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