You are given a queue of N people, each wearing a red, blue, or green hat. Person i states the number of people in front of them wearing the same color hat. Find the number of possible hat color combinations, considering all statements to be true. Output the result modulo 1000000007. Constraints: 1 ≤ N ≤ 100000, 0 ≤ A_i ≤ N-1. Input format: N followed by A_1 to A_N. Output the number of possible combinations. Example: Input 1: 6 0 1 2 3 4 5, Output 1: 3. Example: Input 2: 3 0 0 0, Output 2: 6. Example: Input 3: 54 0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17, Output 3: 115295190.

# Buggy version:
n=int(input())
a=list(map(int,input().split()))

ans=1

d={}

ch=0

mod=10**9+7

for i in range(n):
  if a[i]==0:
    if 0 not in d:
      d[0]=1
      ans*=3
    else:
      if d[0]==1:
        d[0]=2
        ans*=2
      elif:
        d[0]=3
      else:
        ch+=1
        break
      
  else:
    if a[i] not in d:
      d[a[i]]=1
      if a[i]-1 not in d:
        ch+=1
        break
      else:
        ans*=d[a[i]-1]
    else:
      if d[a[i]]==1:
        d[a[i]]=2
        if a[i]-1 not in d:
          ch+=1
          break
        else:
          if d[a[i]-1]>=2:
            ans*=d[a[i]-1]-1
          else:
            ch+=1
            break
      elif:
        d[a[i]]=3
        if a[i]-1 not in d:
          ch+=1
          break
        else:
          if d[a[i]-1]>=3:
            ans*=1
          else:
            ch+=1
            break
      else:
        ch+=1
        break
        
  ans=(ans%mod)
  
if ch==0:
  print(ans)
else:
  print(0)



# Fixed version:
n=int(input())
a=list(map(int,input().split()))

ans=1

d={}

ch=0

mod=10**9+7

for i in range(n):
  if a[i]==0:
    if 0 not in d:
      d[0]=1
      ans*=3
    else:
      if d[0]==1:
        d[0]=2
        ans*=2
      elif d[0]==2:
        d[0]=3
      else:
        ch+=1
        break
      
  else:
    if a[i] not in d:
      d[a[i]]=1
      if a[i]-1 not in d:
        ch+=1
        break
      else:
        ans*=d[a[i]-1]
    else:
      if d[a[i]]==1:
        d[a[i]]=2
        if a[i]-1 not in d:
          ch+=1
          break
        else:
          if d[a[i]-1]>=2:
            ans*=d[a[i]-1]-1
          else:
            ch+=1
            break
      elif d[a[i]]==2:
        d[a[i]]=3
        if a[i]-1 not in d:
          ch+=1
          break
        else:
          if d[a[i]-1]>=3:
            ans*=1
          else:
            ch+=1
            break
      else:
        ch+=1
        break
        
  ans=(ans%mod)
  
if ch==0:
  print(ans)
else:
  print(0)

# Buggy version:
N=int(input())
A=list(map(int, input().split()))
ans=1
c=[0,0,0]
for i in range(N):
  a,f,k=A[i],-1,0
  for j in range(3):
    if c[j]==a:
      k+=1
      if f==-1:
        c[j]+=1
        f=j
  ans*=k%(10**9+7)
print(ans)

# Fixed version:
P=10**9+7
N=int(input())
A=list(map(int, input().split()))
ans=1
c=[0,0,0]
for i in range(N):
  a,f,k=A[i],-1,0
  for j in range(3):
    if c[j]==a:
      k+=1
      if f==-1:
        c[j]+=1
        f=j
  ans=(ans*k)%P
print(ans)

# Buggy version:
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
G = [0]*3
ans = 1
for i in range(N):
    x = 0
    cnt = 0
    for j, g in enumerate(G):
        if g == A[i]:
            x = j if cnt == 0 else x
            cnt += 1
    
    G[x] += 1
    ans *= cnt
    ans = ans % mod
    print(i, G)

print(ans)

# Fixed version:
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
G = [0]*3
ans = 1
for i in range(N):
    x = 0
    cnt = 0
    for j, g in enumerate(G):
        if g == A[i]:
            x = j if cnt == 0 else x
            cnt += 1
    
    G[x] += 1
    ans *= cnt
    ans = ans % mod

print(ans)

# Buggy version:
n=int(input())
a=list(map(int,input().split()))
a_=max(a)
con=[0]*(a_+1)
tou=[0]*(a_+1)
ans=[]
for i in range(len(a)):
    u=a[i]
    if u==0:
        con[0]+=1
        tou[0]+=1
        ans.append(4-con[0])

    else:
        con[u]+=1 
        tou[u]+=1  
        ans.append(min(tou[u-1],4-con[u]))
c=1
for i in range(len(ans)):
    c*=ans[i]
print(c%(10**9+7))   
print(ans) 

# Fixed version:
n=int(input())
a=list(map(int,input().split()))
a_=max(a)
con=[0]*(a_+1)
tou=[0]*(a_+1)
ans=[]
for i in range(len(a)):
    u=a[i]
    if u==0:
        con[0]+=1
        tou[0]+=1
        ans.append(4-con[0])

    else:
        con[u]+=1 
        tou[u]+=1  
        ans.append(min(tou[u-1]-con[u]+1,4-con[u]))
c=1
for i in range(len(ans)):
    c*=ans[i]
    c=c%(10**9+7)
print(c%(10**9+7))

# Buggy version:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
A = LIST()

cnt = [0]*(max(A)+2)
cnt[0] = 3
ans = 1
for a in A:
	print(cnt)
	ans *= cnt[a] - cnt[a+1]
	cnt[a+1] += 1
	ans %= mod

print(ans)

# Fixed version:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
A = LIST()

ans = 1
cnt = [0, 0, 0]
for i in range(N):
	ans = ans * cnt.count(A[i]) % mod
	if A[i] in cnt:
		cnt[cnt.index(A[i])] += 1
	else:
		print(0)
		exit()

print(ans)

# Buggy version:
N = int(input())
A = list(map(int,input().split()))
C = [0,0,0]
Ans = 1
MOD = 1000000007
for a in A:
    ask = C.count(a)
    if ask==0:
        Ans=0
        break
    ind = C.index(a)
    C[ind] += 1
    Ans = (Ans*ask)%MOD
    print(C)
print(Ans)


# Fixed version:
N = int(input())
A = list(map(int,input().split()))
C = [0,0,0]
Ans = 1
MOD = 1000000007
for a in A:
    ask = C.count(a)
    if ask==0:
        Ans=0
        break
    ind = C.index(a)
    C[ind] += 1
    Ans = (Ans*ask)%MOD
print(Ans)

# Buggy version:
def f():
	n=int(input())
    l=[0,0,0]
    ans=1
    mod=10**9+7
    for i in list(map(int,input().split())):
        if i not in l:print(0);exit()
        ans=ans*l.count(i)%mod
        l[l.index(i)]+=1
    print(ans)
if _name__=="__main__":f()

# Fixed version:
def f():
    n=int(input())
    l=[0,0,0]
    ans=1
    mod=10**9+7
    for i in list(map(int,input().split())):
        if i not in l:print(0);exit()
        ans=ans*l.count(i)%mod
        l[l.index(i)]+=1
    print(ans)
if __name__ == "__main__":f()

# Buggy version:
N=int(input())
A=list(map(int,input().split()))
p=1000000007


R=[0]
G=[0]
B=[0]

#print(A)
ans=1
for i in range(N):
    if A[i]==R[-1]:
        tmp+=1
    if A[i]==G[-1]:
        tmp+=1
    if A[i]==B[-1]:
        tmp+=1
    ans = (ans*tmp)%p

    if A[i]==R[-1]:
        R.append(R[-1]+1)
        G.append(G[-1])
        B.append(B[-1])
    elif A[i]==G[-1]:
        R.append(R[-1])
        G.append(G[-1]+1)
        B.append(B[-1])
    else:
        R.append(R[-1])
        G.append(G[-1])
        B.append(B[-1]+1)
    tmp=0
    
#print(R)
#print(G)
#print(B)
print(ans)

# Fixed version:
N=int(input())
A=list(map(int,input().split()))
p=1000000007


R=[0]
G=[0]
B=[0]

#print(A)
ans=1
for i in range(N):
    tmp=0
    if A[i]==R[-1]:
        tmp+=1
    if A[i]==G[-1]:
        tmp+=1
    if A[i]==B[-1]:
        tmp+=1
    ans = (ans*tmp)%p

    if A[i]==R[-1]:
        R.append(R[-1]+1)
        G.append(G[-1])
        B.append(B[-1])
    elif A[i]==G[-1]:
        R.append(R[-1])
        G.append(G[-1]+1)
        B.append(B[-1])
    else:
        R.append(R[-1])
        G.append(G[-1])
        B.append(B[-1]+1)
    
#print(R)
#print(G)
#print(B)
print(ans)

# Buggy version:
N=int(input())
A=list(map(int,input().split()))
l=[[] for _ in range(3)]
ans=1
p=10**9+7
flag=1
for i in range(N):
    cnt=0
    piv=A[i]
    ind=[]
    for j in range(3):
        if l[j]==piv:
            cnt=cnt+1
            ind.append(j)
    if len(ind)==0:
        flag=0
        break
    l[ind[0]].append(piv)
    ans=(ans*cnt)%p
    #print(l,ans)
    #input()
if flag==1:
    print(ans%p)
else:
    print(flag)



# Fixed version:
N=int(input())
A=list(map(int,input().split()))
l=[[] for _ in range(3)]
ans=1
p=10**9+7
flag=1
for i in range(N):
    cnt=0
    piv=A[i]
    ind=[]
    for j in range(3):
        if len(l[j])==piv:
            cnt=cnt+1
            ind.append(j)
    if len(ind)==0:
        flag=0
        break
    l[ind[0]].append(piv)
    ans=(ans*cnt)%p
    #print(l,ans)
    #input()
if flag==1:
    print(ans%p)
else:
    print(flag)

# Buggy version:
N=int(input())
A=list(map(int, input().split()))
mod=10**9+7
if A[0]!=0:
    print(0)
    break
L=[1,0,0]
ans=3
for i in range(1,N):
    a=A[i]
    count=0
    add=True
    for j,l in enumerate(L):
        if a==l:
            count+=1
            if add:
                L[j]+=1
                add=False

    ans*=count
    ans%=mod
print(ans)
#print(L)


# Fixed version:
N=int(input())
A=list(map(int, input().split()))
mod=10**9+7
if A[0]!=0:
    print(0)
    exit()
L=[1,0,0]
ans=3
for i in range(1,N):
    a=A[i]
    count=0
    add=True
    for j,l in enumerate(L):
        if a==l:
            count+=1
            if add:
                L[j]+=1
                add=False

    ans*=count
    ans%=mod
print(ans)
#print(L)

# Buggy version:
_, *a = open(0)
mod = 10**9+7
c = 1
l = [3] + [0]*10**6

for a in a.split():
  a = int(a)
  c = c * l[a] % mod
  d[a] -= 1
  d[a] += 1
print(c)

# Fixed version:
_, a = open(0)
mod = 10**9+7
c = 1
l = [3] + [0]*10**6

for a in a.split():
  a = int(a)
  c = c * l[a] % mod
  l[a] -= 1
  l[a+1] += 1
print(c)

# Buggy version:
n=int(input())
a=list(map(int,input().split()))
MOD=1000000007
hats=[0,0,0]
ans=1
for i in range(n):
    ai=a[i]
    ans=((ans%MOD)*hats.count(ai))%MOD
    if(ans==0):
        break
    hats[hats.index(ai)]+=1
    print(ai,ans,hats)
print(ans%MOD)


# Fixed version:
n=int(input())
a=list(map(int,input().split()))
MOD=1000000007
hats=[0,0,0]
ans=1
for i in range(n):
    ai=a[i]
    ans=((ans%MOD)*hats.count(ai))%MOD
    if(ans==0):
        break
    hats[hats.index(ai)]+=1
#    print(ai,ans,hats)
print(ans%MOD)

# Buggy version:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)

MOD = 10 ** 9 + 7


def main():
    N = int(input())
    A = list(map(int, input().split()))
    RGB = [1, 0, 0]
    ans = 1
    for i in range(1, N):
        a = A[i]
        if a == 0:
            j = RGB.index(a)
            RGB[j] += 1

        elif a in RGB:
            print(0)
            return
        else:
            k = RGB.count(a)
            ans *= k
            ans %= MOD
            j = RGB.index(a)
            RGB[j] += 1
    z = RGB.count(0)
    if z == 0:
        ans *= 6
        ans %= MOD
    elif z == 1:
        ans *= 6
        ans %= MOD
    else:
        ans *= 3
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()


# Fixed version:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(10 ** 9)

MOD = 10 ** 9 + 7


def main():
    N = int(input())
    A = list(map(int, input().split()))
    RGB = [1, 0, 0]
    ans = 1
    if A[0] !=0:
        print(0)
        return
    for i in range(1, N):
        a = A[i]
        if a == 0:
            if 0 in RGB:
                j = RGB.index(a)
                RGB[j] += 1
            else:
                print(0)
                return
        elif a not in RGB:
            print(0)
            return
        else:
            k = RGB.count(a)
            ans *= k
            ans %= MOD
            j = RGB.index(a)
            RGB[j] += 1
    z = RGB.count(0)
    if z == 0:
        ans *= 6
        ans %= MOD
    elif z == 1:
        ans *= 6
        ans %= MOD
    else:
        ans *= 3
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()

# Buggy version:
import itertools
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
M = 1000000007
N = int(input())
left = list(map(int, input().split()))
#assert len(left) == N
#assert left[0] == 0

counter = Counter(left)


def dp(t, ns):
    cached = t.get(ns)
    if cached is not None:
        return cached
    remaining = sum(ns)
    #assert remaining > 0
    last_cnt = left[remaining - 1] + 1
    n1, n2, n3 = ns
    res = 0
    if last_cnt == n1:
        res += dp(t, tuple(sorted([n1 - 1, n2, n3])))
        res %= M
    if last_cnt == n2:
        res += dp(t, tuple(sorted([n1, n2 - 1, n3])))
        res %= M
    if last_cnt == n3:
        res += dp(t, tuple(sorted([n1, n2, n3 - 1])))
        res %= M
    print(f"{remaining}: ({n1},{n2},{n3}) => {res}")
    t[ns] = res
    return res


def solve():
    h = [0, 0, 0]
    for i in range(N):
        k = counter[i]
        if k == 3:
            h[0] = h[1] = h[2] = i + 1
        elif k == 2:
            h[0] = h[1] = i + 1
        elif k == 1:
            h[0] = i + 1
        else:
            break
    #assert sum(h) == N
    t = dict()
    t[0, 0, 0] = 1
    res = dp(t, tuple(sorted(h)))
    return res * len(set(itertools.permutations(h))) % M


print(solve())


# Fixed version:
import itertools
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 9)
M = 1000000007
N = int(sys.stdin.readline())
left = list(map(int, sys.stdin.read().split()))
counter = Counter(left)


def dp(t, ns):
    cached = t.get(ns)
    if cached is not None:
        return cached
    remaining = sum(ns)
    assert remaining > 0
    last_cnt = left[remaining - 1] + 1
    n1, n2, n3 = ns
    res = 0
    if last_cnt == n1:
        res += dp(t, tuple(sorted([n1 - 1, n2, n3])))
        res %= M
    if last_cnt == n2:
        res += dp(t, tuple(sorted([n1, n2 - 1, n3])))
        res %= M
    if last_cnt == n3:
        res += dp(t, tuple(sorted([n1, n2, n3 - 1])))
        res %= M
    # print(f"{remaining}: ({n1},{n2},{n3}) => {res}")
    t[ns] = res
    return res


def solve():
    h = [0, 0, 0]
    for i in range(N):
        k = counter[i]
        if k == 3:
            h[0] = h[1] = h[2] = i + 1
        elif k == 2:
            h[0] = h[1] = i + 1
        elif k == 1:
            h[0] = i + 1
        else:
            break
    if sum(h) != N:
        return 0
    t = dict()
    t[0, 0, 0] = 1
    res = dp(t, tuple(sorted(h)))
    return (res * len(set(itertools.permutations(h)))) % M


print(solve())

# Buggy version:
import sys
input=lambda: sys.stdin.readline().rstrip()

n=int(input())
A=[int(i) for i in input().split()]
ans=1
mod=10**9+7
C=[0,0,0]
for i in range(1,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)


# Fixed version:
import sys
input=lambda: sys.stdin.readline().rstrip()

n=int(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)

# Buggy version:
import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    ans %= mod
    for i in range(3):
      if caps[i] == a:
        caps[i] += 1
print(ans)


# Fixed version:
import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    # caps[caps.index(a)] += 1
    for i in range(3):
        if caps[i] == a:
            caps[i] += 1
            break
    ans %= mod
print(ans)

# Buggy version:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)

ans=1
for num in a:
    if num==0:
        ans*= (3 - d[0])
    else:
        ans*= (d[num-1] - d[num])

    ans*=tmp
    ans%=1000000007
    d[num]+=1

print(ans)

# Fixed version:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)

ans=1
for num in a:
    if num==0:
        ans*= (3 - d[0])
    else:
        ans*= (d[num-1] - d[num])

    ans%=1000000007
    d[num]+=1

print(ans)

# Buggy version:
N = int(input())
A = list(map(int,input().split()))

MOD = 1000000007

#色ごとの人数
C=[0,0,0]
ans=1
for a in A:
    #人数が一致する色がなければOUT
    found=False
    count=C.count(a)
    if count==0:
        print(0)
        exit()
    #適当に1人足す
    C[C.index(a)]+=1
    #今見ている人の色の可能性はcount種類
    ans=ans*count%MOD
    print(count)

print(ans)

# Fixed version:
N = int(input())
A = list(map(int,input().split()))

MOD = 1000000007

#色ごとの人数
C=[0,0,0]
ans=1
for a in A:
    #人数が一致する色がなければOUT
    found=False
    count=C.count(a)
    if count==0:
        print(0)
        exit()
    #適当に1人足す
    C[C.index(a)]+=1
    #今見ている人の色の可能性はcount種類
    ans=ans*count%MOD

print(ans)

# Buggy version:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [-1, -1, -1]
    ans = 1
    for a in A:
        mi = None
        count = 0
        for i in range(3):
            if RGB[i] == a-1:
                if mi == None:
                    RGB[mi] = a
                    mi = i
                count += 1
        if count > 1:
            ans *= count
            ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()


# Fixed version:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [-1, -1, -1]
    ans = 1
    for a in A:
        count = 0
        for i in range(3):
            if RGB[i] == a-1:
                if count == 0:
                    RGB[i] = a
                count += 1
        if count > 1:
            ans *= count
            ans %= MOD
        elif count == 0:
            ans = 0
            print(ans)
            return
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

# Buggy version:
=int(input())
A=list(map(int,input().split()))
x=0
y=0
z=0
ans=1
for a in A:
    c=0
    X,Y,Z=x,y,z
    if a==x:
        x+=1
        c+=1
    if a==y:
        c+=1
        if a!=X:
            y+=1
    if a==z:
        c+=1
        if a!=X and a!=Y:
            z+=1
    ans=ans*c%1000000007
print(ans)

# Fixed version:
n=int(input())
A=list(map(int,input().split()))
x=0
y=0
z=0
ans=1
for a in A:
    c=0
    X,Y,Z=x,y,z
    if a==x:
        x+=1
        c+=1
    if a==y:
        c+=1
        if a!=X:
            y+=1
    if a==z:
        c+=1
        if a!=X and a!=Y:
            z+=1
    ans=ans*c%1000000007
print(ans)

# Buggy version:
N = int(input())
A = map(int, input().split())
xyz,T = [0,0,0],1
for Ai in A:
  n_Ai = xyz.count(Ai)
  T = (T*n_Ai))%1000000007
  if n_Ai>0: xyz.count(Ai)xyz[xyz.index(Ai)] += 1
print(T)

# Fixed version:
N = int(input())
A = map(int, input().split())
xyz,T = [0,0,0],1
for Ai in A:
  n_Ai = xyz.count(Ai)
  T = (T*n_Ai)%1000000007
  if n_Ai>0: xyz[xyz.index(Ai)] += 1
print(T)

# Buggy version:
import collections

n=int(input())
b=[[0,0,0]]
a=[int(i) for i in input().split()]

def my_index(l, x):
    if x in l:
        return l.index(x)
    else:
        return -1

for i in range(n):
    k=b[-1].my_index(a[i])
    if k==-1:
        print(0)
        break
    c=[]
    for j in range(3):
        if j==k:
            c.append(b[-1][j]+1)
        else:
            c.append(b[-1][j])
    b.append(c)
else:
    x=1
    for i in range(n):
        x=x*b[i].count(a[i])%1000000007
    print(x)


# Fixed version:
import collections

n=int(input())
b=[[0,0,0]]
a=[int(i) for i in input().split()]

def my_index(l, x):
    if x in l:
        return l.index(x)
    else:
        return -1

for i in range(n):
    k=my_index(b[-1],a[i])
    if k==-1:
        print(0)
        break
    c=[]
    for j in range(3):
        if j==k:
            c.append(b[-1][j]+1)
        else:
            c.append(b[-1][j])
    b.append(c)
else:
    x=1
    for i in range(n):
        x=x*b[i].count(a[i])%1000000007
    print(x)

# Buggy version:
_,a=open(0)
c=1
d=[3]+[0]*10**6
for a in map(int,a.split()):
  c=c*dp[a]%(10**9+7)
  d[a]-=1
  d[a]+=1
print(c)

# Fixed version:
_,a=open(0)
c=1
d=[3]+[0]*10**6
for a in map(int,a.split()):
  c=c*d[a]%(10**9+7)
  d[a]-=1
  d[a+1]+=1
print(c)

# Buggy version:
N = int(input)
A = list(map(int, input().split()))
L = [0 for i in range(max(A)+2)]
ans = 1
L[0] = 3
p = 10 ** 9 + 7
for i in range(N):
  ai = A[i]
  ans *= (L[ai] - L[ai + 1])
  ans = ans % p
  L[ai + 1] += 1
print(ans)

# Fixed version:
N = int(input())
A = list(map(int, input().split()))
L = [0 for i in range(max(A)+2)]
ans = 1
L[0] = 3
p = 10 ** 9 + 7
for i in range(N):
  ai = A[i]
  ans *= (L[ai] - L[ai + 1])
  ans = ans % p
  L[ai + 1] += 1
print(ans)

# Buggy version:
N=int(input())
A = list(map(int,input().split()))
lis = [0]*200000
ans=1
num_zero=0
mod = 1000000007
for i in A:
  lis[i]+=1
  if i!=0:
    ans*lis[i-1]
    lis[i-1]-=1
    ans%=mod
    if lis[i-1]<0:
      ans=0
      break
    
  else:
    num_zero+=1

if num_zero>=2:
  mul = 6
  
else:
  mul = 3
  
print((ans*mul)%mod)

# Fixed version:
N=int(input())
A = list(map(int,input().split()))
lis = [0]*200000
ans=1
num_zero=0
mod = 1000000007
for i in A:
  lis[i]+=1
  if i!=0:
    ans*=lis[i-1]
    lis[i-1]-=1
    ans%=mod
    if lis[i-1]<0:
      ans=0
      break
    
  else:
    num_zero+=1

if (num_zero==2)or(num_zero==3):
  mul = 6
  
elif num_zero==1:
  mul = 3

else:
  mul = 0
  
print((ans*mul)%mod)

# Buggy version:
N = int(input())
A = list(map(int, input().split()))

c = [-1, -1 ,-1]

sum = 1
tmp = 0
for a in A:
    sum = sum * c.count(A-1) % 1000000007
    if A-1 == c[0]:
        c[0] += 1
        tmp = c.count(c[0] - 1) + 1
    elif A-1 == c[1]:
        c[1] += 1
        tmp = c.count(c[1]-1) + 1
    elif A-1 == c[2]:
        c[2] += 1
        tmp = c.count(c[2] - 1) + 1
print(sum)


# Fixed version:
N = int(input())
A = list(map(int, input().split()))

c = [-1, -1 ,-1]

sum = 1
tmp = 0
for i in range(N):
    sum = sum * c.count(A[i]-1) % 1000000007
    if (A[i]-1) == c[0]:
        c[0] += 1
        tmp = c.count(c[0] - 1) + 1
    elif (A[i]-1) == c[1]:
        c[1] += 1
        tmp = c.count(c[1]-1) + 1
    elif (A[i]-1) == c[2]:
        c[2] += 1
        tmp = c.count(c[2] - 1) + 1
print(sum)

# Buggy version:
N = int(input()) 
a = list(map(int, input().split()))
a[0:0] = [-1, -1, -1]
x=1
for i in range(3,N+2):
  b=a[0:i-1]
  x = x*((b.count(a[i]-1)-b.count(a[i])))
  if x=0:x=1
x = x % 1000000007
print(x)

# Fixed version:
N = int(input()) 
a = list(map(int, input().split()))
b = [-1, -1, -1]
x=1
for i in range(N):
  x = x*b.count(a[i]-1) % 1000000007
  if b[0]==(a[i]-1):
    b[0]+=1
  elif b[1]==(a[i]-1):
    b[1]+=1
  else:
    b[2]+=1
print(x)

# Buggy version:
INF = int(1e9) + 7

n = int(input())
a = list(map(int, input().split()))
col = [0,0,0]
ans = 1
for i in range(n):
        col[i] +=1
        continue
    c = set(col)
    m = 0
    for j in range(3):
        if c==col[j]:
            m +=1
    ans *= m
    
    for j in range(3):
        if i==col[j]:
            col[j] +=1
            break
        else:
            continue
    ans = ans%INF

print(ans)

# Fixed version:
INF = int(1e9) + 7

n = int(input())
a = list(map(int, input().split()))
col = [0, 0, 0]
ans = 1
for i in range(n):
    m = 0
    for j in range(3):
        if a[i] ==col[j]:
            m = col.count(a[i])
    ans *= m
    
    for j in range(3):
        if a[i]==col[j]:
            col[j] +=1
            break
        else:
            continue
    ans = ans%INF

print(ans)

# Buggy version:
import sys
input = sys.stdin.readline
N = int(input())
A = 

rbg = [1, 0, 0]
ans = 1
for a in [int(a) for a in input().strip().split()]:
    ans = (ans * rbg.count(a)) % (10 ** 9 + 7)
    rbg[rbg.index(a)] += 1

print(ans * 3 % (10 ** 9 + 7))

# Fixed version:
import sys
input = sys.stdin.readline
N = int(input())
rbg = [0, 0, 0]
ans = 1
A = [int(a) for a in input().strip().split()]
for a in A:
    if rbg.count(a) == 0:
        print(0)
        exit()
    ans = (ans * rbg.count(a)) % (10 ** 9 + 7)
    rbg[rbg.index(a)] += 1

print(ans)

# Buggy version:
n=int(input())
a=list(map(int,input().split()))

mod=1000000007
ans=1
r,b,g=0,0,0
for i in a:
    if r2==r and b2==b and g2==g:
        print(0)
        exit()
    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)

# Fixed version:
n=int(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)

# Buggy version:
N = int(input())
A = list(map(int,input().split()))
now = 0

c = [0 for k in range(max(A)+2)]
ans = 1
MOD = 1000000007

for k in range(N):
    c[A[k]] += 1
    ans *= c[A[k]]-c[A[k]+1]
    ans %= MOD
print(ans)


# Fixed version:
N = int(input())
A = list(map(int,input().split()))

c = [0 for k in range(max(A)+2)]
ans = 1
MOD = 1000000007

c[0] = 3
for k in range(N):
    ans *= c[A[k]] - c[A[k]+1]
    c[A[k]+1] += 1
    ans %= MOD

print(ans)

# Buggy version:
mod=10**9+7
n=int(input())
a=list(map(int,input().split()))
b=[0,0,0]
ans=1
for i in a:
  ans*=b.count(i)
  for j in range(3):
    if b[j]==i:
      b[j]+=1
      break
    else:print(1)
print(ans%mod)

# Fixed version:
n = int(input())
a = list(map(int, input().split()))

mod = 10**9 + 7


ans = 1
cnt = [0 for i in range(3)]
for i in range(n):
    count = 0
    for j in range(3):
        if cnt[j] == a[i]:
            count += 1
    ans *= count
    ans %= mod
    for j in range(3):
        if cnt[j] == a[i]:
            cnt[j] += 1
            break


print(ans)

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


# Fixed version:
n=int(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)