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: Wrong Answer

Solution code:
mod = 1000000007

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

d = {-1:3}
ans = 1
for a in A:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
    if a-1 in d:
        ans = ans*d[a-1]%mod
        d[a-1] -= 1

#print(d)
print(ans)

Submission status: Wrong Answer

Solution code:
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
      else:
        d[0]=3
      
  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
      else:
        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
        
  ans=(ans%mod)
  
if ch==0:
  print(ans)
else:
  print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

m = 10**9+7
r, b, g = 0, 0, 0
ans = 1

for aa in a:
    if aa == r == b == g:
        ans *= 3
    elif aa == r == b or aa == b == g or aa == g == r:
        ans *= 2
    ans %= m

    aa += 1
    if aa > r:
        r = aa
    elif aa > b:
        b = aa
    elif aa > g:
        g = aa

print(ans%m)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
G = [0] * 3
G[0] = 1
cnt = 0
ans = 1
for i in range(1, N):
    groups = []
    for j, g in enumerate(G):
        if g == A[i]:
            groups.append(j)
    
    if len(groups) == 0:
        ans = 0
        break
    else:
        if A[i] != 0:
            ans *= len(groups)
        G[groups[0]] += 1
        ans = ans % mod

k = len(set(G))
if k == 3:
    ans *= 3
    ans = ans % mod
else:
    ans *= 6
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())

A=list(map(int, input().split()))
p=10**9+7


dp=[0]*N
dp[0]=3
counts=[0]*N #前が何人か(自分が＊人目)
counts[0]=1
for i in range(1,N):
    a=A[i]
    if a==0:
        dp[i]=(dp[i-1]*(3 - counts[a]))%p
    else:
        dp[i]=(dp[i-1]*(counts[a-1] - counts[a]))%p
    

    counts[a]+=1

print(dp[-1])

Submission status: Wrong Answer

Solution code:
N=int(input())

A=list(map(int, input().split()))
p=10**9+7


dp=[0]*N
dp[0]=3
counts=[0]*N #前が何人か(自分が＊人目)
counts[0]=1
for i in range(1,N):
    a=A[i]
    dp[i]=(dp[i-1]*(counts[a-1] - counts[a]))%p
    

    counts[a]+=1

print(dp[-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C={i:0 for i in range(N)}
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
    elif C[a]>3:
        ans = 0
if A[0]!=0:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C={i:0 for i in range(N)}
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
    elif C[a]>3:
        ans = 0
print(max(ans,0))

Submission status: Wrong Answer

Solution code:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C={i:0 for i in range(N)}
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
    elif C[a]>3:
        ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C={i:0 for i in range(N)}
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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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)

print(dp[-1]%mod)

Submission status: Wrong Answer

Solution code:
# なんか雑に書いたらサンプル3通ったしこれでよさそう
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 3
d = [0] * (n + 10)
d[0] = 2
d[1] = 1
for i in range(1, n):
    ans *= d[a[i]]
    d[a[i]] -= 1
    d[a[i] + 1] += 1
    ans %= mod
if ans < 0:
    print(10 / 0)
print(ans)

Submission status: Wrong Answer

Solution code:
# なんか雑に書いたらサンプル3通ったしこれでよさそう
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 3
d = [0] * (n + 10)
d[0] = 2
d[1] = 1
for i in range(1, n):
    if d[a[i]] == 0 and d[a[i] + 1] == 0:
        print(0)
        exit()

    ans *= d[a[i]]
    d[a[i]] -= 1
    d[a[i] + 1] += 1
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
# なんか雑に書いたらサンプル3通ったしこれでよさそう
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 3
d = [0] * (n + 10)
d[0] = 2
d[1] = 1
for i in range(1, n):
    ans *= d[a[i]]
    d[a[i]] -= 1
    d[a[i] + 1] += 1
    ans %= mod
print(ans)

Submission status: Wrong Answer

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

cnt = [0]*n
ans = 1

for i in a:
    if i>0: ans *= max(0,cnt[i-1] - cnt[i])
    cnt[i] += 1
    ans %= mod

ans *= 6 if cnt[0]==3 else 3

print(ans%mod)

Submission status: Wrong Answer

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

cnt = [0]*n
ans = 1

for i in a:
    if i>0: ans *= max(0,cnt[i-1] - cnt[i])
    cnt[i] += 1
    ans %= mod

if cnt[0]==1:
    ans *= 3
    ans %= mod
else:
    ans *= 6
    ans %= mod

print(ans)

Submission status: Wrong Answer

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

cnt = [0]*n
ans = 1

for i in a:
    if i>0: ans *= cnt[i-1] - cnt[i]
    cnt[i] += 1
    ans %= mod

if cnt[0]==3:
    ans *= 6
    ans %= mod
else:
    ans *= 3
    ans %= mod

print(ans)

Submission status: Wrong Answer

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 n == 1 and a_ls[0] != 0:
    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: Wrong Answer

Solution code:
import sys
n = int(input())
a_ls = list(map(int, input().split()))
hats_person = [0,0,1]
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: Wrong Answer

Solution code:
import sys
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: Wrong Answer

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%mod)

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

N = I()
A = LI()
mod = 10**9+7

ans = 1
x,y,z = -1,-1,-1
for i in range(N):
    a = A[i]
    k = [x,y,z].count(a-1)
    if k == 3:
        ans *= 3
        ans %= mod
        z += 1
    elif k == 2:
        ans *= 2
        ans %= mod
        if x == a-1:
            x = a
        elif y == a-1:
            y = a
        else:
            z = a
    else:
        if x == a-1:
            x = a
        elif y == a-1:
            y = a
        else:
            z = a

print(ans)

Submission status: Wrong Answer

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
ans = 3
cnt = [0] * (n+1)
cnt[0] = 1
for i in range(1, n):
    if a[i] == 0:
        ans *= 3 - cnt[0]
        ans %= mod
    else:
        ans *= cnt[a[i]-1] - cnt[a[i]]
        ans %= mod
    cnt[a[i]] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a

# Union Find
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# 約数生成
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


def main():

    inf = 10 ** 18
    mod = 10 ** 9 + 7

    n = ir()
    a = lr()
    dp = [[0, 0, 0] for i in range(n+1)]
    for i in range(n):
        now = a[i]
        if dp[i][0] == now:
            dp[i+1][0] = dp[i][0]+1
            dp[i+1][1] = dp[i][1]
            dp[i+1][2] = dp[i][2]
        elif dp[i][1] == now:
            dp[i+1][0] = dp[i][0]
            dp[i+1][1] = dp[i][1]+1
            dp[i+1][2] = dp[i][2]
        else:
            dp[i+1][0] = dp[i][0]
            dp[i+1][1] = dp[i][1]
            dp[i+1][2] = dp[i][2]+1
    ans = 1
    for i in range(n):
        x,y,z = dp[i]
        now = a[i]
        if x == now and y == now and z == now:
            ans *= 3
        elif (x == now == y) or (y == now == z) or (z == now == x):
            ans *= 2
        ans %= mod
    print(ans)




    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

N=int(input())
A=list(map(int,input().split()))
color1 = -1
color2 = -1
color3 = -1

m = 10**9+7
patern = 1

for a in A:
    if color1 == a-1 and color2 == a-1 and color3 == a-1:
        patern = patern * 3
        color1 = a

    elif (color1 != a-1 and color2 == a-1 and color3 == a-1):
        patern = patern * 2
        color2 = a

    elif (color1 == a-1 and color2 == a-1 and color3 != a-1):
        patern = patern * 2
        color1 = a
    elif (color1 == a-1 and color2 != a-1 and color3 != a-1):
        color1 = a

    elif (color1 != a-1 and color2 == a-1 and color3 != a-1):
        color2 = a

    elif (color1 != a-1 and color2 != a-1 and color3 == a-1):
        color3 = a

    elif (color1 == a-1 and color2 != a-1 and color3 == a-1):
        patern = patern * 2
        color1 = a

    elif (color1 != a-1 and color2 != a-1 and color3 != a-1):
        #(これはありえない)
        continue
print(patern % m)

Submission status: Wrong Answer

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

N=int(input())
A=list(map(int,input().split()))
color1 = -1
color2 = -1
color3 = -1

m = 10**9+7
patern = 1

for a in A:
    if color1 == a-1 and color2 == a-1 and color3 == a-1:
        patern = patern * 3
        color1 = a
    elif (color1 != a-1 and color2 == a-1 and color3 == a-1):
        patern = patern * 2
        color2 = a

    elif (color1 == a-1 and color2 == a-1 and color3 != a-1):
        patern = patern * 2
        color1 = a
    elif (color1 == a-1 and color2 != a-1 and color3 != a-1):
        color1 = a

    elif (color1 != a-1 and color2 == a-1 and color3 != a-1):
        color2 = a

    elif (color1 != a-1 and color2 != a-1 and color3 == a-1):
        color3 = a
    
print(patern % m)

Submission status: Wrong Answer

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

N=int(input())
A=list(map(int,input().split()))
color1 = -1
color2 = -1
color3 = -1

m = 10**9+7
patern = 1

for a in A:
    if color1 == a-1 and color2 == a-1 and color3 == a-1:
        patern = (patern * 3)%m
        color1 = a
    elif (color1 != a-1 and color2 == a-1 and color3 == a-1):
        patern = (patern * 2)%m
        color2 = a

    elif (color1 == a-1 and color2 == a-1 and color3 != a-1):
        patern = (patern * 2)%m
        color1 = a
    elif (color1 == a-1 and color2 != a-1 and color3 != a-1):
        color1 = a

    elif (color1 != a-1 and color2 == a-1 and color3 != a-1):
        color2 = a

    elif (color1 != a-1 and color2 != a-1 and color3 == a-1):
        color3 = a
    
print(patern)

Submission status: Wrong Answer

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

N=int(input())
A=list(map(int,input().split()))
color1 = -1
color2 = -1
color3 = -1

m = 10**9+7
patern = 1

for a in A:
    if color1 == a-1 and color2 == a-1 and color3 == a-1:
        patern = (patern * 3)%m
        color1 = a
    elif (color1 != a-1 and color2 == a-1 and color3 == a-1):
        patern = (patern * 2)%m
        color2 = a

    elif (color1 == a-1 and color2 == a-1 and color3 != a-1):
        patern = (patern * 2)%m
        color1 = a
    elif (color1 == a-1 and color2 != a-1 and color3 != a-1):
        color1 = a

    elif (color1 != a-1 and color2 == a-1 and color3 != a-1):
        color2 = a

    else:
        color3 = a
    
print(patern)

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7


def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [0]*N
    ans = 3
    for a in A:
        if a == 0:
            if B[a] == 1:
                ans *= 2
        else:
            if B[a-1] <= B[a]:
                ans = 0
            else:
                ans *= B[a-1] - B[a]
        B[a] += 1
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7


def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [0]*N
    ans = 3
    for a in A:
        if a == 0:
            if B[a] == 1:
                ans *= 2
        else:
            if B[a-1] - B[a] != 0:
                ans *= B[a-1] - B[a]
        B[a] += 1
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7


def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    B = [0]*N
    ans = 3
    for a in A:
        if a == 0:
            if B[a] == 1:
                ans *= 2
        else:
            ans *= B[a-1] - B[a]
        B[a] += 1
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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:
        if a not in T:
            return -1
        answer = (answer*T.count(a))%modulo
        T[T.index(a)] += 1
    return answer


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

Submission status: Wrong Answer

Solution code:
import sys


read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7



def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    R = 0
    G = 0
    B = 0
    ans = 1
    for a in A:
        if a==R==G==B:
            ans *= 3
            ans %= MOD
            R += 1
        else:
            if a==R==G:
                ans *= 2
                ans %= MOD
                R += 1
            elif a==G==B:
                ans *= 2
                ans %= MOD
                G += 1
            elif a==B==R:
                ans *= 2
                ans %= MOD
                B += 1
            else:
                if a==R:
                    R += 1
                elif a==G:
                    G += 1
                else:
                    B += 1
    print(ans%MOD)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

L = 1000000007

state = [0,0,0]

M = [0,0,0]
for a in A:
    t = None
    n = 0
    for i,s in enumerate(state):
        if s == a:
            t = i
            n += 1
    if n == 0:
        break
    M[n-1] += 1
    state[t] += 1

x = 2**30 % L
y = 3**19 % L


k = M[1] % x
l = M[2] % y
r = 2**k * 3**l % L
print(r)

Submission status: Wrong Answer

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)
  for j in range(3):
    if b[j]==i:
      b[j]+=1
      break
  else:print(1)
print(ans%mod)

Submission status: Wrong Answer

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)
  for j in range(3):
    if b[j]==i:
      b[j]+=1
      break
    else:print(1)
print(ans%mod)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
mod = 10**9+7
x,y,z=0,0,0
ans=1
for a in A:
  if x==a and y==a and z==a:
    ans*=3
  elif x==a and y==a:
    ans*=2
  elif x==a and z==a:
    ans*=2
  elif y==a and z==a:
    ans*=2  
  ans%=mod
  if x==a:
    x+=1
  elif y==a:
    y+=1
  else:
    z+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

#import numpy as np

def main():
	n=II()
	A=LI()
	B=[0]*(n+1)
	ans=3
	B[1]=1
	B[0]=3

	for i in range(1,n):
		ans*=(B[A[i]])-(B[A[i]+1])
		if ans<0:
			ans=0
		B[A[i]+1]+=1
		ans%=10**9+7
	print(ans)








if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
#input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

#import numpy as np

def main():
	n=II()
	A=LI()
	B=[0]*(n+1)
	ans=3
	B[1]=1
	B[0]=3

	for i in range(1,n):
		ans*=(B[A[i]])-(B[A[i]+1])
		B[A[i]+1]+=1
		ans%=10**9+7
	print(ans)








if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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):
    if a[i]!=0:
        ans*=col[a[i]-1]
        col[a[i]-1]-=1
        col[a[i]]+=1
    else:
        ans*=col[0]
        col[0]-=1
    ans%=mod
print(ans)

Submission status: Wrong Answer

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

cnt=0
for i in range(n):
    if a[i]==0: cnt+=1

ans=6 if cnt>1 else 3

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

Submission status: Wrong Answer

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

cnt=0
for i in range(n):
    if a[i]==0: cnt+=1

ans=6 if cnt>1 else 3

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

Submission status: Wrong Answer

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()

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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
m = 1000000007
d = [0,0,0]
ans = 1
for i in range(n):
    if d[0] == d[1] == d[2] == a[i]:
        ans *= 3
        d[0] += 1
    elif (d[0] == d[1] or d[1] == d[2]) and a[i] == d[1]:
        ans *= 2
        d[1] += 1
    elif a[i] == d[0]:
        d[0] += 1
    elif a[i] == d[1]:
        d[1] += 1
    elif a[i] == d[2]:
        d[2] += 1
    
      
    ans %= m
    d = sorted(d,reverse=True)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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