N = int(eval(input()))

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

mod = 1000000007

colors = [0,0,0]

ans = 1

for i in range(N):

    ans = (ans * colors.count(A[i])) % mod

    for j in range(3):

        if colors[j] == A[i]:

            colors[j] += 1

            break

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

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

mod = 10**9+7



RGB = [0, 0, 0]



ans = 1

for i in range(N):

    cnt = RGB.count(A[i])

    ans = (ans * cnt) % mod

    for j in range(3):

        if A[i] == RGB[j]:

            RGB[j] += 1

            break



print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
mod = 1000000007
colors = [0, 0, 0]
ans = 1
for i in range(N):
    ans = (ans * colors.count(A[i])) % mod
    for j in range(3):
        if colors[j] == A[i]:
            colors[j] += 1
            break
print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
mod = 10**9 + 7
RGB = [0, 0, 0]
ans = 1
for i in range(N):
    cnt = RGB.count(A[i])
    ans = (ans * cnt) % mod
    for j in range(3):
        if A[i] == RGB[j]:
            RGB[j] += 1
            break
print(ans)

-mod = 1000000007 -colors = [0, 0, 0] +mod = 10**9 + 7 +RGB = [0, 0, 0] -    ans = (ans * colors.count(A[i])) % mod +    cnt = RGB.count(A[i]) +    ans = (ans * cnt) % mod -        if colors[j] == A[i]: -            colors[j] += 1 +        if A[i] == RGB[j]: +            RGB[j] += 1