import itertools

n=int(eval(input()))

numbers1k=[]

numbers2k=[]

s=[]

for i in range(n):

    s.append(i+1)

p=itertools.permutations(s,n)

u=list(p)

#print(u)

numbers1=input().rstrip().split(" ")

for num1 in numbers1:

    numbers1k.append(int(num1))

numbers2=input().rstrip().split(" ")

for num2 in numbers2:

    numbers2k.append(int(num2))

com1=tuple(numbers1k)

com2=tuple(numbers2k)

for i in range(len(u)):

    if u[i]==com1:

        a=i+1

for j in range(len(u)):

    if u[j]==com2:

        b=j+1

#print(a)

#print(b)

print((abs(b-a)))
#float型を許すな

#numpyはpythonで

import sys

input = sys.stdin.readline

sys.setrecursionlimit(10**7)

from collections import Counter, deque

from collections import defaultdict

from itertools import combinations, permutations, accumulate, groupby, product

from bisect import bisect_left,bisect_right

from heapq import heapify, heappop, heappush

from math import floor, ceil,pi,factorial

from operator import itemgetter

def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))

def LI2(): return [int(eval(input())) for i in range(n)]

def MXI(): return [[LI()]for i in range(n)]

def SI(): return input().rstrip()

def printns(x): print(('\n'.join(x)))

def printni(x): print(('\n'.join(list(map(str,x)))))

inf = 10**17

mod = 10**9 + 7



n=I()

lisp=tuple(LI())

lisq=tuple(LI())

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

u=permutations(x)

count=0

for i in u:

    if i==lisp:

        P=count

    if i==lisq:

        Q=count

    count+=1

print((abs(P-Q)))

    

    



import itertools

n = int(eval(input()))
numbers1k = []
numbers2k = []
s = []
for i in range(n):
    s.append(i + 1)
p = itertools.permutations(s, n)
u = list(p)
# print(u)
numbers1 = input().rstrip().split(" ")
for num1 in numbers1:
    numbers1k.append(int(num1))
numbers2 = input().rstrip().split(" ")
for num2 in numbers2:
    numbers2k.append(int(num2))
com1 = tuple(numbers1k)
com2 = tuple(numbers2k)
for i in range(len(u)):
    if u[i] == com1:
        a = i + 1
for j in range(len(u)):
    if u[j] == com2:
        b = j + 1
# print(a)
# print(b)
print((abs(b - a)))

# float型を許すな
# numpyはpythonで
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left, bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil, pi, factorial
from operator import itemgetter


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def LI2():
    return [int(eval(input())) for i in range(n)]


def MXI():
    return [[LI()] for i in range(n)]


def SI():
    return input().rstrip()


def printns(x):
    print(("\n".join(x)))


def printni(x):
    print(("\n".join(list(map(str, x)))))


inf = 10**17
mod = 10**9 + 7
n = I()
lisp = tuple(LI())
lisq = tuple(LI())
x = [i for i in range(1, n + 1)]
u = permutations(x)
count = 0
for i in u:
    if i == lisp:
        P = count
    if i == lisq:
        Q = count
    count += 1
print((abs(P - Q)))

-import itertools +# float型を許すな +# numpyはpythonで +import sys -n = int(eval(input())) -numbers1k = [] -numbers2k = [] -s = [] -for i in range(n): -    s.append(i + 1) -p = itertools.permutations(s, n) -u = list(p) -# print(u) -numbers1 = input().rstrip().split(" ") -for num1 in numbers1: -    numbers1k.append(int(num1)) -numbers2 = input().rstrip().split(" ") -for num2 in numbers2: -    numbers2k.append(int(num2)) -com1 = tuple(numbers1k) -com2 = tuple(numbers2k) -for i in range(len(u)): -    if u[i] == com1: -        a = i + 1 -for j in range(len(u)): -    if u[j] == com2: -        b = j + 1 -# print(a) -# print(b) -print((abs(b - a))) +input = sys.stdin.readline +sys.setrecursionlimit(10**7) +from collections import Counter, deque +from collections import defaultdict +from itertools import combinations, permutations, accumulate, groupby, product +from bisect import bisect_left, bisect_right +from heapq import heapify, heappop, heappush +from math import floor, ceil, pi, factorial +from operator import itemgetter + + +def I(): +    return int(eval(input())) + + +def MI(): +    return list(map(int, input().split())) + + +def LI(): +    return list(map(int, input().split())) + + +def LI2(): +    return [int(eval(input())) for i in range(n)] + + +def MXI(): +    return [[LI()] for i in range(n)] + + +def SI(): +    return input().rstrip() + + +def printns(x): +    print(("\n".join(x))) + + +def printni(x): +    print(("\n".join(list(map(str, x))))) + + +inf = 10**17 +mod = 10**9 + 7 +n = I() +lisp = tuple(LI()) +lisq = tuple(LI()) +x = [i for i in range(1, n + 1)] +u = permutations(x) +count = 0 +for i in u: +    if i == lisp: +        P = count +    if i == lisq: +        Q = count +    count += 1 +print((abs(P - Q)))