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:
import sys
input = sys.stdin.readline
N = int(input())
rbg = [0, 0, 0]
ans = 1
A = [int(a) for a in input().strip().split()]
if A[0] != 0:
    print(0)
    exit()
for a in A:
    ans = (ans * rbg.count(a)) % (10 ** 9 + 7)
    rbg[rbg.index(a)] += 1

print(ans)

Submission status: Runtime Error

Solution code:
p =1000000007
N = int(input())
A = [int(i)for i in input().split()]

n = A.count(0)


Num = [0]*N
lis = [[0]]
ma = -1
for i in A:
    if ma+1 == i:
        lis.append([0])
        ma  += 1
    else:
        lis[i+1].append(0)
    for j in range(len(lis[i])):
        lis[i][j] += 1
lis.pop(0)
n = len(lis[0])
if n ==3:
    ans =6
else:
    ans =3
for li in lis:
    if 0 in li:
        li.remove(0)
        for i in range(len(li)):
            li[i] -=1

    if 1 in li:
        li.remove(1)
        for i in range(len(li)):
            li[i] -= 1
    if len(li)==2:
        if  li[0]==li[1]==2:
            ans = (ans *2)%p
    if len(li)==3:
        if li ==[3,3,3]:
            ans = (ans*6)%p
        elif li ==[3,3,2]:
            ans = (ans*4)%p
        elif li ==[3,2,2]:
            ans = (ans*2)%p
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split())
r = {}
total = 1
for i in range(N):
  if 'A[i]' in r.keys:
    r['A[i]'] += 1
  else:
    r['A[i]'] = 1
  if A[i] == 0:
    total = total * (4-r[A[i]])
  else:
    total = total * (r['A[i] - 1'] - r['A[i]'] + 1)
total = total % (10 ** 9 + 7)
print(total)

Submission status: Runtime Error

Solution code:
n=int(input())

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

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

result=1

for item in s:

    temp=item-1
    result=result*cnt.count(temp)
    cnt[cnt.index(temp)]=cnt[cnt.index(temp)]+1

print(result%1000000007)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
3
0 0 0

Submission status: Runtime Error

Solution code:
N=int(input())
A = [int(x) for x in input().split()]

ans = 1
d={}

for a in A:
    if a not in d:
        d[a]=1
        if a==0:
            ans *= 3
        else:
            ans = ans*d[a-1]%1000000007
    else:
        d[a] += 1
        if a==0:
            ans *= (4-d[a])
        else:
            ans = ans*(d[a-1]-d[a]+1)%1000000007


print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())

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

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

result=1

for item in s:

    temp=item-1
    result=result*cnt.count(temp)
    cnt[cnt.index(temp)]=cnt[cnt.index(temp)]+1

print(result)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
rbg = [0, 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)] = rbg[rbg.index(a)] + 1

print(ans)

Submission status: Runtime Error

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

X = [-1, -1, -1]
ans = 1
for a in A:
    t = X.count(a-1)
    ans = ans * t % 1000000007
    idx = X.index(a-1)
    if idx < 0:
        print(0)
        exit()
    X[idx] += 1
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split())
r = {}
total = 1
for i in range(N):
  if A[i] in r.keys:
    r[A[i]] += 1
  else:
    r[A[i]] = 1
  if A[i] == 0:
    total = total * (4-r[A[i]])
  else:
    total = total * (r[A[i] - 1] - r[A[i]] + 1)
total = total // (10 ** 9 + 7)
print(total)

Submission status: Runtime Error

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

mod = (10**9) + 7
abc = [0] * 3
ans = 1
for i in range(N):
	pos = abc.count(A[i])
	ind = abc.index(A[i])
	ans *= pos
	ans %= mod
	abc[ind] += 1

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
const = 1000000007
n = int(input())
As = list(map(int,input().split()))
ans = 1
dict = {0:0}
zero = 3
for a in As:
  if a==0:
    ans *= zero
    dict[0] += 1
    zero -= 1
  else:
    if a in dict:
      dict[a] = dict[a] + 1
    else:
      dict[a] = 1
    ans *= dict[a-1]
    ans %= const
    dict[a-1] = dict[a-1] -1
print(ans%const)

Submission status: Runtime Error

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

X = [-1, -1, -1]
ans = 1
for a in A:
    t = X.count(a-1)
    ans = ans * t % 1000000007
    X[X.index(a-1)] += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
rbg = [0, 0, 0]
ans = 1
for a in [int(a) for a in input().strip().split()]:
    ans = (ans * rbg.count(a))
    rbg[rbg.index(a)] += 1

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

Submission status: Runtime Error

Solution code:
const = 1000000007
n = int(input())
As = list(map(int,input().split()))
ans = 1
dict = {0:0}
zero = 3
for a in As:
  if a==0:
    ans *= zero
    dict[0] += 1
    zero -= 1
  else:
    if a in dict:
      dict[a] = dict[a] + 1
    else:
      dict[a] = 1
    ans *= dict[a-1]
    dict[a-1] = dict[a-1] -1
print(ans%const)

Submission status: Runtime Error

Solution code:
MOD = 1000000007

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

vec = [0, 0, 0]
num = 1

for a in A:
    idx = [i for i, x in enumerate(vec) if x == a]
    num = (num * len(idx)) % MOD
    vec[idx[0]] += 1

print(num)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
ali = list(map(int, input().split()))
mod = 10**9+7 # あと６かける
can = []
zerocount = 0
log = [0 for _ in range(len(set(ali)))]
for i in range(n):
    if ali[i] == 0:
        zerocount += 1
        can.append(1)
        log[0] += 1
    else:
        val = ali[i]
        can.append(log[val-1] - log[val])
        log[val] += 1

can = np.array(can)
ones = can.prod()
if zerocount == 1:
  alls = ones*3
else:
  alls = ones*6
print(alls%mod)

Submission status: Runtime Error

Solution code:
n = int(input())
s = str(input())
c1 = [False for _ in range(10)]
c2 = [0 for _ in range(1000)]
count = 0
for i in range(n-2):
  si = int(s[i])
  if c1[si] != True:
    c1[si] = True
    c3 = [False for _ in range(10)]
    for j in range(i+1,n-1):
      sj = int(s[j])
      if c3[sj] != True:
        c3[sj] = True
        c4 = [False for _ in range(10)]
        for k in range(j+1,n):
          sk = int(s[k])
          if c4[sk] != True:
            c4[sk] = True
            count += 1
print(count)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
rbg = [0, 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)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
mod = 1000000007
C = [1,0,0]
j = 1
ans = 3
for i in range(1,N):
    if A[i] == 0:
        C[j] = 1
        ans = (ans * (3-j)) % mod
        j += 1
    else:
        if C[0] == C[1] and C[1] == C[2]:
            C[0] += 1
            ans = (ans * 3) % mod
        elif C[0] == C[1] and A[i] == C[0]:
            ans = (ans * 2) % mod
            C[0] += 1
        elif C[0] == C[2] and A[i] == C[0]:
            ans = (ans * 2) % mod
            C[0] += 1
        elif C[1] == C[2] and A[i] == C[1]:
            C[1] += 1
            ans = (ans * 2) % mod
        else:
            for k in range(3):
            	if A[i] == C[k]:
                    C[k] += 1
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import combinations

MOD = 1000000007

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

vec = [0, 0, 0]
num = 1

for i, a in enumerate(A):
    idx = [i for i, x in enumerate(vec) if x == a]
    if i > 0:
        num = (num * len(idx)) % MOD
    vec[idx[0]] += 1

num_color = sum([x != 0 for x in vec])

if num_color == 1:
    c = 3
if num_color == 2:
    c = 3
if num_color == 3:
    c = 3

ans = c * num % MOD
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
mod = 1000000007
C = [1,0,0]
j = 1
ans = 3
for i in range(1,N):
    if A[i] == 0:
        C[j] = 1
        ans = ans * (3-j) % mod
        j += 1
    else:
        if C[0] == C[1] and C[2] == C[1]:
            C[0] += 1
            ans = ans * 3 % mod
        elif C[0] == C[1] and A[i] == C[0]:
            ans = ans * 2 % mod
            C[0] += 1
        elif C[0] == C[2] and A[i] == C[0]:
            ans = ans * 2 % mod
            C[0] += 1
        elif C[1] == C[2] and A[i] == C[1]:
            C[1] += 1
            ans = ans * 2 % mod
        else:
            for k in range(3):
                if A[i] == C[k]:
                    C[k] += 1
print(ans)

Submission status: Runtime Error

Solution code:
mod = 10 ** 9 + 7

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

data = [[] for _ in range(N)]

for i in range(N):
    data[A[i]].append(i)


for i in range(1,N+1):
    if len(data[-i]) == 1:
        ans = 3
    elif len(data[-i]) >= 2 and len(data[-i+1]) == 0:
        ans = 6
    elif len(data[-i]) == 2 and len(data[-i+1]) == 1:
        for j in range(2):
            count = 1
            if data[-i][1-j] < data[-i+1][0]:
                count += 1
            ans = (ans*(count - j))%mod
    elif len(data[-i]) == 3 and len(data[-i+1]) == 1:
        for j in range(3):
            count = 1
            if data[-i][2-j] < data[-i+1][0]:
                count += 1
            ans = (ans*(count - j))%mod
    elif len(data[-i]) == 2 and len(data[-i+1]) == 2:
        for j in range(2):
            count = 0
            for k in range(2):
                if data[-i][1-j] < data[-i+1][1-k]:
                    count += 1
            ans = (ans*(count - j))%mod
    elif len(data[-i]) == 3 and len(data[-i+1]) == 2:
        for j in range(3):
            count = 1
            for k in range(2):
                if data[-i][2-j] < data[-i+1][1-k]:
                    count += 1
            ans = (ans*(count - j))%mod
    elif len(data[-i]) == 3 and len(data[-i+1]) == 3:
        for j in range(3):
            count = 0
            for k in range(3):
                if data[-i][2-j] < data[-i+1][2-k]:
                    count += 1
            ans = (ans*(count - j))%mod
    #print(i,ans)

#print(data)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
As = list(map(int, input().split()))
dp = [0] * 100000
mod = 10**9 + 7
ans = 1
dp[0] = 3
for a in As:
    a += 1
    ans *= dp[a-1]-dp[a]
    ans %= mod
    dp[a] += 1
print(ans%mod)

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
L=[]
mod = 1000000007
import collections
ans=1
for i in range(n):
  c=collections.Counter(L)
  c=c.most_common()
  
  if A[i]==0:
    if 0 in L:
      ans*=3-c[0][1]
    else:
      ans *=3
  else:
    if A[i] in L:
      ans *=(c[A[i]-1][1] - c[A[i]][1])%mod
    else:
      ans*=(c[A[i]-1][1])%mod
  L.append(A[i])
print(ans%mod)

Submission status: Runtime Error

Solution code:
import sys
inputs = sys.stdin.readlines()
from functools import reduce
from operator import mul

n = int(inputs[0])
A = list(map(int, inputs[1].split()))
B = []
C = []
mod = 1000000007
for i in range(n):
    nb_rgb = [
        len([x for x in B if x == 0]),
        len([x for x in B if x == 1]),
        len([x for x in B if x == 2])
    ]
    tmp = [int(nb == A[i]) for nb in nb_rgb]
    if sum(tmp) == 1:
        B.append(tmp.index(1))
    elif sum(tmp) == 2:
        B.append(tmp.index(1))
        C.append(2)
    elif sum(tmp) == 3:
        B.append(tmp.index(1))
        C.append(3)

print(reduce(mul, C) % mod)

Submission status: Runtime Error

Solution code:
lst=[]
N=int(input())
h = map(int, input().split())
c=1
A=list(h)
B=len(A)
for i in range(B):
 if len(lst)<A[i]:
  c=0
  break
 elif len(lst)==A[i]:
  lst.append(1)
 else:
  lst[A[i]]+=1
  if lst[A[i]]==4:
   c=0
   break
 if A[i]==0:
  c*=1
 elif lst[A[i]-1]-lst[A[i]]+1<=0:
  if A[i]==1:
   c*=(4-lst[A[i]])
  else:
   c=0
   break
 else:
  c*=(lst[A[i]-1]-lst[A[i]]+1)
if lst[0]==1:
 c*=3
else:
 c*=6
print(c%1000000007)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
L=[]
mod = 1000000007
import collections
ans=1
for i in range(n):
  c=collections.Counter(L)
  c=c.most_common()
  
  if A[i]==0:
    if 0 in L:
      ans*=3-c[0][1]
    else:
      ans *=3
  else:
    if A[i] in L:
      ans *=(c[A[i]-1][1] - c[A[i]][1])%mod
    else:
      ans*=(c[A[i]-1][1])%mod
  L.append(A[i])
print(ans%mod)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
mod = 1000000007
C = [1,0,0]
j = 1
ans = 3
for i in range(1,N):
    if A[i] == 0:
        C[j] = 1
        ans = ans *(3-j) % mod
        j += 1
    else:
        if C[0] == C[1] and C[2] == C[1]:
            C[0] += 1
            ans = ans * 3 % mod
        elif C[0] == C[1] and A[i] == C[0]:
            ans = ans * 2 % mod
            C[0] += 1
        elif C[0] == C[2] and A[i] == C[0]:
            ans = ans * 2 % mod
            C[0] += 1
        elif C[1] == C[2] and C[1] != 0 and A[i] == C[1]:
            C[1] += 1
            ans = ans * 2 % mod
        else:
            for k in range(3):
                if A[i] == C[k]:
                    C[k] += 1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
# def i():
#     yield "100000"
#     a = 0
#     b = 0
#     c = 0
#     l = []
#     for i in range(100000):
#         import random
#         r = random.randint(0, 2)
#         if r == 0:
#             l.append(str(a))
#             a += 1
#         elif r == 1:
#             l.append(str(b))
#             b += 1
#         else:
#             l.append(str(c))
#             c += 1
#     yield " ".join(l)
# input = i().__next__
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    cur = 1
    nums = []
    for i in range(N):
        if A[i] == 0:
            cur *= 1
            nums.append(0)
            continue
        #A[i] != 0
        cnt = 0
        for j, n in enumerate(nums):
            if n == A[i] - 1:
                if cnt == 0:
                    if len(nums) <= j:
                        print(0)
                        return
                    nums[j] += 1
                cnt += 1
        cur *= cnt
        cur %= 10**9+7
        # print(nums)
    m = [0, 3, 6, 6]
    
    print((cur * m[len(nums)]) % (10**9+7))
main()

Submission status: Runtime Error

Solution code:
N=int(input())
S=list(map(int,input().split()))
D={}
mod=((10**9)+7)
ans=1
for i in range(N):
    if S[i]==0:
        if 0 not in D:
            D[0]=1
            ans*=3
        elif D[0]==1:
            D[0]+=1
            ans*=2
        else:
            D[0]=3
    elif S[i] not in D:
        ans*=D[S[i]-1]
        ans%mod
        D[S[i]]=1
    else:
        if D[S[i]]==1:
            ans*=(D[S[i]-1]-1)
            D[S[i]]+=1
            ans%=mod
        elif D[S[i]]==2:
            ans*=(D[S[i]-1]-2)
            D[S[i]]+=1
            ans%=mod
    #print(ans)
print(ans%mod)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    t1, t2 = LI()
    a1, a2 = LI()
    b1, b2 = LI()

    if a1 < b1:
        tmp = b1
        b1 = a1
        a1 = tmp

        tmp = b2
        b2 = a2
        a2 = tmp

    l1 = (a1 - b1) * t1
    l2 = (a2 - b2) * t2
    l3 = l1 + l2

    if l3 == 0:
        print("infinity")
        return
    if l3 > 0:
        print(0)
        return
    ans = 1
    if l1 % (-l3) == 0:
        print(ans + 2 * (l1 // (-l3)) - 1)
        return
    print(ans + 2 * (l1 // (-l3)))






if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# def i():
#     yield "100000"
#     a = 0
#     b = 0
#     c = 0
#     l = []
#     for i in range(100000):
#         import random
#         r = random.randint(0, 2)
#         if r == 0:
#             l.append(str(a))
#             a += 1
#         elif r == 1:
#             l.append(str(b))
#             b += 1
#         else:
#             l.append(str(c))
#             c += 1
#     yield " ".join(l)
# input = i().__next__
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    cur = 1
    nums = []
    for i in range(N):
        if A[i] == 0:
            cur *= 1
            nums.append(0)
            continue
        #A[i] != 0
        cnt = 0
        for j, n in enumerate(nums):
            if n == A[i] - 1:
                if cnt == 0:
                    nums[j] += 1
                cnt += 1
        cur *= cnt
        cur %= 10**9+7
        # print(nums)
    m = [0, 3, 6, 6]
    
    print((cur * m[len(nums)]) % (10**9+7))
main()

Submission status: Runtime Error

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

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=list(map(int,input().split()))
D={}
mod=((10**9)+7)
ans=1
for i in range(N):
    if S[i]==0:
        if 0 not in D:
            D[0]=1
            ans*=3
        elif D[0]==1:
            D[0]+=1
            ans*=2
        else:
            D[0]=3
    elif S[i] not in D:
        ans*=D[S[i]-1]
        ans%mod
        D[S[i]]=1
    else:
        if D[S[i]]==1:
            ans*=(D[S[i]-1]-1)
            D[S[i]]+=1
            ans%=mod
        elif D[S[i]]==2:
            ans*=(D[S[i]-1]-2)
            D[S[i]]+=1
            ans%=mod
    #print(ans)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
L=[]
mod = 1000000007
for i in range(3):
  L.append(-1)
import collections
ans=1
for i in range(n):
  c = collections.Counter(L)
  c=c.most_common()
  c.sort()
  if A[i] in L:
    ans *=(c[A[i]][1] - c[A[i]+1][1])
  else:
    ans*=(c[A[i]][1])
  L.append(A[i])
print(ans%mod)

Submission status: Runtime Error

Solution code:
N = int(input())
Alist = list(map(int, input().split()))
abc = [0, 0, 0]
ans = 1
for i in range(N):
    A = Alist[i]
    ans = (ans * abc.count(A)) % (10**9 + 7)
    abc[abc.index(A)] += 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = [int(a) for a in input().strip().split()]

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
mod= 10**9+7
C = [1,0,0]
j = 1
ans = 3
for i in range(1,N):
    if A[i] == 0:
        C[j] = 1
        ans = ans*(3-j) % mod
        j += 1
    else:
        if C[0] == C[1] and C[2] == C[1]:
            C[0] += 1
            ans = ans * 3 % mod
        elif C[0] == C[1] and A[i] == C[0]:
            ans = ans * 2 % mod
            C[0] += 1
        elif C[0] == C[2] and A[i] == C[0]:
            ans = ans * 2 % mod
            C[0] += 1
        elif C[1] == C[2] and C[1] != 0 and A[i] == C[1]:
            C[1] += 1
            ans = ans * 2 % mod
        else:
            for k in range(3):
                if A[i] == C[k]:
                    C[k] += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(item) for item in input().split()]

mod = 1000000007
color = [0, 0, 0]
combination = 1

for i, num in enumerate(a):
    combination *= color.count(num)
    if combination >= mod:
        combination = combination % mod
    color[color.index(num)] += 1

print(combination % mod)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#coding:utf-8
import bisect
from collections import deque
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = True

def index(a,b):
    return str(min(a,b)) + "," +  str(max(a, b))

def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    n = II()
    a = LMIIS()

    if n == 1:
        print(3)
        exit()
    elif n == 2 and a[1] == 0:
        print(3)
    elif n == 2 and a[1] == 1:
        print(6)

    if a[1] == 0 and a[2] == 0:
        x = 1
        y = 1
        z = 1
        res = 1
    elif a[1] == 0 and a[2] == 1:
        x = 2
        y = 1
        z = 0
        res = 2
    elif a[1] == 1 and a[2] == 2:
        x = 3
        y = 0
        z = 0
        res = 1

    MOD = 10**9+7

    for i in range(3, n):
        if a[i] == x and a[i] != y:
            x += 1
        elif a[i] == x and a[i] == y and a[i] != z:
            res = res*2 % MOD
            x += 1
        elif a[i] == x and a[i] == y and a[i] == z:
            res = res*3 % MOD
            x += 1
        elif a[i] == y and a[i] != z:
            y += 1
        elif a[i] == y and a[i] == z:
            res = res*2 % MOD
            y += 1
        else:
            z += 1

    if y == 0:
        res = res* 3 % MOD
    else:
        res = res* 6 % MOD
    print(res)



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

import sys
sys.setrecursionlimit(10000000)

N=int(input())

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


c=[0,0,0]
ans=1

for i in range(N):
    s=sum([c[j]==A[i] for j in range(0,3)])
    c[c.index(A[i])]+=1
    ans=(ans*s)%1000000007

print(ans%1000000007)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

rgb = [0, 0, 0]
rst = 1
mod = 1000000007

for i in a:
    cnt = rgb.count(i)
    idx = rgb.index(i)

    if cnt == 0 or (idx < 0 or 2 < idx):
        rst = 0
        break

    rgb[idx] += 1

    rst *= cnt
    rst %= mod

print(rst)

Submission status: Runtime Error

Solution code:
n=int( input() )
a=[int(i)+1 for i in input().split()]

c=[0,0,0]
ans=1
mod=1000000007
for aa in a:
    ans = ans*c.count(aa-1) %mod
    c[c.index(aa-1)]+=1
print(ans)

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

Solution code:
lst=[]
N=int(input())
h = map(int, input().split())
c=1
A=list(h)
B=len(A)
for i in range(B):
 if len(lst)==A[i]:
  lst.append(1)
 else:
  lst[A[i]]+=1
 if A[i]==0:
  c*=1
 else:
  c*=(lst[A[i]-1]-lst[A[i]]+1)
if lst[0]==1:
 c*=3
else:
 c*=6
print(c%1000000007)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = [int(a) for a in input().strip().split()]

rbg = [1, 0, 0]
ans = 1
multi = 1
for a in A[1:]:
    ans = (ans * rbg.count(a)) % (10 ** 9 + 7)
    rbg[rbg.index(a)] += 1

if rbg.count(0) == 2:
    print(ans % (10 ** 9 + 7))
else:
    print(ans * 3 % (10 ** 9 + 7))

Submission status: Runtime Error

Solution code:
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

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

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

phase = 0
now = [0,0,0]
ans=1

for i in range(n):
    if(a[i]==0):
        now[phase]=1
        ans*=3-phase
        ans%=1000000007
        phase+=1
    else:
        corr = []
        for j in range(3):
            if(now[j]==a[i]):
                corr.append(j)
        ans*=len(corr)
        ans%=1000000007
        now[corr[0]]+=1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(item) for item in input().split()]

color = [0, 0, 0]
combination = 1

for i, num in enumerate(a):
    combination *= color.count(num)
    color[color.index(num)] += 1

print(combination % 1000000007)

Submission status: Runtime Error

Solution code:
N=int(input())

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


c=[0,0,0]
ans=1

for i in range(N):
    s=sum([c[j]==A[i] for j in range(0,3)])
    c[c.index(A[i])]+=1
    ans=(ans*s)%1000000007

print(ans%1000000007)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
A = rl()
color = [0] * 3
answer = 1
MOD = 10 ** 9 + 7

for a in A:
    answer *= color.count(a)
    i = color.index(a)
    color[i] += 1

print(answer%MOD)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = [int(a) for a in input().strip().split()]

rbg = [1, 0, 0]
ans = 1
multi = 1
for a in A[1:]:
    ans *= rbg.count(a)
    rbg[rbg.index(a)] += 1

if rbg.count(0) == 2:
    print(ans % (10 ** 9 + 7))
else:
    print(ans * 3 % (10 ** 9 + 7))

Submission status: Runtime Error

Solution code:


N=int(input())

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


c=[0,0,0]
ans=1

for i in range(N):
    s=sum([c[j]==A[i] for j in range(0,3)])
    c[c.index(A[i])]+=1
    ans=ans*s

print(ans%1000000007)

Submission status: Runtime Error

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

mul = 3 if L[-1] == n-1 else 6
MOD = 1000000007

ans = 1
available = []
for elm in L:
    if elm == 0:
        available.append(elm)
    else:
        ans *= available.count(elm-1)
        available.remove(elm-1)
        available.append(elm)
        ans = ans % MOD

ans = (ans * mul) % MOD
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
A = rl()
color = [0] * 3
answer = 1
MOD = 10 ** 9 + 7

for a in A:
    answer *= color.count(a)
    i = color.index(a)
    color[i] += 1
    answer %= MOD

print(answer)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

rgb = [0, 0, 0]
rst = 1
mod = 1000000007

for i in a:
    cnt = rgb.count(i)
    idx = rgb.index(i)
    rgb[idx] += 1

    rst *= cnt
    rst %= mod

print(rst)

Submission status: Runtime Error

Solution code:
# def i():
#     yield "100000"
#     yield " ".join([str(i) for i in range(100000)])
# input = i().__next__
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    cur = 1
    nums = []
    for i in range(N):
        if A[i] == 0:
            cur *= 1
            nums.append(0)
            continue
        #A[i] != 0
        cnt = 0
        for j, n in enumerate(nums):
            if n == A[i] - 1:
                if cnt == 0:
                    nums[j] += 1
                cnt += 1
        cur *= cnt
        cur %= 10**9+7
        # print(nums)
    m = [3, 6, 6]
    print((cur * m[len(nums)-1]) % (10**9+7))
main()