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
input = sys.stdin.readline


def main():
    MOD = 1000000007
    N = int(input())
    A = [int(x) for x in input().split()]

    ans = 1


    red = 1
    blue = 0
    green = 0

    for i in range(1, N):
        count = 0
        if red == A[i]:
            count += 1
        if blue == A[i]:
            count += 1
        if green == A[i]:
            count += 1

        if red == A[i]:
            red += 1
        elif blue == A[i]:
            blue += 1
        elif green == A[i]:
            green += 1

        ans = ans * count

    print((ans * 3) % MOD)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=input().split()
L=[0]*N
count = 3
L[0] = 1
for idx in range(N)[1:]:
    An = int(A[idx])
    if (A[idx] == "0"):
        count = count * (3 - L[0]) % 1000000007
    else:
        count = count * (L[An -1] - L[An])  % 1000000007
    L[An] += 1
print(count)

Submission status: Wrong Answer

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 a[0] != 0:
        print(0)
        exit()

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

    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
    else:
        print(0)
        exit()

    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
        elif a[i] == z:
            z += 1
        else:
            print(0)
            exit()

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



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
from itertools import permutations
from itertools import combinations

#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N = int(input())
A = list(map(int, input().split()))
div = 1000000007

r = 0
b = 0
g = 0
pat = 1
for i, a in enumerate(A):
    if i==0:
        pat *= 3
        r += 1

    elif r==b and b==g:
        pat *= 3
        r += 1

    elif r>b and b==g:
        if a==r:
            r += 1
        else:
            pat *= 2
            b += 1

    elif r==b and b>g:
        if a==r:
            pat *= 2
            r += 1
        else:
            g += 1

    elif r>b and b>g:
        if a==r:
            r += 1
        elif a==b:
            b += 1
        else:
            g += 1
    
print(pat%div)

Submission status: Wrong Answer

Solution code:
print(6)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    mod = 1000000007
    set1 = set([])
    set2 = set([])
    set3 = set([])
    ans = 1
    for v in a:
        if v == 0:
            if 0 in set1:
                if 0 in set2:
                    set3.add(0)
                else:
                    set2.add(0)
                    ans = ans*2%mod
            else:
                set1.add(0)
                ans = ans*3%mod
        else:
            if v in set1:
                if v in set2:
                    set3.add(v)                
                else:
                    set2.add(v)
                    if v-1 in set2:
                        if v-1 in set3:
                            ans = ans*2%mod
            else:
                set1.add(v)
                if v-1 in set2:
                        if v-1 in set3:
                            ans = ans*3%mod
                        else:
                            ans = ans*2%mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

mod = 1000000007
fmax =0
smax =0
tmax =0

result = 1
for i in a:
    if i == fmax and i == smax and i == tmax:
        result = result * 3 % mod
        fmax += 1
    elif i == fmax and i == smax and i != tmax:
        result = result * 2 % mod
        fmax += 1
    elif i == fmax and i != smax and i == tmax:
        result = result * 2 % mod
        fmax += 1
    elif i != fmax and i == smax and i == tmax:
        result = result * 2 % mod
        smax += 1
    elif i == fmax and i != smax and i != tmax:
        fmax += 1
    elif i != fmax and i == smax and i != tmax:
        smax += 1
    elif i != fmax and i != smax and i == tmax:
        tmax += 1
print(result)

Submission status: Wrong Answer

Solution code:
import subprocess
import gzip, base64, os
import stat

gz = b'H4sIAEbA410C/+19e3wU1dn/7G4Sllt2QdSICEEDJCIhyzXxUjeEhVnYQCQBqQpD2GxIdLOb3+5sCBYVmsS6rlFqrbXt25Zf+6m19u1b7VsVqQUCQqhXFC+ICmgRZ4kXvJRGBPZ9nnPObM7MzkSwvu9frpIzz3eec87znPOc51xm5pzbPL45VotFUH9W4TsCUo9e6Ca0m+F3+NM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9Sdkn2L+liA7A9JXKkmNYfJgCM8TPJvX18+AX/96+ZlE0b3xfgZRDF7y/upYfa+SfzVvmVaF6rMspeqzL6Xqsy+l6q9VStVnUUrVZ1VKS86ylJacfSktOVPh/RE5Ksfq64v9giTNq1gk+bzVNZIk9J0WK8lNkh9PhY0KBpAk1YWlVcHwytqgVAeNOirVxloFf7ipORiQA3XFM8pKpxgzSX3PSCUo+MgagT5nrYs1Na2BKBwl9T1aZaySNGdReaVH8iyYjWfeEsnZNdN64Zw51Z4aqaZ8ls+DHJok6rQAOfRQkGZ/d0F5pbdCwIeyKqY/iNPt5o8y7P/gUh0vPWDRH40RzYV0Dmd5hqdeAsPjbTPENDyAV8NVXHZGRwmT4yozk+cOxTQoon7O+NXK4HIJrHSMD9bUpk2PsGQYO5GSnIuphTKOttTd7vckU702GWeC6mu579zNzGxMzt/NrFLTs3IzcsN3M6DOg0EpfbLw1ztJWJsyOUzUQIHMk4Z1EvV71PRCiXzMJsWigbrME4Z1EkQCgQwB+jsHWBudnoOqS6Cfs671jd70OGsj88446NXQTRidVm3QkgzOotVzGRxvrDcMUE/1iBmHO2faGzkTOBPmTqvWp18XDUsN0NNC4dFDaA3bHHo7NoT6Bk/V1vstdoat3lq0p0Lrtcs8T1mv4spolLOI9JHiGZVxRucMu93cWbmZpyMbepGMo5IzE9EeJ22Uivasdm0Khie8mxyMrnO95EhhAwfx9Q4I1zX9QLheb07pY9S1GpC+/Osd0q3Lk3zjmmFCZ3xGtkG3bHBwvVlzNzl/3Kg+uQPMdUVBXiYzOnk802y+gRO2DTpGA+3Sx9vr7mmOtYZ77Jhp/an1maWqHkBuMMKhR4gbCKE/Xz3TlJvX9NW8a1rmMd36cUdjSKB+7X8ATE88PzaLAAA='
bin = gzip.decompress(base64.b64decode(gz))
p = os.path.dirname(__file__)
pymain = os.path.join(p, "a.out")
with open(pymain, "wb") as f:
    f.write(bin)
os.chmod(pymain, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
if p == "":
    pymain = "./" + pymain
subprocess.call(pymain)


# compiled by pythran
'''
#pythran export main2()
import numpy as np
i8 = np.int64
i4 = np.int32

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
x = list(map(int,input().split()))
# n = 4
# x = [0, 0, 1, 0]

num_list = [0] * n
pattern = 1
waru = 1000000007

for i in x:
    if i == 0:
        if num_list[i] == 0:
            pattern *= 3
            num_list[i] = 1
            pattern %= waru
        elif num_list[i] == 1:
            pattern *= 2
            num_list[i] = 2
            pattern %= waru
    else:
        if num_list[i] == 0:
            pattern *= num_list[i-1]
            num_list[i] = 1
            pattern %= waru
        elif num_list[i] == 1:
            pattern *= (num_list[i-1]-1)
            num_list[i] = 2
            pattern %= waru

print(pattern)

Submission status: Wrong Answer

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

for x in a:
    up = max(up, x)

t = [0 for _ in range(up + 1)]

ans = 1
for i in range(n):
    if a[i] == 0:
        t[a[i]] += 1
    else:
        ans *= t[a[i] - 1]
        ans %= 1000000007
        t[a[i]] += 1
        t[a[i] - 1] -= 1


zero = 0
for x in a:
    if x == 0:
        zero += 1

if zero == 1:
    ans *= 3
else:
    ans *= 6

ans %= 1000000007
print(ans)
exit(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a=list(map(int,input().split(' ')))
mod = 10**9+7
ans = 1
color=[1,0,0]
g = 0
for i in a[1:]:
    cnt = 0
    for n,j in enumerate(color):
        if i==j:
            cnt += 1
            if cnt == 1:
                color[n]+=1
    ans *= cnt
    ans %= mod
for i in color:
    if i >0:
        g += 1
if g ==2:
    print(ans*6%mod)
else:
    print(ans*3%mod)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=[0 for i in range(N+1)]
p=1
MOD=10**9+7

for i in A:
    L[i]+=1
    if i==0:
        if L[0]==1:
            p*=3
        elif L[0]==2:
            p*=2
    else:
        if L[i]==1:
            p*=L[i-1]
        elif L[i]==2:
            p*=(L[i-1]-L[i]+1)

print(p%MOD)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
print(3)

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
    ans %= mod
    #print(l)

print(ans% mod)

# print(l)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
c = [0 for _ in range(10**7+1)]
c[0] = 1
r = 3

for i in range(1,n):
    k = c[s[i]-1]-c[s[i]]
    if k==0: k=3
    r *= k
    c[s[i]]+=1
print(r%1000000007)

Submission status: Wrong Answer

Solution code:
import sys
import math
from itertools import permutations
from itertools import combinations

#input = sys.stdin.readline
sys.setrecursionlimit(10**7)
N = int(input())
A = list(map(int, input().split()))
div = 1000000007

r = 0
b = 0
g = 0
pat = 1
for i, a in enumerate(A):
    if i==0:
        pat *= 3
        r += 1

    elif r==b and b==g:
        pat *= 3
        r += 1

    elif r>b and b==g:
        if a==r:
            r += 1
        else:
            pat *= 2
            b += 1

    elif r==b and b>g:
        if a==r:
            pat *= 2
            r += 1
        else:
            g += 1

    elif r>b and b>g:
        if a==r:
            r += 1
        elif a==b:
            b += 1
        else:
            g += 1
    
    pat = (pat%div)

print(pat)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
mod = 1000000007
var = 1
C1 = C2 = C3 = 0
for i in range(0,N-1):
    if A[i] == C1:
        if C1 != C2:
            var = var % mod
        elif C1 == C2 != C3:
            var = var * 2 % mod
        elif C1 == C2 == C3:
            var = var * 3 % mod
        C1 += 1
    elif A[i] == C2:
        if C2 != C3:
            var = var % mod
        elif C2 == C3:
            var = var * 2 % mod
        C2 += 1
    elif A[i] == C3:
        C3 += 1
print(var)

Submission status: Wrong Answer

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

mod=1000000007
ans=1
r,b,g=0,0,0
for i in a:
    if i==r and i==b and i==g:
        ans*=3
        ans%=mod
        r+=1
    elif (i==r and i==b) or (i==g and i==r):
        ans*=2
        ans%=mod
        r+=1
    elif i==b and i==g:
        ans*=2
        ans%=mod
        g+=1
    elif i==r:
        r+=1
    elif i==b:
        b+=1
    elif i==g:
        g+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=[0 for i in range(N)]
p=1
MOD=10**9+7

for i in A:
    L[i]+=1
    if i==0:
        if L[0]==1:
            p*=3
        elif L[0]==2:
            p*=2
    else:
        if L[i]==1:
            p*=L[i-1]
        elif L[i]==2:
            p*=(L[i-1]-L[i]+1)
    p%=MOD
    
print(p)

Submission status: Wrong Answer

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:
        if a[0] == 0:
            print(3)
        else:
            print(0)
        exit()
    elif n == 2:
        if a[1] == 0:
            print(3)
        if a[1] == 1:
            print(6)
        else:
            print(0)
        exit()

    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
    else:
        print(0)
        exit()

    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
        elif a[i] == z:
            z += 1
        else:
            print(0)
            exit()

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



if __name__ == '__main__':
    main()