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:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 3:
            res *= 3
        elif cnt[i] == 2:
            res *= 2
        cnt[i] -= 1
        cnt[i+1] += 1
        res %= mod
    print(res)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 3:
            res *= cnt[i]
        if cnt[i] == 2:
            res *= cnt[i]
        cnt[i] -= 1
        cnt[i+1] += 1
        res %= mod
    print(res)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 3:
            res = 3 * res
        if cnt[i] == 2:
            res = 2 * res
        cnt[i] -= 1
        cnt[i+1] += 1
        res = res % mod
    print(res)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 3:
            res *= 3 % mod
        if cnt[i] == 2:
            res *= 2 % mod
        cnt[i] -= 1
        cnt[i+1] += 1
    print(res)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 1:
            res *= 1
        if cnt[i] == 3:
            res *= 3
        if cnt[i] == 2:
            res *= 2
        cnt[i] -= 1
        cnt[i+1] += 1
        res %= mod
    print(res%mod)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 4:
            res *= 4
        if cnt[i] == 3:
            res *= 3
        if cnt[i] == 2:
            res *= 2
        cnt[i] -= 1
        cnt[i+1] += 1
        res %= mod
    print(res%mod)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 3:
            res *= 3
        if cnt[i] == 2:
            res *= 2
        cnt[i] -= 1
        cnt[i+1] += 1
        res %= mod
        if res == 0:
            break
    print(res)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''
https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
'''
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate
    #from itertools import product
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    cnt = [3] + [0]*n
    res = 1
    for i in a:
        if cnt[i] == 3:
            res *= 3
        if cnt[i] == 2:
            res *= 2
        cnt[i] -= 1
        cnt[i+1] += 1
        res %= mod
    print(res)
        
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

Count = {x:0 for x in range(0, n)}

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

for i in range(1,n):
    now = A[i]
    if now == 0:
        ans = (3 - Count[0]) * ans % mod
        Count[0] += 1
        continue
    ans = (Count[now-1] - Count[now]) *ans  % mod
    Count[now] += 1

print(ans)
# print(Count)

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in input().split()]
_LI = lambda : [int(x)-1 for x in input().split()]
N = int(input())
A = LI()

c =[1,0,0]
cnt = [0 for _ in range(N)]
cnt[0] = 3
for i in range(1,N):
    cnt[i] = c.count(A[i])
    if cnt[i] == 0:
        print (0)
        exit (0)
    for j in range(3):
        if c[j] == A[i]:
            c[j] += 1
            break
ans = 1
for i in range(N):
    ans = (ans * cnt[i]) % MOD
print (ans)

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()

n=int(input())
A=[int(i) for i in input().split()]
ans=1
mod=10**9+7
C=[0,0,0]
for i in range(1,n):
  cur=A[i]
  ct=0
  Chk=True
  for i,c in enumerate(C):
    if cur==c:
      ct+=1
      if Chk:
        C[i]+=1
        Chk=False
  ans=(ans*ct)%mod
  
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()

n=int(input())
A=[int(i) for i in input().split()]
ans=3
mod=10**9+7
C=[1,0,0]
for i in range(1,n):
  cur=A[i]
  ct=0
  Chk=True
  for i,c in enumerate(C):
    if cur==c:
      ct+=1
      if Chk:
        C[i]+=1
        Chk=False
  ans=(ans*ct)%mod
  
print(ans)

Submission status: Wrong Answer

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

n = int(input())
a = list(map(int, input().split()))
rbg = [-1, -1, -1]
ans = 1
mod = 10 ** 9 + 7

for A in a:
    ans *= rbg.count(A - 1)
    ans %= mod
    if (A - 1) is not rbg:
        ans = 0
        break
    rbg[rbg.index(A - 1)] = A

print(ans)

Submission status: Wrong Answer

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

n = int(input())
a = list(map(int, input().split()))
rbg = [0, 0, 0]
ans = 1
mod = 10 ** 9 + 7

for A in a:
    ans *= rbg.count(A)
    ans %= mod
    if A is not rbg:
        ans = 0
        break
    rbg[rbg.index(A)] += 1

print(ans)

Submission status: Wrong Answer

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

def count(n, list1):
    num0 = list1.count(0)
    statuslist = []
    answer = 1
    for i in range(len(list1)):
        if list1[i] == 0:
                statuslist.append(0)
        else:
            pos = statuslist.count(list1[i]-1)
            answer *= pos
            try:
                statuslist.remove(list1[i]-1)
            except ValueError:
                return answer
            statuslist.append(list1[i])
    if num0 == 2 or num0 == 3:
        answer *= 6
    elif num0 == 1:
        answer *= 3  
    return (answer % 1000000007)
    # else:
    #     return 3
        

print(count(n, list1))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
checker = {'a':1,'b':0,'c':0}
p = 1000000007
ans = 3
for i in range(1,n):
    if checker['a'] == checker['b'] == checker['c'] == A[i]:
        checker['a'] += 1
        ans *= 3
        ans = ans%p
    elif checker['a'] == checker['b'] == A[i] and checker['c'] != A[i]:
        checker['a'] += 1
        ans *= 2
        ans = ans%p
    elif checker['b'] == checker['c'] == A[i] and checker['a'] != A[i]:
        checker['b'] += 1
        ans *= 2
        ans = ans%p
    elif checker['c'] == checker['a'] == A[i] and checker['b'] != A[i]:
        checker['c'] += 1
        ans *= 2
        ans = ans%p
    elif checker['a'] == A[i] and checker['b'] != A[i] and checker['c'] != A[i]:
        checker['a'] += 1
    elif checker['b'] == A[i] and checker['a'] != A[i] and checker['c'] != A[i]:
        checker['b'] += 1
    elif checker['c'] == A[i] and checker['b'] != A[i] and checker['a'] != A[i]:
        checker['c'] += 1
    else:
        ans *= 0
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
checker = {'a':1,'b':0,'c':0}
p = 10**9+7
ans = 3
for i in range(1,n):
    if checker['a'] == checker['b'] == checker['c'] == A[i]:
        checker['a'] += 1
        ans *= 3
        ans = ans%p
    elif checker['a'] == checker['b'] == A[i] and checker['c'] != A[i]:
        checker['a'] += 1
        ans *= 2
        ans = ans%p
    elif checker['b'] == checker['c'] == A[i] and checker['a'] != A[i]:
        checker['b'] += 1
        ans *= 2
        ans = ans%p
    elif checker['c'] == checker['a'] == A[i] and checker['b'] != A[i]:
        checker['c'] += 1
        ans *= 2
        ans = ans%p
    elif checker['a'] == A[i] and checker['b'] != A[i] and checker['c'] != A[i]:
        checker['a'] += 1
    elif checker['b'] == A[i] and checker['a'] != A[i] and checker['c'] != A[i]:
        checker['b'] += 1
    elif checker['c'] == A[i] and checker['b'] != A[i] and checker['a'] != A[i]:
        checker['c'] += 1
print(ans)

Submission status: Wrong Answer

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

#R,B,Gの順に埋めていく
#R,B,Gの現在の数を保存する
cnt={'R':1,'B':0,'G':0}

ans=3
for i in range(1,n):
    count=0
    edi=0
    for j in 'RBG':
        if cnt[j]==a[i]:
            count+=1
            if edi==0:
                cnt[j]+=1
                edi=1
    ans*=count
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7

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

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

ret = ret * (3 if zero == 1 else 3 * 2) % mod

print(ret)

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.buffer.readline
n = int(readline())
A = map(int, readline().split())
mod = 1000000007

caps = [0] * 3
ans = 1
for a in A:
    ans *= caps.count(a)
    ans %= mod
    for i in range(3):
      if caps[i] == a:
        caps[i] += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)

ans=1
for num in a:
    if num==0:
        last_cnt=3
        tmp = last_cnt - d[num]
    else:
        last_cnt=d[num-1]
        tmp = last_cnt - d[num]

        if tmp == 0:
            tmp=1
    ans*=tmp
    ans%=1000000007
    d[num]+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)

ans=1
for num in a:
    if num==0:
        last_cnt=3
        tmp = last_cnt - d[num]
    else:
        last_cnt=d[num-1]
        tmp = last_cnt - d[num]

    if tmp == 0:
        tmp=1
    ans*=tmp
    ans%=1000000007
    d[num]+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)

ans=1
for num in a:
    if num==0:
        last_cnt=3
    else:
        last_cnt=d[num-1]

    tmp = last_cnt - d[num]
    if tmp == 0:
        tmp=1
    ans*=tmp
    ans%=1000000007
    d[num]+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(int)

ans=1
for num in a:
    if num==0:
        last_cnt=3
    else:
        last_cnt=d[num-1]

    tmp = last_cnt - d[num]
    if tmp <= 0:
        tmp=1
    ans*=tmp
    ans%=1000000007
    d[num]+=1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt={'R':1,'B':0,'G':0}
color=['R','B','G']
ans=3
for i in range(1,n):
    count=0
    num=0
    for j in color:
        if cnt[j]==a[i]:
            count+=1
            if num==0:
                cnt[j]+=1
                num=1
    ans*=count
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7


class modint():
    def __init__(self, value):
        self.value = value % MOD

    def __int__(self):
        return int(self.value)

    def __float__(self):
        return float(self.value)

    def __str__(self):
        return str(self.value)

    def __repr__(self):
        return str(self.value)

    def __add__(self, other):
        return (modint(self.value + other.value) if isinstance(other, modint)
                else modint(self.value + other))

    def __sub__(self, other):
        return (modint(self.value - other.value) if isinstance(other, modint)
                else modint(self.value - other))

    def __mul__(self, other):
        return (modint(self.value * other.value) if isinstance(other, modint)
                else modint(self.value * other))

    def __truediv__(self, other):
        return (modint(self.value * pow(other.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(self.value * pow(other, MOD - 2, MOD)))

    def __pow__(self, other):
        return (modint(pow(self.value, other.value, MOD))
                if isinstance(other, modint)
                else modint(pow(self.value, other, MOD)))

    def __eq__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __ne__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __radd__(self, other):
        return (modint(other.value + self.value) if isinstance(other, modint)
                else modint(other + self.value))

    def __rsub__(self, other):
        return (modint(other.value - self.value) if isinstance(other, modint)
                else modint(other - self.value))

    def __rmul__(self, other):
        return (modint(other.value * self.value) if isinstance(other, modint)
                else modint(other * self.value))

    def __rtruediv__(self, other):
        return (modint(other.value * pow(self.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(other * pow(self.value, MOD - 2, MOD)))

    def __rpow__(self, other):
        return (modint(pow(other.value, self.value, MOD))
                if isinstance(other, modint)
                else modint(pow(other, self.value, MOD)))

    def modinv(self):
        return modint(pow(self.value, MOD - 2, MOD))


def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    from collections import defaultdict
    d = defaultdict(int)
    ans = modint(0)
    for a in A:
        d[a] += 1
        if a == 0:
            if d[a] == 1:
                ans += 1
            elif d[a] == 2:
                ans *= 2
            continue
        if d[a-1] < d[a]:
            return print(0)
        if d[a] == 1:
            ans *= d[a-1]
        elif d[a] == 2:
            ans *= d[a-1] - 1

    print((ans*3))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
MOD = 10**9 + 7


class modint():
    def __init__(self, value):
        self.value = value % MOD

    def __int__(self):
        return int(self.value)

    def __float__(self):
        return float(self.value)

    def __str__(self):
        return str(self.value)

    def __repr__(self):
        return str(self.value)

    def __add__(self, other):
        return (modint(self.value + other.value) if isinstance(other, modint)
                else modint(self.value + other))

    def __sub__(self, other):
        return (modint(self.value - other.value) if isinstance(other, modint)
                else modint(self.value - other))

    def __mul__(self, other):
        return (modint(self.value * other.value) if isinstance(other, modint)
                else modint(self.value * other))

    def __truediv__(self, other):
        return (modint(self.value * pow(other.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(self.value * pow(other, MOD - 2, MOD)))

    def __pow__(self, other):
        return (modint(pow(self.value, other.value, MOD))
                if isinstance(other, modint)
                else modint(pow(self.value, other, MOD)))

    def __eq__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __ne__(self, other):
        return (self.value == other.value if isinstance(other, modint)
                else self.value == (other % MOD))

    def __radd__(self, other):
        return (modint(other.value + self.value) if isinstance(other, modint)
                else modint(other + self.value))

    def __rsub__(self, other):
        return (modint(other.value - self.value) if isinstance(other, modint)
                else modint(other - self.value))

    def __rmul__(self, other):
        return (modint(other.value * self.value) if isinstance(other, modint)
                else modint(other * self.value))

    def __rtruediv__(self, other):
        return (modint(other.value * pow(self.value, MOD - 2, MOD))
                if isinstance(other, modint)
                else modint(other * pow(self.value, MOD - 2, MOD)))

    def __rpow__(self, other):
        return (modint(pow(other.value, self.value, MOD))
                if isinstance(other, modint)
                else modint(pow(other, self.value, MOD)))

    def modinv(self):
        return modint(pow(self.value, MOD - 2, MOD))


def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    from collections import defaultdict
    d = defaultdict(int)
    ans = modint(0)
    for a in A:
        d[a] += 1
        if a == 0:
            if d[a] == 1:
                ans += 1
            elif d[a] == 2:
                ans *= 2
            continue
        if d[a] == 1:
            ans *= d[a-1]
        elif d[a] == 2:
            ans *= d[a-1] - 1

    print((ans*3))


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()]
    from collections import defaultdict
    d = defaultdict(int)
    ans = 0
    for a in A:
        d[a] += 1
        if a == 0:
            if d[a] == 1:
                ans += 1
            elif d[a] == 2:
                ans *= 2
            continue
        if d[a] == 1:
            ans *= d[a-1]
        elif d[a] == 2:
            ans *= d[a-1] - 1

    print((ans*3) % MOD)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
MOD = 1000000007
n = int(input())
a = [0,0,0]
ans = 1
for x in list(map(int, input().split())):
	if x == 0:
		if a[0] == 0:
			a[0] = 1
			ans *= 3
			ans %= MOD
		elif a[1] == 0:
			a[1] = 1
			ans *= 2
			ans %= MOD
		else: 
			a[2] = 1
	else: 
		if x == a[0] and x == a[1] and x == a[2]:
			ans *= 3
			ans %= MOD
			a[0]+= 1
		elif x == a[0] and x == a[1]:
			ans *= 2
			ans %= MOD
			a[0]+= 1
		elif x == a[1] and x == a[2]:
			ans *= 2
			ans %= MOD
			a[1]+= 1
		elif x == a[2] and x == a[0]:
			ans *= 2
			ans %= MOD
			a[2]+= 1
		elif x == a[0]:
			a[0]+= 1
		elif x == a[1]:
			a[1]+= 1
		elif x == a[2]:
			a[2]+= 1
 
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = [0, 0, 0]  # 同じ帽子をかぶっている人数　降順
B = 10 ** 9 + 7
ans = 1
for i in range(N):
    if A[i] in C:
        ans *= C.count(A[i])
        C[C.index(A[i])] += 1
if ans == 1:
    ans -= 1
print(ans % B)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = [0, 0, 0]  # 同じ帽子をかぶっている人数　降順
B = 10 ** 9 + 7
ans = 1
for i in range(N):
    if A[i] in C:
        ans *= C.count(A[i])
        C[C.index(A[i])] += 1
print(ans % B)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
num = [0 for i in range(3)]
num[0] = 1
suma = 3
mod = 10**9+7
A.pop(0)
for i in A:
    suma = (suma*num.count(i))%mod
    if num[0] == i:
        num[0] += 1
    elif num[1] == i:
        num[1] += 1
    else:
        num[2] += 1
print(suma)

Submission status: Wrong Answer

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

MOD = 1000000007

#色ごとの人数
C=[0,0,0]
ans=1
for a in A:
    #人数が一致する色がなければOUT
    found=False
    count=C.count(a)
    if count==0:
        print(0)
        exit()
    #適当に1人足す
    C[C.index(a)]+=1
    #今見ている人の色の可能性はcount種類
    ans=ans*count%MOD
    print(count)

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [-1, -1, -1]
    ans = 1
    for a in A:
        count = 0
        for i in range(3):
            if RGB[i] == a-1:
                if count == 0:
                    RGB[i] = a
                count += 1
        if count > 1:
            ans *= count
            ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [-1, -1, -1]
    ans = 1
    for a in A:
        mi = None
        count = 0
        for i in range(3):
            if RGB[i] == a-1:
                if mi == None:
                    mi = i
                    RGB[mi] = a
                count += 1
        if count > 1:
            ans *= count
            ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [-1, -1, -1]
    ans = 1
    for a in A:
        mi = -1
        count = 0
        for i in range(3):
            if RGB[i] == a-1:
                if mi == -1:
                    mi = i
                count += 1
        if count > 1:
            ans *= count
            ans %= MOD
        RGB[mi] = a
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [None, None, None]
    ans = 1
    for a in A:
        if a == 0:
            for i in range(3):
                if RGB[i] == None:
                    RGB[i] = 0
                    break
        else:
            mi = -1
            count = 0
            for i in range(3):
                if RGB[i] == a-1:
                    if mi == -1:
                        mi = i
                    count += 1
            if count > 1:
                ans *= count
                ans %= MOD
            RGB[mi] = a

    if RGB[1] is None:
        ans *= 3
        ans %= MOD
    elif RGB[2] is None:
        ans *= 3
        ans %= MOD
    else:
        ans *= 6
        ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    RGB = [None, None, None]
    ans = 1
    for a in A:
        if a == 0:
            for i in range(3):
                if RGB[i] == None:
                    RGB[i] = 0
                    break
            else:
                print(-1)
                return
        else:
            mi = -1
            count = 0
            for i in range(3):
                if RGB[i] == a-1:
                    if mi == -1:
                        mi = i
                    count += 1
            if count > 1:
                ans *= count
                ans %= MOD
            RGB[mi] = a

    if RGB[1] is None:
        ans *= 3
        ans %= MOD
    elif RGB[2] is None:
        ans *= 3
        ans %= MOD
    else:
        ans *= 6
        ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

# numpy and scipy are available for use
import numpy
import scipy
#https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e
#Colorful Hats 2
"""
"""

# reads a single line
N = gi()
tails = [0] * N

ans = 1
for i in range(N):
    tails[i] = 0
    cur = gi()
    #print(cur)
    if cur == 0:
        tails[cur] += 1
        continue

    tail_c = tails[cur - 1]

    ans *= tail_c
    ans %= MOD
    tails[cur - 1] -= 1
    tails[cur] += 1

num_c = 0
for v in tails:
    num_c += v

if num_c == 1:
    ans *= 3
elif num_c > 1:
    ans *= 6

ans %= MOD

print(ans)