import itertools

import bisect

n = int(eval(input()))

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

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



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



a = bisect.bisect_right(per, p)

b = bisect.bisect_right(per, q)



print((abs(a-b)))

import bisect

import itertools

n = int(eval(input()))

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

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



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

a = bisect.bisect_right(perm, p)

b = bisect.bisect_right(perm, q)

print((abs(a-b)))

import itertools
import bisect

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
(*per,) = itertools.permutations(list(range(1, n + 1)))
a = bisect.bisect_right(per, p)
b = bisect.bisect_right(per, q)
print((abs(a - b)))

import bisect
import itertools

n = int(eval(input()))
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))
(*perm,) = itertools.permutations(list(range(1, n + 1)), n)
a = bisect.bisect_right(perm, p)
b = bisect.bisect_right(perm, q)
print((abs(a - b)))

+import bisect -import bisect -(*per,) = itertools.permutations(list(range(1, n + 1))) -a = bisect.bisect_right(per, p) -b = bisect.bisect_right(per, q) +(*perm,) = itertools.permutations(list(range(1, n + 1)), n) +a = bisect.bisect_right(perm, p) +b = bisect.bisect_right(perm, q)