n,*a=list(map(int,open(0).read().split()))

c=1

d=[0]*3

for b in a:

  c=c*sum(t==b for t in d)%(10**9+7)

  for i,t in enumerate(d):

    if t==b:

      d[i]+=1

      break

print(c)
_,a=open(0)

c=1

d=[3]+[0]*10**6

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

  c=c*d[a]%(10**9+7)

  d[a]-=1

  d[a+1]+=1

print(c)
n, *a = list(map(int, open(0).read().split()))
c = 1
d = [0] * 3
for b in a:
    c = c * sum(t == b for t in d) % (10**9 + 7)
    for i, t in enumerate(d):
        if t == b:
            d[i] += 1
            break
print(c)

_, a = open(0)
c = 1
d = [3] + [0] * 10**6
for a in map(int, a.split()):
    c = c * d[a] % (10**9 + 7)
    d[a] -= 1
    d[a + 1] += 1
print(c)

-n, *a = list(map(int, open(0).read().split())) +_, a = open(0) -d = [0] * 3 -for b in a: -    c = c * sum(t == b for t in d) % (10**9 + 7) -    for i, t in enumerate(d): -        if t == b: -            d[i] += 1 -            break +d = [3] + [0] * 10**6 +for a in map(int, a.split()): +    c = c * d[a] % (10**9 + 7) +    d[a] -= 1 +    d[a + 1] += 1