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



Sample input:
6
0 1 2 3 4 5


Sample output:
3

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))

ans=1

d={}

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
      else:
        d[0]=3
      
  else:
    if a[i] not in d:
      d[a[i]]=1
      ans*=d[a[i]-1]
    else:
      if d[a[i]]==1:
        d[a[i]]=2
        ans*=d[a[i]-1]-1
      else:
        d[a[i]]=3
        ans*=1
        
  ans%=mod
  
print(ans)

Submission status: Runtime Error

Solution code:
mod=10**9+7
n=int(input())
l=list(map(int,input().split()))
ans=1
rgb=[0,0,0]
for i in range(n):
    c=0
    for j in range(3):
        if l[i]==rgb[j]:
            c+=1
    p=rgb.index(l[i])
    if not 0<=p<=2:
        print(0)
        exit()
    rgb[p]+=1
    ans*=c
    ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
mod=10**9+7
n=int(input())
l=list(map(int,input().split()))
ans=1
rgb=[0,0,0]
for i in range(n):
    c=0
    for j in range(3):
        if l[i]==rgb[j]:
            c+=1
    p=rgb.index(l[i])
    rgb[p]+=1
    ans*=c
    ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
mod=10**9+7
n=int(input())
l=list(map(int,input().split()))
ans=1
rgb=[0,0,0]
for i in range(n):
    for j in range(3):
        c=0
        if l[i]==rgb[j]:
            c+=1
    p=rbg.index(l[i])
    rgb[p]+=1
    ans*=c
    ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
mod=10**9+7
n=int(input())
l=list(map(int,input().split()))
ans=1
rgb=[0,0,0]
for i in range(n):
    for j in range(3):
        c=0
        if l[i]==rgb[0]:
            c+=1
    p=rbg.index(l[i])
    rgb[p]+=1
    ans*=c
    ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n=int(input())
    lst=list(map(int,input().split()))
    mod=10**9+7
    sm=1
    color=[0,0,0]
    for x in lst:
        sm*=color.count(x)
        sm%=mod
        color[color.index(x)]+=1
    print(sm)

main()

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def mapline(t = int):
    return map(t, sysread().split())
def mapread(t = int):
    return map(t, read().split())

def run():
    N, *A = mapread()
    ans = 1
    current = [0,0,0]
    for a in A:
        transition = 0
        cache = INF
        for i, c in enumerate(current):
            if c == a:
                transition += 1
                cache = i
        ans *= transition
        ans %= mod
        current[cache] += 1
        #print(current)
    print(ans)

if __name__ == "__main__":
    run()

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def mapline(t = int):
    return map(t, sysread().split())
def mapread(t = int):
    return map(t, read().split())

def run():
    N, *A = mapread()
    ans = 1
    current = [0,0,0]
    for a in A:
        transition = 0
        #cache = 1
        for i, c in enumerate(current):
            if c == a:
                transition += 1
                cache = i
        ans *= transition
        ans %= mod
        current[cache] += 1
        #print(current)
    print(ans)

if __name__ == "__main__":
    run()

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def mapline(t = int):
    return map(t, sysread().split())
def mapread(t = int):
    return map(t, read().split())

def run():
    N, *A = mapread()
    ans = 1
    current = [0,0,0]
    for a in A:
        transition = 0
        cache = None
        for i, c in enumerate(current):
            if c == a:
                transition += 1
                cache = i
        ans *= transition
        ans %= mod
        current[cache] += 1
        #print(current)
    print(ans)

if __name__ == "__main__":
    run()

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
MOD = 10**9 + 7
ans = 1

lst = [0] * 3

for aa in a:
    cnt = 0
    for i, l in enumerate(lst):
        if l == aa:
            cnt += 1
            j = i
    ans *= cnt
    ans %= MOD
    lst[j] += 1
print(ans%MOD)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
MOD = 10**9 + 7
ans = 1

lst = [0] * 3

for aa in a:
    cnt = 0
    for i, l in enumerate(lst):
        if l == aa:
            cnt += 1
            j = i
    ans *= cnt
    lst[j] += 1
print(ans%MOD)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def mapline(t = int):
    return map(t, sysread().split())
def mapread(t = int):
    return map(t, read().split())

def run():
    N, *A = mapread()
    ans = 1
    current = [0,0,0]
    for a in A:
        transition = 0
        cache = None
        for i, c in enumerate(current):
            if c == a:
                transition += 1
                cache = i
        ans *= transition
        ans %= mod
        current[cache] += 1
    print(ans)

if __name__ == "__main__":
    run()

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def mapline(t = int):
    return map(t, sysread().split())
def mapread(t = int):
    return map(t, read().split())

def run():
    N, *A = mapread()
    ans = 1
    current = [0,0,0]
    for a in A:
        transition = 0
        cache = None
        for i, c in enumerate(current):
            if c == a:
                transition += 1
                cache = i
        ans *= transition
        ans %= mod
        current[cache] += 1
    print(ans)


if __name__ == "__main__":
    run()

Submission status: Runtime Error

Solution code:
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))

######################################################

N,A=ilint()
C=counter()
C.add(-1,3)
ans=1
for a in A:
    C.add(a,1)
    ans*=C[a-1]-C[a]+1
    ans%=MOD
print(ans)

Submission status: Runtime Error

Solution code:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C={0:1}
ans = 3
for i in range(1,N):
    a = A[i]
    if a not in C:
        C[a]=0
    C[a] += 1
    if C[a]==1:
        ans = (ans*C[a-1])%p
    elif a==0 and C[a]==2:
        ans = ans*2
    elif a>0 and C[a]==2:
        ans = (ans*(C[a-1]-1))%p
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))

hat = [0,0,0]

mod = 10**9 + 7

ans=1
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

for i in range(n):
    arr=my_index_multi(hat,a[i])

    ans = (ans*len(arr))%mod

    hat[arr[0]]+=1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))

hat = [0,0,0]

mod = 10**9 + 7
ans=1

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

for i in range(n):
    arr=my_index_multi(hat,a[i])

    ans = ans*len(arr)%mod

    hat[arr[0]]+=1

print(ans)

Submission status: Runtime Error

Solution code:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ans = 3
C = {0:1}
for i in range(1,N):
    a = A[i]
    if a not in C:
        C[a] = 0
    C[a] += 1
    if C[a]==1:
        ans = (ans*C[a-1])%p
    elif a>0 and C[a]==2:
        ans = (ans*(C[a-1]-1))%p
    elif a==0 and C[a]==2:
        ans = (ans*2)%p
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
mod=10**9+7

dp=[3]
col=[1]
for i in range(1,N):
    if A[i]==0:
        col.append(1)
        dp.append(dp[-1]*(4-len(col)))
    else:
        p=0
        for c in range(len(col)):
            if col[c]==A[i]:
                if p==0:
                    col[c]+=1
                p+=1
        dp.append(dp[-1]*p%mod)
        if p==0:
            print(A[N])
print(dp[-1]%mod)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
mod=10**9+7

dp=[3]
col=[1]
for i in range(1,N):
    if A[i]==0:
        col.append(1)
        dp.append(dp[-1]*(4-len(col)))
    else:
        dp.append(dp[-1]*col.count(A[i])%mod)
        col[col.index(A[i])]+=1
print(dp[-1]%mod)

Submission status: Runtime Error

Solution code:
n,*a = map(int,open(0).read().split())
cnt = [3]+[0]*(n-2)
ans = 1
mod = 10**9+7
for i in a:
  ans = ans * cnt[i] % mod
  cnt[i] -= 1
  cnt[i+1] += 1
print(ans)

Submission status: Runtime Error

Solution code:
n,*a = map(int,open(0).read().split())
b = [0,0,0]
ans = 1
mod = 10**9+7
for i in a:
  ans = (ans * b.count(i)) % mod
  b.remove(i)
  b.append(i + 1)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
*A, = map(int, input().split())
mod = 10**9 + 7

col = [0, 0, 0]
ans = 1
for a in A:
    ans = ans * col.count(a) % mod
    col[col.index(a)] += 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
a_ls = list(map(int, input().split()))
hat_nperson = [0,0,1]
ans = 3
mod = 10**9+7
if a_ls[0] != 0 or a_ls[1] == 2:
    print(0)
    sys.exit()
for i in range(1,n):
    n_same = a_ls[i]
    n_choices = 0
    choice_ind = -1
    for j,person in enumerate(hat_nperson):
        if person == n_same:
            n_choices += 1
            choice_ind = j
    if choice_ind == -1:
        print(0)
        sys.exit()
    hat_nperson[choice_ind] += 1
    ans *= n_choices
    ans %= mod
print(ans%mod)

Submission status: Runtime Error

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
hats_person = [1,0,0]
ans = 3
mod = 10**9+7
for i in range(1,n):
    same = a_ls[i]
    n_choices = 0
    choice_ind = -1
    for j,person in enumerate(hats_person):
        if person == same:
            n_choices += 1
            choice_ind = j
    if choice_ind == -1:
        print(0)
        sys.exit()
    hats_person[choice_ind] += 1
    ans *= n_choices
    ans %= mod
print(ans%mod)

Submission status: Runtime Error

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
hats_person = [1,0,0]
ans = 3
mod = 10**9+7
for i in range(1,n):
    same = a_ls[i]
    n_choices = 0
    #choice_ind = -1
    for j,person in enumerate(hats_person):
        if person == same:
            n_choices += 1
            choice_ind = j
    #if choice_ind == -1:
        #ans = 0
    hats_person[choice_ind] += 1
    ans *= n_choices
    ans %= mod
print(ans)

Submission status: Runtime Error

Solution code:
import sys,math,collections,itertools
input = sys.stdin.readline

N=int(input())
A=list(map(int,input().split()))
X = [0,0,0]

m = 10**9+7
patern = 1

for a in A:
    patern *= X.count(a)
    patern %= m
    X[X.index(a)]+=1
    

print(patern % m)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
mod = 10**9 + 7
mem = [0, 0, 0]
ans = 1
for i in range(n):
    ans *= mem.count(A[i])
    ans %= mod
    idx = mem.index(A[i])
    mem[idx] += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
mod = 10**9 + 7
mem = [1, 0, 0]
ans = 3
for i in range(1, n):
    ans *= mem.count(A[i])
    ans %= mod
    mem[mem.index(A[i])] += 1
print(ans)

Submission status: Runtime Error

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    """
    """
    N = read_int()
    A = read_ints()
    T = [0, 0, 0]
    answer = 1
    modulo = 1000000007
    for a in A:
        answer = (answer*T.count(a))%modulo
        T[T.index(a)] += 1
    return answer


if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
c=[0,0,0]
for i in range(n):
  ans*=c.count(a[i])
  c[c.index(a[i])]+=1
print(ans%1000000007)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
c=[0,0,0]
for i in range(n):
  ans*=c.count(a[i])
  ans%=1000000007
  c[c.index(a[i])]+=1
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1
c=[0,0,0]
for i in range(n-1):
  ans*=c.count(a[i])
  ans%=1000000007
  c[c.index(a[i])]+=1
print(ans)

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

    cnt = [0] * 3
    ans = 1

    for x in a:
        p = cnt.count(x)
        ans *= p
        ans %= MOD
        cnt[cnt.index(x)] += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
mod = 10**9 + 7

def main():
    n = int(input())
    A = list(map(int, input().split()))
    candidates = [0] * n
    candidates[0] = 3
    ans = 1

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
mod = 1000000007
ans = 1
cnt = [3 for 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)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
data = [0]*3
ans = 1
mod = pow(10,9)+7
for i in A:
    ans *= data.count(i)
    ans %= mod
    data[data.index(i)] += 1
print(ans)

Submission status: Runtime Error

Solution code:
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)
  b[b.index(i)]+=1
print(ans%mod)

Submission status: Runtime Error

Solution code:
mod = 10 ** 9 + 7
cnt = [0] * 3
N = int(input())
A = list(map(int,input().split()))

ans = 1
for i in range(N):
    ans *= cnt.count(A[i])
    ans %= mod
    cnt[cnt.index(A[i])] += 1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=10**9+7
b=[0]*3
ans=1
for i in a:
  ans*=b.count(i)
  ans%=m
  b[b.index(i)]+=1
print(ans%m)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=10**9+7
b=[-1]*3
ans=1
for i in a:
  ans*=b.count(i-1)
  ans%=m
  b[b.index(i-1)]+=1
print(ans%m)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=10**9+7
b=[-1]*3
ans=1
for i in range(0,n):
  j=a[i]
  ans*=(b.count(j-1))
  ans%=m
  b[b.index(j-1)]+=1
print(ans%m)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=10**9+7
b=[-1]*3
ans=1
for i in range(0,n):
  j=a[i]
  ans*=(b.count(j-1))
  b[b.index(j-1)]+=1
print(ans%m)

Submission status: Runtime Error

Solution code:
N,*A = map(int, open(0).read().split())
group = [0] * 3
ans = 1
for x in A:
    ans = (ans*group.count(x)) % (10**9+7)
    group[group.index(x)] += 1
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
*a,=map(int,input().split())
mod=10**9+7
col=[0]*n
col[0]=3

ans=1

for i in range(n):
    ans*=col[a[i]]
    col[a[i]]-=1
    col[a[i]+1]+=1
    ans%=mod

print(ans%mod)

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N = in_n()
    A = in_nl()

    mod = 10**9 + 7
    num = [0] * 3
    ans = 1

    for i in range(N):
        min_j = 4
        count = 0
        for j in range(3):
            if num[j] == A[i]:
                count += 1
                min_j = min(min_j, j)
        num[min_j] += 1
        ans *= count
        ans %= mod

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
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
	cnt[cnt.index(A[i])] += 1

print(ans)

Submission status: Runtime Error

Solution code:
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)
    ind = C.index(a)
    C[ind] += 1
    Ans = (Ans*ask)%MOD
print(Ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    from functools import reduce
    xyz_list = [[0]*3]
    for a in A:
        xyz_list.append(xyz_list[-1][:])
        xyz_list[-1][xyz_list[-1].index(a)] += 1
    T = [xyz.count(a) for xyz, a in zip(xyz_list, A)]
    return reduce(lambda a,b: a*b%MOD, T)

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
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]"
    print(solve(N, A))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0).read().split())
mod=10**9+7
l=[0]*n
l[0]=3
ans=1
for ai in a:
  ans*=l[ai]
  ans%=mod
  l[ai]-=1
  l[ai+1]+=1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7
C = [0] * n
C[0] = 3
ans = 1
for i in a:
    ans = ans * C[i] % MOD
    C[i] -= 1
    C[i+1] += 1
print(ans % MOD)

Submission status: Runtime Error

Solution code:
N = int(input())
Ali = list(map(int,input().split()))
Ali = Ali[::-1]
Bli = sorted(Ali)
C = [-1,-1,-1]

for i in range(Ali.count(0)):
  C[i] = Bli[-i-1]+1
  
if Ali.count(0) <= 2:
  r = 3
else:
  r = 1

for i in range(N):
  n = 0
  ta = 0 
  for j in range(3):
    if C[j]-1 == Ali[i]:
      n += 1
      ta = j
  r *= n
  C[ta] = Ali[i]
  
print(r%1000000007)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 1
MOD = 1000000007
c = [0, 0, 0]
for a in A:
    cnt = c.count(a)
    ans *= cnt
    ans %= MOD
    c[c.index(a)] += 1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
l=[0,0,0]
ans=1
mod=10**9+7
for i in list(map(int,input().split())):
    ans=ans*l.count(i)%mod
    l[l.index(i)]+=1
print(ans)

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:

import collections
n=int(input())
a=list(map(int, input().split()))
b=[]
if a.count(0)==1:
    ans=3
elif a.count(0)==2:
    ans=6
    for i in a:
        k=b.count(i)#その数が何個あるか
        s=b.count(i-1)
        if i>0 and s-k==2:
            ans*=2
            ans%=1000000007
        b.append(i)

elif a.count(0)==3:
    ans=6
    for i in a:
        k=b.count(i)#その数が何個あるか
        s=b.count(i-1)
        if i>0 and s-k==3:
            ans*=3
            
        elif i>0 and s-k==2:
            ans*=2
        b.append(i)
        ans%=1000000007

if a.count(collections.Counter(a).most_common()[0][0])>=4:
    ans=0

print(ans%1000000007)

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())
num = {i:0 for i in range(N+1)}
ans = 1
mod = 1000000007
for a in A:
    s += str(a)
    if a == 0:
        if a not in num.keys():
            num[a] = 1
            ans *= 3 %mod
        else:
            if 1 not in num.keys():
                ans *= (3 - num[a]) %mod
            num[a] += 1
    else:
        if a not in num.keys():
            num[a] = 1
            ans *= num[a-1] %mod
            num[a-1] -= 1
        else:
            num[a] += 1
            ans *= num[a-1] %mod
            num[a-1] -= 1
    #print(s, num, ans%mod)
print(ans%mod)

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())
num = {}
ans = 1
mod = 1000000007
for a in A:
    s += str(a)
    if a == 0:
        if a not in num.keys():
            num[a] = 1
            ans *= 3 %mod
        else:
            if 1 not in num.keys():
                ans *= (3 - num[a]) %mod
            num[a] += 1
    else:
        if a not in num.keys():
            num[a] = 1
            ans *= num[a-1] %mod
            num[a-1] -= 1
        else:
            num[a] += 1
            ans *= num[a-1] %mod
            num[a-1] -= 1
    #print(s, num, ans%mod)
print(ans%mod)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int ,input().split()))
num = {}
ans = 1
mod = 1000000007
s = ''
for a in A:
    s += str(a)
    if a == 0:
        if a not in num.keys():
            num[a] = 1
            ans *= 3 %mod
        else:
            if 1 not in num.keys():
                ans *= (3 - num[a]) %mod
            num[a] += 1
    else:
        if a not in num.keys():
            num[a] = 1
            ans *= num[a-1] %mod
            num[a-1] -= 1
        else:
            num[a] += 1
            ans *= num[a-1] %mod
            num[a-1] -= 1
    #print(s, num, ans%mod)
print(ans%mod)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=1
mod=1000000007
me=[0,0,0]
now=3
for x in l:
  if x==0:
    ans*now
    now-=1
    if now<0:
      print(0)
      quit()
    me[-now]=1
  else:
    a=len([i for i, y in enumerate(me) if y == x])
    if a>3:
      print(0)
      quit()
    ans=ans*a%mod    
    me[me.index(x)]+=1
if now==0:
  print(6*ans%mod)
else:
  print(3*ans%mod)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=1
mod=1000000007
me=[0,0,0]
now=3
for x in l:
  if x==0:
    ans*now
    now-=1
    if now<0:
      print(0)
      quit()
    me[-now]=1
  else:
    a=len([i for i, y in enumerate(me) if y == x])
    if a>2:
      print(0)
      quit()
    ans=ans*a%mod    
    me[me.index(x)]+=1
if now==0:
  print(6*ans%mod)
else:
  print(3*ans%mod)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=1
mod=10**9+7
me=[0,0,0]
now=3
for x in l:
  if x==0:
    ans*now
    now-=1
    if now<0:
      print(0)
      quit()
    me[-now]=1
  else:
    ans=ans*len([i for i, y in enumerate(me) if y == x])%mod
    me[me.index(x)]+=1
if now==0:
  print(6*ans%mod)
else:
  print(3*ans%mod)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=1
mod=10**9+7
me=[0,0,0]
now=3
for x in l:
  if x==0:
    ans*now
    now-=1
    me[-now]=1
  else:
    ans=ans*len([i for i, y in enumerate(me) if y == x])%mod
    me[me.index(x)]+=1
if now==0:
  print(6*ans%mod)
else:
  print(3*ans%mod)

Submission status: Runtime Error

Solution code:
#from math import *
N=int(input())
A=list(map(int,input().split()))
l=[0 for _ in range(3)]
ans=1
p=10**9+7
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)
    l[ind[0]]=l[ind[0]]+1
    ans=(ans*cnt)%p
    #print(l,ans)
    #input()
print(ans%p)

Submission status: Runtime Error

Solution code:
#from math import *
N=int(input())
A=list(map(int,input().split()))
l=[[],[],[]]
ans=1
p=10**9+7
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)
    l[ind[0]].append(piv)
    ans=(ans*cnt)%p
    #print(l,ans)
    #input()
print(ans)

Submission status: Runtime Error

Solution code:
from math import *
N=int(input())
A=list(map(int,input().split()))
l=[[],[],[]]
ans=1
p=10**9+7
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)
    l[ind[0]].append(piv)
    ans=(ans*cnt)%p
    #print(l,ans)
    #input()
print(ans)