n=int(eval(input()))

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



col=[0,0,0]

res=1



for ai in a:

    cnt=0

    for i in range(3):

        if col[i]==ai:

            if cnt==0:

                col[i]=col[i]+1



            

            cnt+=1

            

    

    res=(res*cnt)%1000000007



print(res)

n=int(eval(input()))

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



cnt=[0,0,0]

ans=1

mod=10**9+7



for aa in a:

    tmp=0

    for c in cnt:

        if aa==c:

            tmp+=1

    

    if tmp>0:

        for i in range(3):

            if cnt[i]==aa:

                cnt[i]+=1

                break

    

    ans*=tmp

    ans%=mod



print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
col = [0, 0, 0]
res = 1
for ai in a:
    cnt = 0
    for i in range(3):
        if col[i] == ai:
            if cnt == 0:
                col[i] = col[i] + 1
            cnt += 1
    res = (res * cnt) % 1000000007
print(res)

n = int(eval(input()))
(*a,) = list(map(int, input().split()))
cnt = [0, 0, 0]
ans = 1
mod = 10**9 + 7
for aa in a:
    tmp = 0
    for c in cnt:
        if aa == c:
            tmp += 1
    if tmp > 0:
        for i in range(3):
            if cnt[i] == aa:
                cnt[i] += 1
                break
    ans *= tmp
    ans %= mod
print(ans)

-a = list(map(int, input().split())) -col = [0, 0, 0] -res = 1 -for ai in a: -    cnt = 0 -    for i in range(3): -        if col[i] == ai: -            if cnt == 0: -                col[i] = col[i] + 1 -            cnt += 1 -    res = (res * cnt) % 1000000007 -print(res) +(*a,) = list(map(int, input().split())) +cnt = [0, 0, 0] +ans = 1 +mod = 10**9 + 7 +for aa in a: +    tmp = 0 +    for c in cnt: +        if aa == c: +            tmp += 1 +    if tmp > 0: +        for i in range(3): +            if cnt[i] == aa: +                cnt[i] += 1 +                break +    ans *= tmp +    ans %= mod +print(ans)