

from collections import defaultdict,deque

import sys,heapq,bisect,math,itertools,string,queue,copy,time

sys.setrecursionlimit(10**8)

INF = float('inf')

mod = 10**9+7

eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())



N = inp()

AA = inpl()



cnts = [0]*(N+1)

cnts[-1] = 3



ans = 1

for a in AA:

    ans *= (cnts[a-1] - cnts[a])

    ans %= mod

    cnts[a] += 1





print(ans)

c = [0]*(int(eval(input()))+1)

c[-1] = 3

ans = 1

for a in list(map(int, input().split())):

    ans = (ans * (c[a-1] - c[a]))%(10**9+7)

    c[a] += 1

print(ans)

from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time

sys.setrecursionlimit(10**8)
INF = float("inf")
mod = 10**9 + 7
eps = 10**-7


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


def inpl_str():
    return list(sys.stdin.readline().split())


N = inp()
AA = inpl()
cnts = [0] * (N + 1)
cnts[-1] = 3
ans = 1
for a in AA:
    ans *= cnts[a - 1] - cnts[a]
    ans %= mod
    cnts[a] += 1
print(ans)

c = [0] * (int(eval(input())) + 1)
c[-1] = 3
ans = 1
for a in list(map(int, input().split())):
    ans = (ans * (c[a - 1] - c[a])) % (10**9 + 7)
    c[a] += 1
print(ans)

-from collections import defaultdict, deque -import sys, heapq, bisect, math, itertools, string, queue, copy, time - -sys.setrecursionlimit(10**8) -INF = float("inf") -mod = 10**9 + 7 -eps = 10**-7 - - -def inp(): -    return int(sys.stdin.readline()) - - -def inpl(): -    return list(map(int, sys.stdin.readline().split())) - - -def inpl_str(): -    return list(sys.stdin.readline().split()) - - -N = inp() -AA = inpl() -cnts = [0] * (N + 1) -cnts[-1] = 3 +c = [0] * (int(eval(input())) + 1) +c[-1] = 3 -for a in AA: -    ans *= cnts[a - 1] - cnts[a] -    ans %= mod -    cnts[a] += 1 +for a in list(map(int, input().split())): +    ans = (ans * (c[a - 1] - c[a])) % (10**9 + 7) +    c[a] += 1