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:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    zeros = A.count(0) 
    if zeros==0 or A[0]!=0:
        print(0)
        return 
    elif zeros==1:
        ans = 3
    else:
        ans = 6
    cnt = defaultdict(int)
    cnt2 = defaultdict(int)
    for a in A:
        if a==0:
            cnt[0]+=1
            continue
        ans *= cnt[a-1]
        ans %= mod
        cnt[a-1]-=1
        cnt[a]+=1
        cnt2[a]+=1
        if cnt2[a]>3:
            ans = 0
        # print(a,ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
MOD = 1000000007
N = int(input())
A = [int(i) for i in input().split()]
count = [0]*N
dp = [0]*N
dp[0] = 3
count[0] += 1
for i, a in enumerate(A[1:]):
    if a == 0:
        dp[i+1] = dp[i]*(3-count[a])
    else:
        dp[i+1] = dp[i]*(count[a-1]-count[a])
    count[a] += 1
    dp[i+1] %= MOD
print(dp[-1])

Submission status: Wrong Answer

Solution code:
INF = 1000000007

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

ans = 3
rgb = [1, 0, 0]

for i in range(1, N):
    a = Alist[i]
    temp = 0
    check = False
    for j in range(3):
        if rgb[j] == a:
            temp += 1
            if not check:
                rgb[j] += 1
                check = True
    ans = ans * temp
    ans = ans % INF

print(ans)

Submission status: Wrong Answer

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

for a in A:
    count = 1
    flag = False
    for i in range(3):
        if l[i] == a:
            if flag:
                count += 1
            else:
                flag = True
                l[i] += 1
    ans *= count

c = 1
for ll in l:
    c = max(c, l.count(ll))

if c == 1:
    ans *= 6
elif c == 2:
    ans *= 3

print(ans% mod)

# print(l)

Submission status: Wrong Answer

Solution code:
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
input = sys.stdin.readline
N = int(input())
A =[int(i) for i in input().split()]
num_count = [0] * N
ans = 1
md = 1000000007
for i in range(N):
    num_count[A[i]] += 1
    if num_count[A[i]] > 3 :
        ans = 0
        break
    else:
        if A[i] != 0:
            if num_count[A[i]-1] == 0:
                ans = 0
                break
            else:
                ans *=  num_count[A[i]-1]-num_count[A[i]]+1
                ans = ans%md
        else:
            continue
if num_count[0] == 2:
    ans *= 3
    ans = ans%md
elif num_count[0] == 3:
    ans *= 6
    ans = ans%md
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
lists=list(map(int,input().split()))
counter=1
uselist=[0,0,0]
for j in range(n):
    human=lists[j]
    if human==0:
        for k in range(3):
            if uselist[k]==0:
                uselist[k]+=1
                counter=counter*(3-k)
                break
    elif human!=0:
        sub=0
        for j in range(3):
            if uselist[j]==human:
                sub+=1
        for j in range(3):
            if uselist[j]==human:
                uselist[j]+=1
                break
        counter=counter*sub
        counter=counter%(10**9+7)
print(counter)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

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


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)

@mt
def slv(N, A):
    M = Mod(1000000007)
    ans = 1
    rem = Counter()
    c = 0
    for a in A:
        if a == 0:
            c +=1
        if rem[a-1] > 0:
            ans = M.mul(ans, rem[a-1])
            rem[a-1] -= 1
        rem[a] += 1

    if c == 1:
        ans = M.mul(ans, 3)
    else:
        ans = M.mul(ans, 6)
    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
n1=-1
n2=-1
n3=-1
ans=1
mod=1000000007
for i in range(n):
  num=l[i]
  if num==n1+1 and num==n2+1 and num==n3+1:
    ans=(ans*3)%mod
    n1+=1
  elif num==n1+1 and num==n2+1:
    ans=(ans*2)%mod
    n1+=1
  elif num==n2+1 and num==n3+1:
    ans=(ans*2)%mod
    n2+=1
  else:
    if num==n1+1:
      n1+=1
    elif num==n2+1:
      n2+=1
    else:
      n3+=1
print((ans)%mod)

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    zeros = A.count(0) 
    if zeros==0 or A[0]!=0:
        print(0)
        return 
    elif zeros==1:
        ans = 3
    else:
        ans = 6
    cnt = defaultdict(int)
    for a in A:
        if a==0:
            cnt[0]+=1
            continue
        ans *= cnt[a-1]
        ans %= mod
        cnt[a-1]-=1
        cnt[a]+=1
        if cnt[a]>3:
            ans = 0
        # print(a,ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split( )))

cnt = {i:0 for i in range(-1,n+1)}

cnt[-1] = 3
ans = 1
mod = 10**9+7
for i in range(n):
    ans *= min(cnt[a[i]-1],3-cnt[a[i]])
    ans %= mod
    cnt[a[i]]+=1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=[0]*n
#print(a) 
x=0;y=0;z=0
c=0;d=0;e=0
ss=[[0,0,0] for _ in range(n)]
ans=1
for i in range(n):
    if a[i]==c:
        l[i]='r'
        c+=1
    elif a[i]==d:
        l[i]='b'
        d+=1
    elif a[i]==e:
        l[i]='g'
        e+=1
    ss[i][0]=c;ss[i][1]=d;ss[i][2]=e
ss=[[0,0,0]]+ss
#print(ss)
p=[0,0,0]
for i in range(1,n+1):
    p[0]=ss[i][0]-ss[i-1][0];p[1]=ss[i][1]-ss[i-1][1];p[2]=ss[i][2]-ss[i-1][2]
    #print(p)
    if ss[i-1][0]==ss[i-1][1]==ss[i-1][2] and p[0]==1:
        ans=ans*3%(10**9+7)
    elif ss[i-1][0]==ss[i-1][1] and p[0]==1:
        ans=ans*2%(10**9+7)
    elif ss[i-1][1]==ss[i-1][2] and p[1]==1:
        ans=ans*2%(10**9+7) 
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split( )))

cnt = {i:0 for i in range(-1,n+1)}

cnt[-1] = 3
ans = 1
mod = 10**9+7
for i in range(1,n):
    ans *= min(cnt[a[i]-1],3-cnt[a[i]])
    ans %= mod
    cnt[a[i]]+=1

print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7

N = int(input())
count = [0] * 3
ans = 1

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

for i in range(N):
    sames = 0
    for j in range(3):
        if A[i] == count[j]:
            sames += 1

    if sames == 0:
        break

    ans *= sames
    ans %= MOD

    for j in range(3):
        if A[i] == count[j]:
            count[j] += 1
            break
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    ans = 1
    r,g,b = 0,0,0
    for i in range(n):
        if i == 0:
            r += 1
            ans = 3
        else:
            cnt = 0
            f = True
            if r == a[i]:
                cnt += 1
                r += 1
                f = False
            if g == a[i] :
                cnt += 1
                if f:
                    g += 1
                f = False
            if b == a[i] :
                cnt += 1
                if f:
                    b += 1

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




    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

result = 3
t = [1, 0, 0]
for i in range(1, N):
  a = A[i]
  f = -1
  k = 0
  for j in range(3):
    if t[j] == a:
      k += 1
      if f == -1:
        t[j] += 1
        f = j
  result = (result * k) % 1000000007
print(result)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    a[i]+=1
mod=10**9+7
iro=[0]*(10**5+3)
iro[0]=3
ans=1
for i in range(n):
    ans%=mod
    # print(iro[a[i]-1])
    # print(iro[a[i]])
    if iro[a[i]]==0:
        if iro[a[i]-1]==0:
            print(0)
            quit()
        else:
            ans*=iro[a[i]-1]
    elif iro[a[i]]==1:
        if iro[a[i]-1]<=1:
            print(0)
            quit()
        else:
            ans*=iro[a[i]-1]-1
    elif iro[a[i]]==2:
        if iro[a[i]-1]<=2:
            print(0)
            quit()
        else:
            ans*=1
    else:
        print(0)
        quit()
    iro[a[i]]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
r = 1
b = 0
g = 0
MOD = 1000000007
if N == 1 and A[0] != 0:
    print(0)
    exit()
for i in range(1,N):
    a = A[i]
    count = 0
    flag = 0
    if a == r:
        r += 1
        count += 1
        flag = 1
    if a == b:
        if flag == 1:
            count += 1
        else:
            b += 1
            count += 1
            flag = 1
    if a == g:
        if flag == 1:
            count += 1
        else:
            g += 1
            count += 1
            flag = 1
    if flag == 0:
        print(0)
        exit()
    ans *= count
    ans %= MOD
print((ans*3)%MOD)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7

N = int(input())
count = [0] * 3
ans = 1

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

for i in range(N):
    sames = 0
    for j in range(3):
        if A[i] == count[j]:
            sames += 1

    if sames == 0:
        print(0)
        break

    ans *= sames
    ans %= MOD

    for j in range(3):
        if A[i] == count[j]:
            count[j] += 1
            break
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
# from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    ans = 1
    r,g,b = 0,0,0
    for i in range(n):
        if i == 0:
            r += 1
            ans = 3
        else:
            cnt = 0
            f = True
            if r == a[i]:
                cnt += 1
                r += 1
                f = False
            if g == a[i] :
                cnt += 1
                if f:
                    g += 1
                f = False
            if b == a[i] :
                cnt += 1
                if f:
                    b += 1
            if cnt == 0:
                continue
            
            ans = (ans * cnt) % MOD
    print(ans % MOD)            
            




    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for a in A:
    count = 1
    flag = False
    for i in range(3):
        if l[i] == a:
            if flag:
                count += 1
            else:
                flag = True
                l[i] += 1
    ans *= count

print(ans% mod)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
r = 1
b = 0
g = 0
MOD = 1000000007

for i in range(1,N):
    a = A[i]
    count = 0
    flag = 0
    if a == r:
        r += 1
        count += 1
        flag = 1
    if a == b:
        if flag == 1:
            count += 1
        else:
            b += 1
            count += 1
            flag = 1
    if a == g:
        if flag == 1:
            count += 1
        else:
            g += 1
            count += 1
            flag = 1
    if flag == 0:
        print(0)
        exit()
    ans *= count
    ans %= MOD
print((ans*3)%MOD)

Submission status: Wrong Answer

Solution code:
import sys

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

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

main()

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
n1=-1
n2=-1
n3=-1
ans=1
mod=10**9+7
for i in range(n):
  num=l[i]
  if num==n1+1 and num==n2+1 and num==n3+1:
    ans=(ans*3)%mod
    n1+=1
  elif num==n1+1 and num==n2+1:
    ans=(ans*2)%mod
    n1+=1
  elif num==n2+1 and num==n3+1:
    ans=(ans*2)%mod
    n2+=1
  else:
    if num==n1+1:
      n1+=1
    elif num==n2+1:
      n2+=1
    else:
      n3+=1
if n2==-1 and n3==-1:
  print(3)
else:
  print((ans)%mod)

Submission status: Wrong Answer

Solution code:
import sys

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

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

main()

Submission status: Wrong Answer

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

max_a = max(a)
lst = [0 for i in range(max_a+1)]

ans = 1
for i in a:
    if i == 0:
        if lst[i] == 0:
            ans = (ans * 3) % 1000000007
            lst[i] += 1
        elif lst[i] == 1:
            ans = (ans * 2) % 1000000007
            lst[i] += 1
        else:
            lst[i] += 1
    else:
        ans = (ans * (lst[i-1]-lst[i])) % 1000000007
        lst[i] += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
r = 1
b = 0
g = 0
MOD = 1000000007

for i in range(1,N):
    a = A[i]
    count = 0
    flag = 0
    if a == r:
        r += 1
        count += 1
        flag = 1
    if a == b:
        if flag == 1:
            count += 1
        else:
            b += 1
            count += 1
            flag = 1
    if a == g:
        if flag == 1:
            count += 1
        else:
            g += 1
            count += 1
    ans *= count
    ans %= MOD
print((ans*3)%MOD)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

ans = 3
hats = [1, 0, 0]

if n == 1:
    if A[0] == 0:
        print(3)
    else:
        print(0)
    exit()


for a in A[1:]:
    count = hats.count(a)
    if count == 0:
        print(0)
        exit()

    idx = hats.index(a)
    hats[idx] += 1
    ans *= count
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

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

main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 1
r = 1
b = 0
g = 0
MOD = 1000000007

for i in range(1,N):
    a = A[i]
    count = 0
    flag = 0
    if a == r:
        r += 1
        count += 1
        flag = 1
    if a == b:
        if flag == 1:
            count += 1
        else:
            b += 1
            count += 1
            flag = 1
    if a == g:
        if flag == 1:
            count += 1
        else:
            g += 1
            count += 1
    ans *= count
    ans %= MOD
print(ans*3)

Submission status: Wrong Answer

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

pattern = 1
hats = [0, 0, 0]
for a in A:
    if pattern >= 1000000007:
        pattern = pattern % 1000000007
    if pattern == 0:
        break
    i = 0
    for j in range(3):
        if a == hats[j]:
            i += 1
    pattern *= i
    for j in range(3):
        if a == hats[j]:
            hats[j] += 1
            break

print(pattern)

Submission status: Wrong Answer

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

li = [0]*n

ans = 1
mod = 1000000007

for i in a:
    if i == 0:
        if li[0] == 0:
            ans *= 3
        elif li[0] == 1:
            ans *= 2
        else:
            ans *= 1
        li[0] += 1
    else:
        if li[i] == 0:
            if li[i-1] == 1:
                ans *= 1
            elif li[i-1] == 2:
                ans *= 2
            elif li[i-1] == 3:
                ans *= 3
        elif li[i] == 1:
            if li[i-1] == 2:
                ans *= 1
            elif li[i-1] == 3:
                ans *= 2
        else:
            ans *= 1
        li[i] += 1
    ans %= mod

print(ans)

Submission status: Wrong Answer

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

MOD = 10 ** 9 + 7

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

ans = 3

for x in a:
    if x == 0:
        for i in range(3):
            if c[i] == -1:
                c[i] = 0
                break
    else:
        ans = ans * c.count(x-1) % MOD
        for i in range(3):
            if c[i] == x-1:
                c[i] = x
                break

if c[1] != -1:
    ans = ans * 2 % MOD

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N=int(input())
    MOD=1000000007
    
    a=list(map(int,input().split()))
    c=[0,0,0]
    ans=1
    for i in range(N):
        if c[1]==c[0] and c[2]==c[0]:
            ans=ans*3%MOD
            c[0]+=1

        elif c[0]==c[1]:
            if c[0]==a[i]:
                c[0]+=1
                ans=(ans*2)%MOD
            else:
                c[2]+=1

        elif c[0]>c[1] and c[1]==c[2]:
            if c[0]==a[i]:
                c[0]+=1
            else:
                ans=(ans*2)%MOD
                c[1]+=1
            
        else:
            if c[0]==a[i]: c[0]+=1
            if c[1]==a[i]: c[1]+=1
            if c[2]==a[i]: c[2]+=1       

        c.sort(reverse=True)
    print(ans)


 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

MOD = 10**9 + 7

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

ptns = Counter([(0, 0, 0)])
#print('ptns:', ptns)
for A in As:
    ptn2s = Counter()
    for ptn, num in ptns.items():
        for i in range(3):
            if ptn[i] == A:
                ptn2 = list(ptn)
                ptn2[i] += 1
                ptn2s[tuple(ptn2)] += num
                ptn2s[tuple(ptn2)] %= MOD
    ptns = ptn2s

#print(ptns)
print(sum(ptns.values()))

Submission status: Wrong Answer

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

pattern = 1
hats = [0, 0, 0]
for a in A:
    if pattern > 1000000007:
        pattern = pattern % 1000000007
    if pattern == 0:
        break
    i = 0
    for j in range(3):
        if a == hats[j]:
            i += 1
    pattern *= i
    for j in range(3):
        if a == hats[j]:
            hats[j] += 1
            break

print(pattern)

Submission status: Wrong Answer

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

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

if max(cnt) == 1:
    print((ans*3) % mod)
elif max(cnt) == 3:
    print((ans * 6) % mod)
elif max(cnt) == 2:
    print((ans * 3) % mod)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
MOD = 1000000007

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

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

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

    if yo > 1:
        res = (res * yo) % MOD

    values[last] += 1

print(res)

Submission status: Wrong Answer

Solution code:
mod=1000000007

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
it=list(map(int,input().split()))
mod=1000000007
#dp=[[0,0,0] for i in range(n+1)]
#dp[-1]=[1,1,1]
#col=[0 for i in range(n)]
#fe=[]
cu=0
tot=1
ss={}

for i in range(n):
    try:
        x=ss[it[i]-1]
        tot*=len(x.copy())
        tot%=mod
        ss[it[i]-1].pop()
        try:
            ss[it[i]].add(i)
        except:
            ss[it[i]]=set([i])
        
    except:
        cu+=1
        tot*=(3-cu+1)
        tot%=mod
        try:
            ss[it[i]].add(i)
        except:
            ss[it[i]]=set([i])
        
        
                

print(tot%mod)

Submission status: Wrong Answer

Solution code:
def solve():
    mod = 10 ** 9 + 7

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

    ret = 1
    cnt = [0] * (n + 1)
    cnt_0 = 0
    for x in a:
        cnt[x] += 1
        if x == 0:
            cnt_0 += 1
        else:
            ret = (ret * cnt[x - 1]) % mod
            cnt[x - 1] -= 1

    ret = ret * (3 if cnt_0 == 1 else 6) % mod

    print(ret)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
now = 0
c = [0 for k in range(max(A)+1)]
ans = 1
MOD = 1000000007

def kaijou(k):
    ret = 1
    for k in range(1,k+1):
        ret *= k
    return ret

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

Submission status: Wrong Answer

Solution code:
n=int(input())
it=list(map(int,input().split()))
mod=1000000007
#dp=[[0,0,0] for i in range(n+1)]
#dp[-1]=[1,1,1]
#col=[0 for i in range(n)]
#fe=[]
cu=0
tot=1
ss={}

for i in range(n):
    try:
        x=ss[it[i]-1]
        tot*=len(x)
        tot%=mod
        ss[it[i]-1].pop()
        try:
            ss[it[i]].add(i)
        except:
            ss[it[i]]=set([i])
        
    except:
        cu+=1
        tot*=(3-cu+1)
        tot%=mod
        try:
            ss[it[i]].add(i)
        except:
            ss[it[i]]=set([i])
        
        
                

print(tot%mod)

Submission status: Wrong Answer

Solution code:
mod=10**9+7

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    mod = 10 ** 9 + 7
    n = int(input())
    l = [int(x) for x in input().split()]
    cnt = [0] * n
    res = 1
    for v in l:
        if v:
            res *= cnt[v - 1] - cnt[v]
            res %= mod
        cnt[v] += 1
    res *= 3 if cnt[0] == 1 else 6
    res %= mod
    print(res)

main()