import itertools

N = int(eval(input()))

P = [int(a)-1 for a in input().split()]

Q = [int(a)-1 for a in input().split()]

i = 0

ans = 0

for a in itertools.permutations(list(range(N)), N):

    a = [b for b in a]

    if a == P:

        ans -= i

    if a == Q:

        ans += i

    i += 1



print((abs(ans)))
fact = [1]

for i in range(20):

    fact.append(fact[-1] * (i+1))



def calc(L):

    n = len(L)

    if n == 1: return 0

    a =  len([l for l in L if l < L[0]])

    return a * fact[n-1] + calc(L[1:])



N = int(eval(input()))

P = [int(a)-1 for a in input().split()]

Q = [int(a)-1 for a in input().split()]



print((abs(calc(P) - calc(Q))))
import itertools

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

fact = [1]
for i in range(20):
    fact.append(fact[-1] * (i + 1))


def calc(L):
    n = len(L)
    if n == 1:
        return 0
    a = len([l for l in L if l < L[0]])
    return a * fact[n - 1] + calc(L[1:])


N = int(eval(input()))
P = [int(a) - 1 for a in input().split()]
Q = [int(a) - 1 for a in input().split()]
print((abs(calc(P) - calc(Q))))

-import itertools +fact = [1] +for i in range(20): +    fact.append(fact[-1] * (i + 1)) + + +def calc(L): +    n = len(L) +    if n == 1: +        return 0 +    a = len([l for l in L if l < L[0]]) +    return a * fact[n - 1] + calc(L[1:]) + -i = 0 -ans = 0 -for a in itertools.permutations(list(range(N)), N): -    a = [b for b in a] -    if a == P: -        ans -= i -    if a == Q: -        ans += i -    i += 1 -print((abs(ans))) +print((abs(calc(P) - calc(Q))))