from collections import deque

n = int(eval(input()))

p_que = deque(list(map(int, input().split())))

q_que = deque(list(map(int, input().split())))



que = deque(i for i in range(1, n+1))

cnt = 0

a = 0

b = 0



def dfs(d, q):

  # print('-'*10, q)

  # sorted(q)

  for i in list(q):

    # print(i, d, q)

    d.append(i)

    q.remove(i)

    # print('>', d, q)

    if q == deque([]):

      global cnt

      cnt += 1

      # print('>>>', cnt, d, q)

      if d == p_que:

        global a

        a = cnt

      if d == q_que:

        global b

        b = cnt

      # print('return')

      d.remove(i)

      q.appendleft(i)

      return

    # print('tansaku')

    dfs(d, q)

    # print('>returned')

    # print('---', i, d, q)

    d.remove(i)

    if i > q[0]:

      q.append(i)

    else:

      q.appendleft(i)

    q = deque(sorted(q))



dfs(deque(), que)

# print(a, b)

print((abs(a-b)))
from itertools import permutations

n = int(eval(input()))

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

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



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

# print(list(permutations(n_list, n)))

a = b = 0

for n, i in enumerate(permutations(n_list, n)):

  # print(n, i)

  if i == P:

    a = n

  if i == Q:

    b = n

    

print((abs(a-b)))
from collections import deque

n = int(eval(input()))
p_que = deque(list(map(int, input().split())))
q_que = deque(list(map(int, input().split())))
que = deque(i for i in range(1, n + 1))
cnt = 0
a = 0
b = 0


def dfs(d, q):
    # print('-'*10, q)
    # sorted(q)
    for i in list(q):
        # print(i, d, q)
        d.append(i)
        q.remove(i)
        # print('>', d, q)
        if q == deque([]):
            global cnt
            cnt += 1
            # print('>>>', cnt, d, q)
            if d == p_que:
                global a
                a = cnt
            if d == q_que:
                global b
                b = cnt
            # print('return')
            d.remove(i)
            q.appendleft(i)
            return
        # print('tansaku')
        dfs(d, q)
        # print('>returned')
        # print('---', i, d, q)
        d.remove(i)
        if i > q[0]:
            q.append(i)
        else:
            q.appendleft(i)
        q = deque(sorted(q))


dfs(deque(), que)
# print(a, b)
print((abs(a - b)))

from itertools import permutations

n = int(eval(input()))
P = tuple(map(int, input().split()))
Q = tuple(map(int, input().split()))
n_list = [x for x in range(1, n + 1)]
# print(list(permutations(n_list, n)))
a = b = 0
for n, i in enumerate(permutations(n_list, n)):
    # print(n, i)
    if i == P:
        a = n
    if i == Q:
        b = n
print((abs(a - b)))

-from collections import deque +from itertools import permutations -p_que = deque(list(map(int, input().split()))) -q_que = deque(list(map(int, input().split()))) -que = deque(i for i in range(1, n + 1)) -cnt = 0 -a = 0 -b = 0 - - -def dfs(d, q): -    # print('-'*10, q) -    # sorted(q) -    for i in list(q): -        # print(i, d, q) -        d.append(i) -        q.remove(i) -        # print('>', d, q) -        if q == deque([]): -            global cnt -            cnt += 1 -            # print('>>>', cnt, d, q) -            if d == p_que: -                global a -                a = cnt -            if d == q_que: -                global b -                b = cnt -            # print('return') -            d.remove(i) -            q.appendleft(i) -            return -        # print('tansaku') -        dfs(d, q) -        # print('>returned') -        d.remove(i) -        if i > q[0]: -            q.append(i) -        else: -            q.appendleft(i) -        q = deque(sorted(q)) - - -dfs(deque(), que) -# print(a, b) +P = tuple(map(int, input().split())) +Q = tuple(map(int, input().split())) +n_list = [x for x in range(1, n + 1)] +# print(list(permutations(n_list, n))) +a = b = 0 +for n, i in enumerate(permutations(n_list, n)): +    # print(n, i) +    if i == P: +        a = n +    if i == Q: +        b = n