n=int(eval(input()))

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



mod=1000000007

ans=1

r,b,g=0,0,0

r2,b2,g2=-1,-1,-1

for i in a:

    if r2==r and b2==b and g2==g:

        print((0))

        exit()

    r2=r

    b2=b

    g2=g

    if i==r and i==b and i==g:

        ans*=3

        ans%=mod

        r+=1

    elif (i==r and i==b) or (i==g and i==r):

        ans*=2

        ans%=mod

        r+=1

    elif i==b and i==g:

        ans*=2

        ans%=mod

        g+=1

    elif i==r:

        r+=1

    elif i==b:

        b+=1

    elif i==g:

        g+=1



    else:

        print((0))

        exit()



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

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



mod=1000000007

ans=1

r,b,g=0,0,0

for i in a:

    if i==r and i==b and i==g:

        ans*=3

        ans%=mod

        r+=1

    elif (i==r and i==b) or (i==g and i==r):

        ans*=2

        ans%=mod

        r+=1

    elif i==b and i==g:

        ans*=2

        ans%=mod

        g+=1

    elif i==r:

        r+=1

    elif i==b:

        b+=1

    elif i==g:

        g+=1

    else:

        print((0))

        exit()

print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
mod = 1000000007
ans = 1
r, b, g = 0, 0, 0
r2, b2, g2 = -1, -1, -1
for i in a:
    if r2 == r and b2 == b and g2 == g:
        print((0))
        exit()
    r2 = r
    b2 = b
    g2 = g
    if i == r and i == b and i == g:
        ans *= 3
        ans %= mod
        r += 1
    elif (i == r and i == b) or (i == g and i == r):
        ans *= 2
        ans %= mod
        r += 1
    elif i == b and i == g:
        ans *= 2
        ans %= mod
        g += 1
    elif i == r:
        r += 1
    elif i == b:
        b += 1
    elif i == g:
        g += 1
    else:
        print((0))
        exit()
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
mod = 1000000007
ans = 1
r, b, g = 0, 0, 0
for i in a:
    if i == r and i == b and i == g:
        ans *= 3
        ans %= mod
        r += 1
    elif (i == r and i == b) or (i == g and i == r):
        ans *= 2
        ans %= mod
        r += 1
    elif i == b and i == g:
        ans *= 2
        ans %= mod
        g += 1
    elif i == r:
        r += 1
    elif i == b:
        b += 1
    elif i == g:
        g += 1
    else:
        print((0))
        exit()
print(ans)

-r2, b2, g2 = -1, -1, -1 -    if r2 == r and b2 == b and g2 == g: -        print((0)) -        exit() -    r2 = r -    b2 = b -    g2 = g