Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02618 (python)

Fixed submission:
D = int(input())
C = list(map(int, input().split()))
S = []
con= 0
for _ in range(D):
    S.append(list(map(int, input().split())))
t = [-1]*D
best = [0]*D
last = [0]*26

for i in range(D):#1日ごと
    for x in range(26):#どの問題を選ぶか
        if i==0:
            con = 0
        else:
            con = best[i-1]
        
        con += S[i][x]
        last[x] = i+1
        for j in range(26): #減少量の計算 #問題ごと
            con -= C[j]*(i-last[j]+1)
            
        if con>best[i]:
            last[x]=i+1
            best[i]=con
            t[i]=x+1
            
#print(best)
for i in range(D):
  print(t[i])

Buggy submission (verdict: Runtime Error):
D = int(input())
C = list(map(int, input().split()))
S = []
con= 0
for _ in range(D):
    S.append(list(map(int, input().split())))
t = [-1]*D
best = [0]*D
last = [0]*26

for i in range(D-4):#1日ごと
    
    score = [-1]*26
    
    for x in range(26):#どの問題を選ぶか
        if i==0:
            con = 0
        else:
            con = best[i-1]
        
        con += S[i][x]
        #last[x] = i+1
        for j in range(26): #減少量の計算 #問題ごと
            if not j==x:
                con -= C[j]*(i-last[j]+1)

        score[x]=con
    m = -100000
    ii = 0
    for k in range(26):
        if m<score[k]:
            m=score[k]
            ii=k
    
    last[ii]=i+1
    best[i]=score[ii]
    t[i]=ii+1



p2 = [0]*(26**2)
for i in range(26):
    for j in range(26):
        #print(i*26+j)
        p2[i*26+j]+=S[D-4][i]
        p2[i*26+j]+=S[D-3][j]
        for k in range(26):
            if not k==i:
                p2[i*26+j] -= C[k]*(25-last[k])
        for k in range(26):
            if not k==j:
                if k==i:
                    p2[i*26+j] -= C[k]*2
ma = 0
ind = 0
for h in range(26**2):
    if p2[h]>ma:
        ma=p2[h]
        ind = h
t[D-4]= h//26+1
t[D-3]=h%26+1
last[t[D-4]]=361
last[t[D-3]]=362



p2 = [0]*(26**2)
for i in range(26):
    for j in range(26):
        #print(i*26+j)
        p2[i*26+j]+=S[D-2][i]
        p2[i*26+j]+=S[D-1][j]
        for k in range(26):
            if not k==i:
                p2[i*26+j] -= C[k]*(25-last[k])
        for k in range(26):
            if not k==j:
                if k==i:
                    p2[i*26+j] -= C[k]*2
ma = 0
ind = 0
for h in range(26**2):
    if p2[h]>ma:
        ma=p2[h]
        ind = h
t[D-2]= h//26 +1
t[D-1]=h%26 +1
        
    
    
    
    
for i in range(D):
    print(t[i])
#print(best)
Problem p02678 (python)

Fixed submission:
from collections import deque
n, m  = map(int, input().split())
a  = []
b = [[] for _ in range(n)]
for i in range(m):
    a.append(list(map(int, input().split())))
    

for i in range(m):
    b[a[i][0]-1].append(a[i][1])
    b[a[i][1]-1].append(a[i][0])

seen = [-1]*n
seen[0] = 1
que = deque([1])
while que:
    v = que.popleft()
    for i in b[v-1]:
        if seen[i-1] != -1:
            continue
        que.append(i)
        seen[i-1] = v

for i in range(n):
    if seen[i] == -1:
        print("No")
else:
    del seen[0]
    print("Yes")
    for i in range(n-1):
        print(seen[i])

Buggy submission (verdict: Runtime Error):
from collections import deque
n, m  = map(int, input().split())
a  = []
b = [[] for _ in range(m)]
for i in range(m):
    a.append(list(map(int, input().split())))
    

for i in range(m):
    b[a[i][0]-1].append(a[i][1])
    b[a[i][1]-1].append(a[i][0])

seen = [-1]*n
seen[0] = 1
que = deque([1])
while que:
    v = que.popleft()
    for i in b[v-1]:
        if seen[i-1] != -1:
            continue
        que.append(i)
        seen[i-1] = v

for i in range(n):
    if seen[i] == -1:
        print("No")
else:
    del seen[0]
    print("Yes")
    for i in range(n-1):
        print(seen[i])
Problem p02681 (python)

Fixed submission:
s = str(input())
t = str(input())
ans = True
for i in range(len(s)):
    if s[i] == t[i]:
        continue
    else:
        ans =False
if len(s) != len(t)-1:
    ans = False

if ans:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
import sys
n, k = map(int, input().split())
a = list(map(int, input().split())) 

visited = [None]*n
cur = 0
for i in range(k):
    if visited[cur] is None:
        visited[cur] = i
        cur = a[cur]-1
    else:
        break
else:
    print(cur+1)
	sys.exit()

k -= i
k %= i-visited[cur]

for i in range(k):
    cur = a[cur]-1

print(cur+1)
Problem p02684 (python)

Fixed submission:
import sys
n, k = map(int, input().split())
a = list(map(int, input().split())) 

visited = [None]*n
cur = 0
for i in range(k):
    if visited[cur] is None:
        visited[cur] = i
        cur = a[cur]-1
    else:
        break
else:
    print(cur+1)
    sys.exit()

k -= i
k %= i-visited[cur]

for i in range(k):
    cur = a[cur]-1

print(cur+1)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split())) 

visited = [None]*n
cur = 0
for i in range(k):
    if visited[cur] is None:
        visited[cur] = i
        cur = a[cur]-1
    else:
        break
else:
    print(cur+1)


k -= i
k %= i-visited[cur]

for i in range(k):
    cur = a[cur]-1

print(cur+1)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))

ans =1
now = [1]
ed = set([1])

for i in range(n+1):
    if a[now[i]-1] ==2:
        break
    now.append(a[now[i]-1])
    ed = set(now)
    
t = len(now)-now.index(a[now[i]-1])
f = now.index(a[now[i]-1])


if k >= f + t:
    p = (k-f)%t
    k = f + p
    

print(now[k])
Problem p02727 (python)

Fixed submission:
x, y, a, b, c = map(int, input().split())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted(p)
q = sorted(q)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r = r + p
r = r + q
r = sorted(r)
print(sum(r[(len(r)-x-y):]))

Buggy submission (verdict: Runtime Error):
x, y, a, b, c = map(int, input().split())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted(p)
q = sorted(q)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r.append(p)
r.append(q)
r = sorted(r)
print(sum(r[(len(r)-x-y):]))

Buggy submission (verdict: Runtime Error):
x, y, a, b, c = map(int, input().split())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted(p)
q = sorted(q)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r.append(p)
r.append(q)
sort(r)
print(sum(r[(len(r)-x-y):]))

Buggy submission (verdict: Runtime Error):
x, y, a, b, c = map(int, input().split())
p =list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
p = sorted(p)
p = sorted(p)

p = p[(len(p)-x):]
q = q[(len(q)-y):]
r=r+p
r=r+q
r = sorted(r)
print(sum(r[(len(r)-x-y):]))
Problem p02882 (python)

Fixed submission:
import math
a, b, x = map(int, input().split())
if a*a*b==x:
    print(0)
elif a*a*b<=2*x:
    print(90-math.degrees(math.atan(a**3/(2*b*a*a-2*x))))
else:
    print(90-math.degrees(math.atan(2*x/(a*b*b))))

Buggy submission (verdict: Runtime Error):
import math
a, b, x = map(int, input().split())
if a*a*b<=2*x:
    print(90-math.degrees(math.atan(a**3/(2*b*a*a-2*x))))
else:
    print(90-math.degrees(math.atan(2*x/(a*b*b))))
Problem p02553 (python)

Fixed submission:
n = list(map(int,input().split()))


ans = n[1] * n[3]
ans = max(n[0]*n[2],ans)
ans = max(n[0]*n[3],ans)
ans = max(n[1]*n[2],ans)
ans = max(n[1]*n[3],ans)
print(ans)

Buggy submission (verdict: Runtime Error):
n = list(map(int,input().split()))

x = False
y = False

if max(n[0],n[1]) < 0:
    x = True
    
if max(n[2],n[3]) < 0:
    y = True

if n[1] > 0:
    if n[3] > 0:
        ans = n[1] * n[3]
    else:
        if n[0] < 0:
            ans = n[0] * n[2]
        else:
            ans = n[0] * n[3]
elif n[3] > 0:
    if n[2] < 0:
        ans = n[0] * n[2]
    else:
        ans = n[1] * n[2]
if n[1] == 0 and n[3] == 0:
    ans = n[0] * n[2]

print(ans)
Problem p02574 (python)

Fixed submission:
if True:
    N = int(input())
    As = list(map(int, input().split()))

    maxA = 10**6 + 6
    c = [0] * (maxA+1)
    for A in As:
        c[A] += 1
    isPrime = True
    cnt = 0
    for i in range(2, maxA+1):
        cnt = sum(c[i::i])
        if cnt == N:
            # Asはすべてiの約数
            print("not coprime")
            exit()
        if cnt >= 2:
            isPrime = False
        
    else:
        if isPrime:
            print("pairwise coprime")
        else:
            print("setwise coprime")

Buggy submission (verdict: Runtime Error):
import math
import fractions


def make_prime_and_deleted_by_list(n):
    prime_list = [i for i in range(2, n+1)]
    deleted_by_list = [i for i in range(n+1)]
    pi = 0
    target_prime = prime_list[pi]
    while(target_prime < math.sqrt(n)):
        old_prime_list = prime_list.copy()
        prime_list = prime_list[:pi+1]
        for i in old_prime_list:
            if i % target_prime == 0:
                deleted_by_list[i] = target_prime
            else:
                if i > prime_list[pi]:
                    prime_list.append(i)
        pi += 1
        target_prime = prime_list[pi]
    return prime_list, deleted_by_list

def make_factor_list(n, deleted_by_list, factor_list):
    if n == 1:
        return factor_list
    else:
        factor = deleted_by_list[n]
        factor_list.append(factor)
        return make_factor_list(int(n/factor), deleted_by_list, factor_list)

def calc_gcd(As):
    ans = As[0]
    for i in range(1, len(As)):
        ans = fractions.gcd(ans, As[i])
    return ans
        
N = int(input())
As = list(map(int, input().split()))
_, d = make_prime_and_deleted_by_list(max(As))

As_factor_list = []
for A in As:
    As_factor_list.extend(set(make_factor_list(A, d, [])))

if len(set(As_factor_list)) == len(As_factor_list):
    print("pairwise coprime")

else:
    if calc_gcd(As) == 1:
        print("setwise coprime")
    else:
        print("not coprime")
Problem p02582 (python)

Fixed submission:
S = input()

if S == "RRR":
  print(3)
elif S in ["RSS", "SRS", "SSR", "RSR"]:
  print(1)
elif S in ["RRS", "SRR"]:
  print(2)
else:
  print(0)

Buggy submission (verdict: Runtime Error):
S = input()
suma = 0
a = t

for s in range(len(S)):
  if s == a:
    suma += 1
  else:
    suma = 0
    a = s
    
print(suma)
Problem p02607 (python)

Fixed submission:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
A = A.reshape((int(N/2), 2))[:,0] if N % 2 == 0 else np.append(A, 2, 100).reshape((int((N+1)/2), 2))[:, 0]
print((A % 2 == 1).sum())

Buggy submission (verdict: Runtime Error):
import numpy as np
N = int(input())
A = np.array(li,
A = A.reshape((int(N/2), 2))[:,0] if N % 2 == 0 else np.append(A, 2, 100).reshape((int((N+1)/2), 2))[:, 0]
print((A % 2 == 1).sum())
Problem p02573 (python)

Fixed submission:
import networkx as nx
import sys

G = nx.Graph()

n, m = map(int, input().split())
if m == 0:
    print(1)
    sys.exit()
for i in range(m):
    a, b = map(int, input().split())
    G.add_edge(a, b)

largest_cc = max(nx.connected_components(G), key=len)
print(len(largest_cc))

Buggy submission (verdict: Runtime Error):
import networkx as nx

G = nx.Graph()

n, m = map(int, input().split())
for i in range(m):
    a, b = map(int, input().split())
    G.add_edge(a, b)

largest_cc = max(nx.connected_components(G), key=len)
print(len(largest_cc))

Buggy submission (verdict: Runtime Error):
import networkx as nx

G = nx.Graph()

n, m = map(int, input().split())
for i in range(m):
    a, b = map(int, input().split())
    G.add_edge(a, b)

largest_cc = max(nx.connected_components(G), key=len)
print(len(largest_cc))
Problem p02612 (python)

Fixed submission:
n = int(input())
while(n > 0):
    n -= 1000
print(-n)

Buggy submission (verdict: Runtime Error):
n = input()
while(n > 0):
    n -= 1000
print(-n)
Problem p02574 (python)

Fixed submission:
import sys
input = sys.stdin.readline
from math import gcd
from functools import reduce
from collections import defaultdict


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def is_pairwise_coprime(A):
    n = max(A)
    base = [False for i in range(n+1)]
    for a in A:
        if a > 1 and base[a]:
            return False
        base[a] = True
    
    is_prime = [True for i in range(n+1)]
    for i in range(2, n+1):
        if is_prime[i]:
            include_number = set()
            if base[i]:
                include_number.add(i)
            for j in range(i+i, n+1, i):
                is_prime[j] = False
                if base[j]:
                    include_number.add(j)
            if len(include_number) >= 2:
                return False
    return True


def solve(N, A):
    if is_pairwise_coprime(A):
        return "pairwise coprime"
    if reduce(gcd, A) == 1:
        return "setwise coprime"
    return "not coprime"


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from math import gcd
from functools import reduce
from collections import Counter


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def prime_factorization(n):
    """
    nの素因数分解を求める
    """
    c = Counter()
    i = 2
    while i * i <= n:
        while n % i == 0:
            n //= i
            c[i] += 1
        i += 1
    if n > 1:
        c[n] += 1
    return c


def solve(N, A):
    C = Counter()
    for a in A:
        primes = prime_factorization(a)
        for key in primes.keys():
            C[key] += 1
    if C.most_common()[0][1] == 1:
        return "pairwise coprime"
    if reduce(gcd, A) == 1:
        return "setwise coprime"
    return "not coprime"


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))
Problem p02642 (python)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200000)
from collections import defaultdict
from bisect import bisect_left


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def prime_factorization(n, primes):
    """
    nの素因数分解を求める
    """
    prime_factors = defaultdict(int)
    if n < 2:
        return prime_factors
    for p in primes:
        if p * p > n:
            break
        while n % p == 0:
            prime_factors[p] += 1
            n //= p
    if n > 1:
        prime_factors[n] = 1
    return prime_factors


def sieve(n):
    """
    n以下の素数を昇順に列挙する
    """
    is_prime = [True for i in range(n+1)]
    primes = []
    for i in range(2, n+1):
        if is_prime[i]:
            primes.append(i)
            for j in range(i+i, n+1, i):
                is_prime[j] = False
    return primes


def solve(N, A):
    A.sort()
    AMAX = max(A)
    ans = 0
    is_div = [True for i in range(AMAX+1)]
    for i in range(N):
        a = A[i]
        if is_div[a]:
            if not ((i > 0 and A[i] == A[i-1]) or (i < N-1 and A[i] == A[i+1])):
                ans += 1
            for j in range(a, AMAX+1, a):
                is_div[j] = False
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(200000)
from collections import defaultdict
from bisect import bisect_left


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def sieve(n):
    """
    n以下の素数を昇順に列挙する
    """
    is_prime = [True for i in range(n+1)]
    primes = []
    for i in range(2, n+1):
        if is_prime[i]:
            primes.append(i)
            for j in range(i+i, n+1, i):
                is_prime[j] = False
    return primes


def solve(N, A, AMAX=10**3):
    A.sort()
    primes = sieve(AMAX)
    P = len(primes)
    table = [[False for i in range(P)] for j in range(N)]
    L = [[False for i in range(P)] for j in range(N+1)]
    for i in range(N):
        a = A[i]
        for j in range(P):
            p = primes[j]
            if p * p > a:
                break
            while a % p == 0:
                table[i][j] = True
                a //= p
        if a > 1:
            idx = bisect_left(primes, a)
            table[i][idx] = True
    for i in range(N):
        for j in range(P):
            L[i+1][j] = L[i][j] ^ table[i][j]

    ans = 0
    A.append(A[-1])
    for i in range(N):
        not_div = True
        if A[i] == A[i+1]:
            continue
        for j in range(P):
            if table[i][j] and (i > 0 and L[i-1][j]):
                not_div = False
                break
        if not_div:
            ans += 1
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))
Problem p02787 (python)

Fixed submission:
from collections import deque


def read():
    H, N = list(map(int, input().strip().split()))
    A = list()
    B = list()
    for i in range(N):
        a, b = list(map(int, input().strip().split()))
        A.append(a)
        B.append(b)
    return H, N, A, B


def solve(H, N, A, B, INF=10**9):
    BMAX = max(B)
    dp = [[INF for i in range(N)] for h in range(H + BMAX + 1)]
    for i in range(0, N):
        dp[0][i] = 0
    for h in range(0, H + BMAX + 1):
        for i in range(N):
            if i == 0:
                if h - A[i] >= 0:
                    dp[h][i] = dp[h - A[i]][i] + B[i]
            else:
                if h - A[i] >= 0:
                    dp[h][i] = min(dp[h][i-1], dp[h - A[i]][i] + B[i])
                else:
                    dp[h][i] = dp[h][i-1]
    min_b = INF
    for h in range(H, H + BMAX + 1):
        for i in range(N):
            min_b = min(min_b, dp[h][i])
    return min_b


if __name__ == "__main__":
    inputs = read()
    print("%s" % solve(*inputs))

Buggy submission (verdict: Runtime Error):
import numpy as np


def read():
    H, N = list(map(int, input().strip().split()))
    A = list()
    B = list()
    for i in range(N):
        a, b = list(map(int, input().strip().split()))
        A.append(a)
        B.append(b)
    return H, N, A, B


def solve(H, N, A, B, INF=10**9):
    BMAX = max(B)
    dp = np.empty(H + BMAX + 1, dtype=np.int32)
    dp[:] = INF
    dp[0] = 0
    for h in range(0, H + BMAX + 1, A[0]):
        dp[h] = dp[h-A[0]] + b[0]
    
    for h in range(0, H + BMAX + 1):
        for i in range(N):
            if h - A[i] >= 0:
                dp[h] = min(dp[h], dp[h - A[i] + B[i])
    return dp[H]


if __name__ == "__main__":
    inputs = read()
    print("%s" % solve(*inputs))
Problem p02789 (python)

Fixed submission:
def read():
    N, M = list(map(int, input().strip().split()))
    return N, M

def solve(N, M):
    return 'Yes' if N == M else 'No'


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Buggy submission (verdict: Runtime Error):
import math
import collections


def modinv(n, p):
    """
    逆元のmod pを求める
    """
    if n >= p:
        raise ValueError('p must be larger than n')
    return pow(n, p-2, p)


def prime_sieve(n):
    """
    n以下の素数を昇順に列挙する
    エラトステネスの篩 計算量O(Nlog(log(N)))
    """
    is_prime = [True for i in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, n+1):
        if is_prime[i]:
            for j in range(i+i, n+1, i):
                is_prime[j] = False
    return [i for i in range(2, n+1) if is_prime[i]]


def prime_factorization(it):
    """
    数値の素因数分解をリストで返すイテレータ
    エラトステネスの篩を応用する 計算量O(A+Nlog(log(N)))
    """
    n = max(it)
    factor = [1 for i in range(n+1)]
    factor[0] = 0
    factor[1] = 1
    for i in range(2, n+1):
        if factor[i] == 1:
            for j in range(i, n+1, i):
                factor[j] = i
    for a in it:
        if a < 2:
            yield list()
        else:
            prime_factors = list()
            while a != factor[a]:
                prime_factors.append(factor[a])
                a //= factor[a]
            prime_factors.append(factor[a])
            yield list(sorted(prime_factors))


def lcm(x, p=-1):
    """
    配列xに対する最小公倍数 LCM(x_1, x_2, ..., x_n) を求める
    p > 0のときはLCM mod pを返す
    """
    max_prime_counts = dict()
    for prime in prime_sieve(max(x)):
        max_prime_counts[prime] = 0
    pfs = prime_factorization(x)
    for pf in pfs:
        prime_count = collections.Counter(pf)
        for prime in prime_count.keys():
            max_prime_counts[prime] = max(max_prime_counts[prime], prime_count[prime])
    lcm_value = 1
    for prime, factor in max_prime_counts.items():
        if p > 0:
            lcm_value *= pow(prime, factor, p)
            lcm_value %= p
        else:
            lcm_value *= pow(prime, factor)
    return lcm_value


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A, MOD=1000000007):
    bsum = 0
    for i in range(N):
        bsum += modinv(A[i], MOD)
        bsum %= MOD
    bsum *= lcm(A, p=MOD)
    bsum %= MOD
    return bsum


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))
Problem p02845 (python)

Fixed submission:
def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def next_state(c, a, n_patterns):
    # c := c[0] >= c[1] >= c[2]
    idx = -1
    for i in range(3):
        if c[i] == a:
            idx = i
            break
    if idx == -1:
        return -1, -1
    n_equals = 1
    n_equals += 1 if c[idx%3] == c[(idx + 1) % 3] else 0
    n_equals += 1 if c[idx%3] == c[(idx + 2) % 3] else 0
    c[idx%3] += 1
    n_patterns *= n_equals
    n_patterns %= 1000000007
    return c, n_patterns

def solve(N, A):
    c = [0, 0, 0]
    n = 1
    for a in A:
        c, n = next_state(c, a, n)
        if c == -1 and n == -1:
            return 0
    return n
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)

Buggy submission (verdict: Runtime Error):
def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def next_state(c, a, n_patterns):
    # c := c[0] >= c[1] >= c[2]
    idx = c.index(a)
    n_equals = 1
    n_equals += 1 if c[idx%3] == c[(idx + 1) % 3] else 0
    n_equals += 1 if c[idx%3] == c[(idx + 2) % 3] else 0
    c[idx%3] += 1
    n_patterns *= n_equals
    n_patterns %= 1000000007
    return c, n_patterns

def solve(N, A):
    c = [0, 0, 0]
    n = 1
    for a in A:
        c, n = next_state(c, a, n)
    return n
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)

Buggy submission (verdict: Runtime Error):
def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def next_state(c, a, n_patterns):
    # c := c[0] >= c[1] >= c[2]
    idx = c.index(a)
    n_equals = 1
    n_equals += 1 if c[idx] == c[(idx + 1) % 3] else 0
    n_equals += 1 if c[idx] == c[(idx + 2) % 3] else 0
    c[idx] += 1
    n_patterns *= n_equals
    n_patterns %= 1000000007
    return c, n_patterns

def solve(N, A):
    c = [0, 0, 0]
    n = 1
    for a in A:
        c, n = next_state(c, a, n)
    return n
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)

Buggy submission (verdict: Runtime Error):
def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def next_state(c, a, n_patterns):
    # c := c[0] >= c[1] >= c[2]
    idx = c.index(a)
    n_equals = 1
    n_equals += 1 if c[idx] == c[(idx + 1) % 3] else 0
    n_equals += 1 if c[idx] == c[(idx + 2) % 3] else 0
    c[idx] += 1
    n_patterns *= n_equals
    n_patterns %= 1000000007
    return c, n_patterns

def solve(N, A):
    c = [0, 0, 0]
    n = 1
    for a in A:
        c, n = next_state(c, a, n)
    return n
    
if __name__ == '__main__':
    inputs = read()
    output = solve(*inputs)
    print("%d" % output)