import itertools



N = int(eval(input()))

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

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



a = 0

b = 0

cnt = 0



tmp = sorted(P)



for x in itertools.permutations(tmp):

    cnt += 1



    if P == list(x):

        a = cnt



    if Q == list(x):

        b = cnt



print((abs(a - b)))

from itertools import permutations



N = int(eval(input()))

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

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



a = 0

b = 0

ans = 0



for i in permutations(list(range(1, N + 1))):

    a += 1

    b += 1



    if i == P:

        ans = abs(ans - a)



    if i == Q:

        ans = abs(ans - b)



print(ans)

import itertools

N = int(eval(input()))
P = list(map(int, (input().split())))
Q = list(map(int, (input().split())))
a = 0
b = 0
cnt = 0
tmp = sorted(P)
for x in itertools.permutations(tmp):
    cnt += 1
    if P == list(x):
        a = cnt
    if Q == list(x):
        b = cnt
print((abs(a - b)))

from itertools import permutations

N = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
a = 0
b = 0
ans = 0
for i in permutations(list(range(1, N + 1))):
    a += 1
    b += 1
    if i == P:
        ans = abs(ans - a)
    if i == Q:
        ans = abs(ans - b)
print(ans)

-import itertools +from itertools import permutations -P = list(map(int, (input().split()))) -Q = list(map(int, (input().split()))) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) -cnt = 0 -tmp = sorted(P) -for x in itertools.permutations(tmp): -    cnt += 1 -    if P == list(x): -        a = cnt -    if Q == list(x): -        b = cnt -print((abs(a - b))) +ans = 0 +for i in permutations(list(range(1, N + 1))): +    a += 1 +    b += 1 +    if i == P: +        ans = abs(ans - a) +    if i == Q: +        ans = abs(ans - b) +print(ans)