n = int(eval(input()))

l = list(input().split())

l1 = [int(i) for i in l]

ans = 1

mod = 10**9+7

ch = [0]*3

for i in range(n):

  if l1[i] in ch:

    ans = ans * ch.count(l1[i]) % mod

    

    k = ch.index(l1[i])

    ch[k] += 1

    

  else:

    print((0))

    exit()

    

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

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

ans = 1

mod = 10**9+7

ch = [0]*(n+1)

ch[0] = 3

for i in l:

  ans = ans * ch[i] % mod

  ch[i+1] += 1

  ch[i] -= 1

print(ans)

  
n = int(eval(input()))
l = list(input().split())
l1 = [int(i) for i in l]
ans = 1
mod = 10**9 + 7
ch = [0] * 3
for i in range(n):
    if l1[i] in ch:
        ans = ans * ch.count(l1[i]) % mod
        k = ch.index(l1[i])
        ch[k] += 1
    else:
        print((0))
        exit()
print(ans)

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

-l = list(input().split()) -l1 = [int(i) for i in l] +l = list(map(int, input().split())) -ch = [0] * 3 -for i in range(n): -    if l1[i] in ch: -        ans = ans * ch.count(l1[i]) % mod -        k = ch.index(l1[i]) -        ch[k] += 1 -    else: -        print((0)) -        exit() +ch = [0] * (n + 1) +ch[0] = 3 +for i in l: +    ans = ans * ch[i] % mod +    ch[i + 1] += 1 +    ch[i] -= 1