n=int(eval(input()))

A=[int(i) for i in input().split()]

mod=10**9+7

ans=1

B=[0,0,0]

for i in range(n):

  ct=0

  chk=False

  for j in range(3):

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

      ct+=1

      if not chk:

        B[j]+=1

        chk=True

  ans=(ans*ct)%mod

print(ans)



import sys

input=lambda: sys.stdin.readline().rstrip()



n=int(eval(input()))

A=[int(i) for i in input().split()]

ans=1

mod=10**9+7

C=[0,0,0]

for i in range(n):

  cur=A[i]

  ct=0

  Chk=True

  for i,c in enumerate(C):

    if cur==c:

      ct+=1

      if Chk:

        C[i]+=1

        Chk=False

  ans=(ans*ct)%mod

  

print(ans)

n = int(eval(input()))
A = [int(i) for i in input().split()]
mod = 10**9 + 7
ans = 1
B = [0, 0, 0]
for i in range(n):
    ct = 0
    chk = False
    for j in range(3):
        if B[j] == A[i]:
            ct += 1
            if not chk:
                B[j] += 1
                chk = True
    ans = (ans * ct) % mod
print(ans)

import sys

input = lambda: sys.stdin.readline().rstrip()
n = int(eval(input()))
A = [int(i) for i in input().split()]
ans = 1
mod = 10**9 + 7
C = [0, 0, 0]
for i in range(n):
    cur = A[i]
    ct = 0
    Chk = True
    for i, c in enumerate(C):
        if cur == c:
            ct += 1
            if Chk:
                C[i] += 1
                Chk = False
    ans = (ans * ct) % mod
print(ans)

+import sys + +input = lambda: sys.stdin.readline().rstrip() +ans = 1 -ans = 1 -B = [0, 0, 0] +C = [0, 0, 0] +    cur = A[i] -    chk = False -    for j in range(3): -        if B[j] == A[i]: +    Chk = True +    for i, c in enumerate(C): +        if cur == c: -            if not chk: -                B[j] += 1 -                chk = True +            if Chk: +                C[i] += 1 +                Chk = False