Problem:
 Score: 500 points    Problem Statement  N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.  The person numbered i says:   "In front of me, exactly A_i people are wearing hats with the same color as mine."   Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.  Since the count can be enormous, compute it modulo 1000000007 .      Constraints   1 \leq N \leq 100000  0 \leq A_i \leq N-1  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  A_3  ...  A_N       Output  Print the number of possible combinations of colors of the N people's hats, modulo 1000000007 .        Sample Input 1 6
0 1 2 3 4 5      Sample Output 1 3  We have three possible combinations, as follows:   Red, Red, Red, Red, Red, Red  Blue, Blue, Blue, Blue, Blue, Blue  Green, Green, Green, Green, Green, Green        Sample Input 2 3
0 0 0      Sample Output 2 6       Sample 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      Sample Output 3 115295190

Solution (Python):
import subprocess
import gzip, base64, os
import stat

gz = b'H4sIAJ/d410C/+19e2ATVb7/JGlLeDUBESogFC0KIqWFAq2otJTABFKotCCrwtCmKY2mTW8ygaKoYFvXGKvourusv13l7r3renfv7uo+XPCBvIuiiC8WxUdlRSeWly+soOR+v+ecSc9MZiq43vvPzyg98/3M9zy+3/M93/OYmXNud3lmWS0WQf1ZhasFpJ4YVkzoYobf5U2yAFYo9Ia/I4XhQgbQ6RxfsVCsCbtY0mpoZ3w2+JcG/wqtlC60FmvC4YxPDS1cmC7wv2JNqCwSNKEgZCfjYVkPLKHogSXLNKEzjeJ5vbTxrCxeF4vXxfjVULAKmlCVL439q2R4JZNLDWcyvpkcP/7KD8s1eJ11DaWzrinWhKsZ32pdvGsgXoZw9j8nCxew/Mz0MoaVXw3VepgQ8FdPKZgQqBkf8DdEmsY3FU4ZP6UgNxzMnUjK5GS8s+ct1Ogxi5V5ELMB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akq0iR4g/WNAZ/sq8mdUlQ40ZhJ6n5GKoHiQ6sE+py1JlJfvwqicJTU/WiVsUrSrAUlZS7JNW8mnnlLSs6umdTzZ82qcFVKlSUzPC7k0CRRowXIoYeCNPNH80rK3KUCPpRVMf1BnMXF/FGGPR9cquOlByx6wxEiuZDM4RzP8NSXwPB425RiGh7Aq+HKLTqro4TJcZWpyXOHYhqoqIczfrVlyM8XmHaMD9bUpk2PsGQYO5GSnIuphVKOttTd7vEkU700KWeC6mu5+9zN1GxMzt9NrVLTs3JTcsN3M6DOAwEpebLwdztJWJsyOUzUQIDUk4Z1JerxqOn5EvkYTYqEfTWpJwzrShDy+VIK0NM5wNro9BxUXQI9nHWtb/Smx1kbmXfKQa+GbsLotGqDlmRwFq2ey+B4Y71hgHiqR0w53DnV3siZwKkwd1q1Pv2acFCqg54WlEcPoTVsc+jt2BDqezxVW++32Bm2emvRngqtly71PGW9iNXhMGcRySPFUyrjrM4ZLi7mzspNPR3Z0IukHJWcmoj2OGmjVLRntWtTMDzh3eRgdJ3rJUcKGziI73ZAuK7p+4K1enNKHqOulYD05d/tkG5dnuQb1RQTOuszsg26ZYOD682au8n540b1yR1grlMFeZnM6OTxVLP5Hk7YNugYDaRLHm+vu6c51hrusWOm9afWp2pVPYDcYIRDjxA3KIT+fPVUU25c1V3z+QWpx3Trxx3+BoH6tf8BTt6pODaLAAA='
bin = gzip.decompress(base64.b64decode(gz))
p = os.path.dirname(__file__)
pymain = os.path.join(p, "a.out")
with open(pymain, "wb") as f:
    f.write(bin)
os.chmod(pymain, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
if p == "":
    pymain = "./" + pymain
subprocess.call(pymain)



'''

import numpy as np
i8 = np.int64
i4 = np.int32

def solve(A, N):
    MOD = 1000000007
    x = 0
    y = 0
    z = 0
    count = 1
    for i in range(N):
        if A[i] == x:
            if x == y and x == z:
                count = (count * 3) % MOD
            elif x == y:
                count = (count * 2) % MOD
            x += 1
        elif A[i] == y:
            if y == z:
                count = (count * 2) % MOD
            y += 1
        elif A[i] == z:
            z += 1
        else:
            return 0
    return count

def main2():
    stdin = open('/dev/stdin')
    N = int(stdin.readline())
    A = np.empty(N, i8)
    for i, c in enumerate(map(int, stdin.readline().split())):
        A[i] = c
    print(solve(A, N))
'''

Solution (Python):
n=int(input())
A=list(map(int,input().split()))

mod=10**9+7
ans=1
cs=[0]*3
for a in A:
  c=cs.count(a)
  if c==0:
    print(0)
    exit()
  ans=(ans*c)%mod
  i=cs.index(a)
  cs[i]+=1
print(ans)

Solution (Python):
import numpy as np
i8 = np.int64
i4 = np.int32

def solve(A, N):
    MOD = 1000000007
    x = 0
    y = 0
    z = 0
    count = 1
    for i in range(N):
        if A[i] == x:
            if x == y and x == z:
                count = (count * 3) % MOD
            elif x == y:
                count = (count * 2) % MOD
            x += 1
        elif A[i] == y:
            if y == z:
                count = (count * 2) % MOD
            y += 1
        elif A[i] == z:
            z += 1
        else:
            return 0
    return count

def main2():
    stdin = open('/dev/stdin')
    N = int(stdin.readline())
    A = np.empty(N, i8)
    for i, c in enumerate(map(int, stdin.readline().split())):
        A[i] = c
    print(solve(A, N))

main2()

Solution (Python):
import sys
from bisect import *
import math

input = sys.stdin.readline

N = int(input())
A = list(map(int,input().split()))
mod = 1000000007

cnum = A.count(0)
amax = max(A)

numind = [[] for i in range(amax+1)]
for i,ai in enumerate(A):
	numind[ai].append(i)

if cnum==0 or cnum>3:
	res = 0
else:
	res = 1
	for i in range(amax):
		if len(numind[i+1])==0 or len(numind[i+1])>3:
			res = 0
			break
		for order, j in enumerate(numind[i+1]):
			res = (res * (bisect(numind[i],j)-order)) % mod

	res = (res*math.factorial(3)//math.factorial(3-cnum)) % mod

print(res)

Solution (Python):
N = int(input())
A = map(int, input().split())
MOD = 1000000007
ans = 1
cnt = [3 if i == 0 else 0 for i in range(N + 1)]

for a in A:
    ans = ans * cnt[a] % MOD
    if ans == 0:
        break
    cnt[a] -= 1
    if cnt[a] < 0:
        cnt[N + 10] = 0
    cnt[a + 1] += 1

print(ans)

Solution (Python):
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  if xyz.count(Ai)>0:
    T = (T * xyz.count(Ai))%1000000007
    xyz[xyz.index(Ai)] += 1
  else:
    T = 0
    break
print(T)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))

    t = [0,0,0]

    total = 1
    for i in range(n):
        count = 0
        for j in range(3):
            if t[j] == a[i]:
                count +=1
        for j in range(3):
            if t[j] == a[i]:
                t[j] += 1
                break
        if count == 0:
            return 0
        total *= count
    return total%1000000007

if __name__ == "__main__":
    print(main())

Solution (Python):
n=int(input())
List=list(map(int,input().split()))

arr = [0,0,0]
ans = 1
MOD = 1000000007

for i in range(len(List)):
  a = List[i]
  
  b = arr.count(a)
  
  if b > 0:
    j = arr.index(a)
    arr[j] = a + 1
    
    ans = ans * b
    
  else:
    ans = 0
    break
    
print(ans%MOD)

Solution (Python):
n = int(input())

visited = [False for i in range(n)]

lst = list(map(int,input().split()))
mod = 1000000007
occur = [0]*(n+1)
occur[-1] = 3
count = 1
for i in range(n):
    count = count * occur[lst[i]-1] % mod
    occur[lst[i]-1] -= 1
    occur[lst[i]] += 1

print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
mod = 1000000007
l = [0] * 3
ans = 1

for a in A:
    
    ans *= l.count(a)
    for i in range(3):
        if l[i] == a:
            l[i] += 1
            break

print(ans% mod)

Solution (Python):
mod = 10 ** 9 + 7

N, *A = map(int, open(0).read().split())

C = [3] + [0] * N

ans = 1
for a in A:
    ans = ans * C[a] % mod
    C[a] -= 1
    C[a + 1] += 1

print(ans)

Solution (Python):
N = int(input())
A = map(int, input().split())
MOD = 1000000007
ans = 1
cnt = [3 if i == 0 else 0 for i in range(N + 1)]

for a in A:
    ans = ans * cnt[a] % MOD

    cnt[a] -= 1
    cnt[a+1] += 1

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
counts = [0 for x in range(N)]
ans = 1
for i in range(N):
	if A[i] == 0:
		ans = (ans * (3 - counts[0])) % 1000000007
		counts[0] += 1
	else:
		ans = (ans * (counts[A[i] - 1] - counts[A[i]])) % 1000000007
		counts[A[i]] += 1
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md = 10 ** 9 + 7
    n = int(input())
    aa = LI()
    cnt = [0] * 100005
    check = [0] * 100005
    zero = 0
    ans = 1
    for a in aa:
        if a == 0:
            zero += 1
        else:
            if cnt[a - 1] == 0:
                print(0)
                exit()
            ans *= cnt[a - 1]
            ans %= md
            cnt[a - 1] -= 1
        cnt[a] += 1
        check[a] += 1
        if check[a] > 3:
            print(0)
            exit()
    if zero == 1:
        ans *= 3
    else:
        ans *= 6
    if sum(cnt) == zero:
        print(ans % md)
    else:
        print(0)

main()

Solution (Python):
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)

Solution (Python):

import sys

MOD = 1000000007  


def solve(N: int, A: "List[int]"):
    if A[0] != 0:
        print(0)
        return 

    
    color = [1,0,0]
    answer = 3

    for i in range(1,N):
        if A[i] not in color:
            print(0)
            return 
        
        count = color.count(A[i])
        answer *= count

        for j in range(len(color)):
            if color[j] == A[i]:
                color[j]+=1
                break

    print(answer%MOD)
    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))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
MOD = 1000000007

memo = [0] * 3
answer = 1
for i in range(N):
    first = True
    count = 0
    for j in range(3):
        if memo[j] == A[i]:
            if first == True:
                first = False
                memo[j] += 1
            count += 1
    if count == 0:
        print(0)
        exit()
    elif count == 3:
        answer *= 3
    elif count == 2:
        answer *= 2

print(answer % MOD)

Solution (Python):
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)

Solution (Python):
def main():
    import sys
    N=int(input())
    MOD=1000000007
    
    a=list(map(int,input().split()))
    c=[3]+[0]*N
    ans=1
    for i in range(N):
        p=a[i]
        ans=ans*c[p]%MOD
        c[p]-=1
        c[p+1]+=1

    print(ans)


 
if __name__ == '__main__':
    main()

Solution (Python):
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)

Solution (Python):
n = int(input())

hats = [0, 0, 0]
count = 1
mod = 1000000007

for a in map(int, input().split()):
  count *= hats.count(a)
  count %= mod
  try:
    hats[hats.index(a)] += 1
  except ValueError:
    count = 0
    break
  
print(count)

Solution (Python):

import sys
from collections import defaultdict

N=input()
A=map(int, sys.stdin.readline().split())

S=defaultdict(lambda: 0) 
S[0]=3  

mod=10**9+7
ans=1
for x in A:
    ans*=S[x]
    ans%=mod
    S[x+1]+=1
    S[x]-=1

print ans%mod

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
MOD=1000000007



cnt = [0] * (N + 1)
cnt[0] = 3

cmb = 1
for Ai in A:
  
  
  cmb = (cmb * cnt[Ai]) % MOD
  
  
  
  cnt[Ai] -= 1
  
  cnt[Ai+1] += 1

print(cmb)

Solution (Python):
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)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

dif_colors = [0]*3
patterns = []

for i in range(N):
    cnt = 0
    tmp_color = -1
    for j in range(3):
        if dif_colors[j] == A[i]:
            cnt += 1
            tmp_color = j
    dif_colors[tmp_color] += 1
    patterns.append(cnt)

ans = 1
for pattern in patterns:
    ans *= pattern
    ans %= 1000000007

print(ans)

Solution (Python):
MOD = 10**9 + 7
N = int(input())
*A, = map(int, input().split())
hats = [0] * 3
ans = 1
for a in A:
    ans = (ans * hats.count(a)) % MOD
    for i in range(3):
        if hats[i] == a:
            hats[i] += 1
            break
print(ans)

Solution (Python):
import sys
N = int(input())
A = list(map(int, input().split()))
ans = 1
mod = 1000000007
cnt = 0
maxA = max(A)
dp = [0] * (max(A) + 1)
for i in A:
    if i == 0:
        dp[0] += 1
        cnt += 1
    else:
        if dp[i-1] <= 0:
            print(0)
            sys.exit()
        else:
            ans *= dp[i-1]
            ans %= mod
            dp[i-1] -= 1
            dp[i] += 1
if cnt == 1 and sum(dp) == 1:
    print((ans * 3)%mod)
elif cnt ==2 and sum(dp) == 2:
    print((ans * 6)%mod)
elif cnt == 3 and sum(dp) == 3:
    print((ans * 6)%mod)
else:
    print(0)

Solution (Python):
n=int(input())

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

cnt=[-1,-1,-1]

result=1

for item in s:

    temp=item-1
    if temp in cnt:
        result=result*cnt.count(temp)
        cnt[cnt.index(temp)]=cnt[cnt.index(temp)]+1
    else:
        print(0)
        exit()

print(result%1000000007)

Solution (Python):
n=int(input())
a = [int(i) for i in input().split()]
mod = 10**9+7
ans = 3

red=1
gre=0
blu=0
for i in range(1,n):
    temp=0
    if a[i] == red:
        temp+=1
    if a[i] == gre:
        temp+=1        
    if a[i] == blu:
        temp+=1
    if temp!=0:
        ans*=temp
    
        ans=ans%mod
    if a[i]==red:
        red+=1
        continue
    if a[i]==gre:
        gre+=1
        continue
    if a[i]==blu:
        blu+=1
        continue
    else:
      ans=0
    
if a[0]!=0:
  ans=0
    
print(ans)

Solution (Python):
N = int(input())
A = map(int, input().split())
MOD = 1000000007
ans = 1
cnt = [3 if i == 0 else 0 for i in range(N + 1)]
 
for a in A:
    ans = ans * cnt[a] % MOD
    if ans == 0:
        break
    cnt[a] -= 1
    cnt[a+1] += 1
 
print(ans)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]

p = 1000000007

num = [0] * N + [3]  
tot = 1
for i in range(N):
    tot = tot * (num[A[i] - 1] - num[A[i]]) % p
    num[A[i]] += 1

print(tot)

Solution (Python):
MOD = 1000000007

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

res = 1
values = [0, 0, 0]
last = 0

for e in a:
    yo = 0
    for (i, v) in enumerate(values):
        if v == e:
            yo += 1
            last = i

    if yo == 0:
        res = 0
        break


    res = (res * yo) % MOD

    values[last] += 1

print(res)

Solution (Python):
mod=1000000007

N=int(input())
A=list(map(int,input().split()))

ans=1
c1=c2=c3=0
for Ai in A:
    if c1==c2==c3==Ai:
        c1+=1
        ans=ans*3%mod
    elif c1==c2==Ai:
        c1+=1
        ans=ans*2%mod
    elif c2==c3==Ai:
        c2+=1
        ans=ans*2%mod
    elif c1==Ai:
        c1+=1
    elif c2==Ai:
        c2+=1
    elif c3==Ai:
        c3+=1
    else:
        ans=0
        break

print(ans)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
E = 10 ** 9 + 7

dp = [(1, [0, 0, 0])]
for i in range(N):
    p = dp[i][0]

    cc = 0
    sw = False
    cp = [dp[i][1][0], dp[i][1][1], dp[i][1][2]]
    for c in range(3):
        if As[i] == dp[i][1][c]:
            cc += 1
            if not sw:
                cp[c] += 1
                sw = True
    p = (p * cc) % E
    dp.append((p, cp))
print(dp[N][0])