import itertools

N=int(eval(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)))
#Favorite Answers

from itertools import permutations

n=int(eval(input()))

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

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

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

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

N = int(eval(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)))

# Favorite Answers
from itertools import permutations

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

-import itertools +# Favorite Answers +from itertools import permutations -N = int(eval(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))) +n = int(eval(input())) +p = tuple(map(int, input().split())) +q = tuple(map(int, input().split())) +l = list(permutations(list(range(1, n + 1)))) +print((abs(l.index(p) - l.index(q))))