import sys

def cmb(n,r,mod):

  if r<0 or r>n:

    return 0

  r=min(r,n-r)

  return g1[n]*g2[r]*g2[n-r]%mod



n=int(eval(input()))

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

mod=10**9+7

g1=[1,1]

g2=[1,1]

inverse=[0,1]

 

for i in range(2,n*2):

  g1.append((g1[-1]*i)%mod)

  inverse.append((-inverse[mod%i]*(mod//i))%mod)

  g2.append((g2[-1]*inverse[-1])%mod)



ct=[0,0,0]

ans=1

for i in range(n):

  f=0

  ct2=0

  for j in range(3):

    if a[i]==ct[j]:

      if ct2==0:

        ct[j]+=1

      ct2+=1

  ans*=ct2

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

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

mod=10**9+7



ct=[0,0,0]

ans=1

for i in range(n):

  f=0

  ct2=0

  for j in range(3):

    if a[i]==ct[j]:

      if ct2==0:

        ct[j]+=1

      ct2+=1

  ans*=ct2

  ans%=mod

print((ans%mod))
import sys


def cmb(n, r, mod):
    if r < 0 or r > n:
        return 0
    r = min(r, n - r)
    return g1[n] * g2[r] * g2[n - r] % mod


n = int(eval(input()))
a = list(map(int, input().split()))
mod = 10**9 + 7
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, n * 2):
    g1.append((g1[-1] * i) % mod)
    inverse.append((-inverse[mod % i] * (mod // i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)
ct = [0, 0, 0]
ans = 1
for i in range(n):
    f = 0
    ct2 = 0
    for j in range(3):
        if a[i] == ct[j]:
            if ct2 == 0:
                ct[j] += 1
            ct2 += 1
    ans *= ct2
print((ans % mod))

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

-import sys - - -def cmb(n, r, mod): -    if r < 0 or r > n: -        return 0 -    r = min(r, n - r) -    return g1[n] * g2[r] * g2[n - r] % mod - - -g1 = [1, 1] -g2 = [1, 1] -inverse = [0, 1] -for i in range(2, n * 2): -    g1.append((g1[-1] * i) % mod) -    inverse.append((-inverse[mod % i] * (mod // i)) % mod) -    g2.append((g2[-1] * inverse[-1]) % mod) +    ans %= mod