n=int(eval(input()))

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

mod=10**9+7

ans=1

rgb=[0]*3

for i in range(n):

    f=0

    for j in range(3):

        if l[i]==rgb[j]:

            if f==0:

                rgb[j]+=1

            f+=1

    ans*=f

    ans%=mod

print(ans)
mod=10**9+7

n=int(eval(input()))

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

ans=1

rgb=[0,0,0]

for i in range(n):

    c=0

    for j in range(3):

        if l[i]==rgb[j]:

            c+=1

    if c==0:

        print((0))

        exit()

    p=rgb.index(l[i])

    if not 0<=p<=2:

        print((0))

        exit()

    rgb[p]+=1

    ans*=c

    ans%=mod

print(ans)

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

mod = 10**9 + 7
n = int(eval(input()))
l = list(map(int, input().split()))
ans = 1
rgb = [0, 0, 0]
for i in range(n):
    c = 0
    for j in range(3):
        if l[i] == rgb[j]:
            c += 1
    if c == 0:
        print((0))
        exit()
    p = rgb.index(l[i])
    if not 0 <= p <= 2:
        print((0))
        exit()
    rgb[p] += 1
    ans *= c
    ans %= mod
print(ans)

+mod = 10**9 + 7 -mod = 10**9 + 7 -rgb = [0] * 3 +rgb = [0, 0, 0] -    f = 0 +    c = 0 -            if f == 0: -                rgb[j] += 1 -            f += 1 -    ans *= f +            c += 1 +    if c == 0: +        print((0)) +        exit() +    p = rgb.index(l[i]) +    if not 0 <= p <= 2: +        print((0)) +        exit() +    rgb[p] += 1 +    ans *= c