n=int(eval(input()))

a=list(map(int,input().split()))

d=[-1,-1,-1]

x=1

for i in a:

  b=0

  f=True

  for j in range(3):

    if d[j]==i-1:

      b+=1

      if f:

        f=False

        d[j]=i

  x*=b

  x%=10**9+7

print(x)
n=int(eval(input()))

d=[-1,-1,-1]

x=1

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

  x*=d.count(i-1)

  x%=10**9+7

  if i-1 in d:d[d.index(i-1)]+=1

print(x)
n = int(eval(input()))
a = list(map(int, input().split()))
d = [-1, -1, -1]
x = 1
for i in a:
    b = 0
    f = True
    for j in range(3):
        if d[j] == i - 1:
            b += 1
            if f:
                f = False
                d[j] = i
    x *= b
    x %= 10**9 + 7
print(x)

n = int(eval(input()))
d = [-1, -1, -1]
x = 1
for i in list(map(int, input().split())):
    x *= d.count(i - 1)
    x %= 10**9 + 7
    if i - 1 in d:
        d[d.index(i - 1)] += 1
print(x)

-a = list(map(int, input().split())) -for i in a: -    b = 0 -    f = True -    for j in range(3): -        if d[j] == i - 1: -            b += 1 -            if f: -                f = False -                d[j] = i -    x *= b +for i in list(map(int, input().split())): +    x *= d.count(i - 1) +    if i - 1 in d: +        d[d.index(i - 1)] += 1