Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

import bisect

def main():
    mod=10**9+7
    N,W=MI()
    w=[0]*N
    v=[0]*N
    for i in range(N):
        w[i],v[i]=MI()
    w1=w[0]
    for i in range(N):
        w[i]-=w1
        
    a=set([])
    for i in range(N+1):
        for j in range(3*i+1):
            ww=w1*i+j
            if ww>W:
                break
            a.add(ww)
    a=list(a)
    M=len(a)
    
    
    #dp[i][j]はiまでの商品で,重さa[j]のときの価値の最大
    dp=[[-1]*(M) for _ in range(N+1)]
    dp[0][0]=0
    ans=0
    
    for i in range(N):
        for j in range(M):
            if dp[i][j]!=-1:
                dp[i+1][j]=max(dp[i+1][j],dp[i][j])
                ww=a[j]+w1+w[i]
                if ww<=W:
                    jj=bisect.bisect_left(a,ww)
                    dp[i+1][jj]=max(dp[i+1][jj],dp[i][j]+v[i])

    print(max(dp[-1]))
    
     
    
            

main()


Fixed submission:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

import bisect

def main():
    mod=10**9+7
    N,W=MI()
    w=[0]*N
    w2=[0,0,0,0]#結局4通りしかない
    v2=[[] for _ in range(4)]
    v=[0]*N
    for i in range(N):
        w[i],v[i]=MI()
    w1=w[0]
    for i in range(N):
        w[i]-=w1
        w2[w[i]]+=1
        v2[w[i]].append(v[i])
        
    for i in range(4):
        v2[i].sort(reverse=True)
        
    s=[[0] for _ in range(4)]
    for i in range(4):
        for j in range(len(v2[i])):
            s[i].append(s[i][-1]+v2[i][j])
     
    ans=0       
    for a in range(len(s[0])):
        for b in range(len(s[1])):
            for c in range(len(s[2])):
                for d in range(len(s[3])):
                    ww=b+2*c+3*d+w1*(a+b+c+d)
                    if ww<=W:
                        temp=s[0][a]+s[1][b]+s[2][c]+s[3][d]
                        ans=max(ans,temp)
                        
    print(ans)
                        
        
    

main()
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    if N==1:
        print(1)
        exit()
    temp=0
    ans=[]
    for i in range(1,N):
        temp+=i
        ans.append(i)
        if temp>=N:
            ans.remove(temp-N)
            break
    for i in range(len(ans)):
        print(ans[i])
        

main()


Fixed submission:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    if N==1:
        print(1)
        exit()
    temp=0
    ans=[]
    for i in range(1,N+5):
        temp+=i
        ans.append(i)
        if temp>N:
            ans.remove(temp-N)
            break
        elif temp==N:
            break
    for i in range(len(ans)):
        print(ans[i])
        

main()
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    NT=1
    NA=1
    for i in range(N):
        T,A=MI()
        xt=NT//T
        xa=NA//A
        x=min(xt,xa)
        tempT=T*x
        tempA=A*x
        while tempT<NT or tempA<NA:
            tempT+=T
            tempA+=A
        NT=tempT
        NA=tempA
    print(NT+NA)
            

main()


Fixed submission:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    NT=1
    NA=1
    for i in range(N):
        T,A=MI()
        xt=(NT+T-1)//T
        xa=(NA+A-1)//A
        x=max(xt,xa)
        NT=T*x
        NA=A*x
    print(NT+NA)
            

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    NT=1
    NA=1
    for i in range(N):
        T,A=MI()
        tempT=T
        tempA=A
        while tempT<NT or tempA<NA:
            tempT+=T
            tempA+=A
        NT=tempT
        NA=tempA
    print(NT+NA)
            

main()


Fixed submission:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    NT=1
    NA=1
    for i in range(N):
        T,A=MI()
        xt=(NT+T-1)//T
        xa=(NA+A-1)//A
        x=max(xt,xa)
        NT=T*x
        NA=A*x
    print(NT+NA)
            

main()
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A=MI()
    x=LI()
    M=max(x)
    
    #dp[i][j][k]はi番目まで，j枚で，和がkにできるか
    dp=[[[0]*(N*M+1) for j in range(N+1)] for _ in range(N+1)]
    dp[0][0][0]=1
    
    for i in range(N):
        for j in range(i+1):
            for k in range(N*M+1-x[i]):
                if dp[i][j][k]:
                    dp[i+1][j][k]+=dp[i][j][k]
                    dp[i+1][j+1][k+x[i]]+=dp[i][j][k]
                    

        
    ans=0
    for j in range(1,N+1):        
        ans+=dp[-1][j][j*A]
        
    print(ans)
 

            

main()


Fixed submission:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A=MI()
    x=LI()
    M=max(x)
    
    #dp[i][j][k]はi番目まで，j枚で，和がkにできるか
    dp=[[[0]*(N*M+1) for j in range(N+1)] for _ in range(N+1)]
    dp[0][0][0]=1
    
    for i in range(N):
        for j in range(i+1):
            for k in range(N*M+1-x[i]):
                if dp[i][j][k]:
                    dp[i+1][j][k]+=dp[i][j][k]
                    dp[i+1][j+1][k+x[i]]+=dp[i][j][k]
                    

        
    ans=0
    for j in range(1,N+1):
        if j*A<=N*M:     
            ans+=dp[-1][j][j*A]
        
    print(ans)
 

                
    
        
            
            
    
            
        
        
            

main()
Buggy python submission (verdict: Runtime Error):
f = lambda x: x if p[x]<0 else f(p[x])
N,M = map(int,input().split())
p = [-1]*N
for _ in range(M):
  A,B = map(lambda x:f(int(x)),input().split())
  if A==B: continue
  elif A<B: A,B=B,A
  p[A] += p[B]
  p[B] = A
print(sum(i<0 for i in p)-1)

Fixed submission:
def f(x):
  while p[x]>0: x = p[x]
  return x
N,M = map(int,input().split())
p = [-1]*N
for _ in range(M):
  A,B = map(lambda x:f(int(x)-1),input().split())
  if A==B: continue
  elif A<B: A,B=B,A
  p[A] += p[B]
  p[B] = A
print(sum(i<0 for i in p)-1)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
ans = N-1
d = []
for m in range(M):
  A,B = map(int,input().split())
  for i,di in enumerate(d):
    if set([A,B]).issubset(di):
      d[i] = di|set([A,B])
      break
  else:
    d.append(set([A,B]))
    ans -= 1
print(ans)

Fixed submission:
def f(x):
  while p[x]>0: x = p[x]
  return x
N,M = map(int,input().split())
p = [-1]*N
for _ in range(M):
  A,B = map(lambda x:f(int(x)-1),input().split())
  if A==B: continue
  elif A<B: A,B=B,A
  p[A] += p[B]
  p[B] = A
print(sum(i<0 for i in p)-1)

Buggy python submission (verdict: Runtime Error):
from collections import deque
N,M = map(int,input().split())
E = {i: [] for i in range(N)}
for m in range(M):
  A,B = map(int,input().split())
  E[A].append(B)
  E[B].append(A)
  
q = deque([0])
checked = [False]*N
ans = 0
while q and not(all(checked)):
  n = q.popleft()
  if checked[n]: continue
  checked[n] = True
  for i in E[n]:
    if not checked[i]: q.append(i)
  if all(checked): break
  elif not q:
    ans += 1
    for i in range(N):
      if not checked[i]:
        q.append(i)
        break
  
print(ans)

Fixed submission:
def f(x):
  while p[x]>0: x = p[x]
  return x
N,M = map(int,input().split())
p = [-1]*N
for _ in range(M):
  A,B = map(lambda x:f(int(x)-1),input().split())
  if A==B: continue
  elif A<B: A,B=B,A
  p[A] += p[B]
  p[B] = A
print(sum(i<0 for i in p)-1)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,K = map(int,input().split())
LR = []
for _ in range(K):
  L,R = map(int,input().split())
  LR += list(range(L,R+1))
LR = np.array(LR)
MOD = 998244353
dp = np.zeros(N,"i8")
dp[0] = 1
for i in range(N):
  lr = (i+LR)
  lr = lr[lr<N]
  dp[lr] = (dp[lr]+dp[i])%MOD
print(dp[-1])

Fixed submission:
N,K = map(int,input().split())
LR = []
for _ in range(K): LR.append(list(map(int,input().split())))
MOD = 998244353
dp = [0]*(N+1)
dp[0] = 1
for i in range(N):
  for L,R in LR:
    if i+L<N: dp[i+L] += dp[i]
    if i+R+1<N: dp[i+R+1] -= dp[i]
  if i>0: dp[i+1] = (dp[i+1]+dp[i])%MOD
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,K = map(int,input().split())
LR = []
for _ in range(K):LR.append(list(map(int,input().split())))
MOD = 998244353
dp = np.zeros(N,"i8")
dp[0] = 1
for i in range(N):
  for L,R in LR:
    if i+L<N:
      dp[i+L:i+R+1] += dp[i]
      dp[i+L:i+R+1] %= MOD
print(dp[-1])

Fixed submission:
N,K = map(int,input().split())
LR = []
for _ in range(K): LR.append(list(map(int,input().split())))
MOD = 998244353
dp = [0]*(N+1)
dp[0] = 1
for i in range(N):
  for L,R in LR:
    if i+L<N: dp[i+L] += dp[i]
    if i+R+1<N: dp[i+R+1] -= dp[i]
  if i>0: dp[i+1] = (dp[i+1]+dp[i])%MOD
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
LR = []
for i in range(K):
  L,R = map(int,input().split())
  LR += list(range(L,R+1))
LR = sorted(LR)
MOD = 998244353
dp = [0]*N
dp[0] = 1
for i in range(1,N):
  for j in LR:
    if i-j<0: continue
    else: dp[i] += dp[i-j]
  dp[i] %= MOD
print(dp[-1])

Fixed submission:
N,K = map(int,input().split())
LR = []
for _ in range(K): LR.append(list(map(int,input().split())))
MOD = 998244353
dp = [0]*(N+1)
dp[0] = 1
for i in range(N):
  for L,R in LR:
    if i+L<N: dp[i+L] += dp[i]
    if i+R+1<N: dp[i+R+1] -= dp[i]
  if i>0: dp[i+1] = (dp[i+1]+dp[i])%MOD
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int,input().split()))
MOD = 7+10**9
ans = 0
for i in range(N-1): ans = (ans+As[i]*sum(As[i+1:]))%MOD
print(ans)

Fixed submission:
N = int(input())
As = list(map(int,input().split()))
MOD = 7+10**9
ans = 0
Asum = sum(As)
for i in range(N-1):
  Asum -= As[i]
  ans = (ans+As[i]*Asum)%MOD
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
N = int(input())
As = list(map(int,input().split()))
MOD = 7+10**9
print(sum([(Ai*Aj)%MOD for Ai,Aj in combinations(As,2)])%MOD)

Fixed submission:
N = int(input())
As = list(map(int,input().split()))
MOD = 7+10**9
ans = 0
Asum = sum(As)
for i in range(N-1):
  Asum -= As[i]
  ans = (ans+As[i]*Asum)%MOD
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H,W = map(int,input().split())
CH,CW = map(int,input().split())
DH,DW = map(int,input().split())
maze = [list(input()) for _ in range(H)]
 
def BFS(maze,s,g):
  q1 = deque([s])
  checked = []
  cost = [[-1]*W for _ in range(H)]
  m = 0
  while q1:
    h,w,n = q1.popleft()
    if cost[h][w]<0:
      cost[h][w] = n
      if (h,w)==g: return n
      dp = [(x-2,y-2) for x in range(5) for y in range(5) if (x-2)!=0 or (y-2)!=0]
      for dh,dw in dp:
        if 0<=h+dh<H and 0<=w+dw<W and maze[h+dh][w+dw]==".":
          if (dh,dw) in [(-1,0),(1,0),(0,-1),(0,1)]:
            q1.appendleft((h+dh,w+dw,n))
          else:
            q1.append((h+dh,w+dw,n+1))
  return cost[g[0]][g[1]]
print(BFS(maze,(CH-1,CW-1,0),(DH-1,DW-1)))

Fixed submission:
from collections import deque
H,W = map(int,input().split())
CH,CW = map(int,input().split())
DH,DW = map(int,input().split())
maze = [list(input()) for _ in range(H)]
 
def BFS(maze,s,g):
  q1 = deque([s])
  checked = []
  cost = [[-1]*W for _ in range(H)]
  m = 0
  while q1:
    h,w,n = q1.popleft()
    if cost[h][w]<0:
      cost[h][w] = n
      if (h,w)==g: return n
      dp = [(x-2,y-2) for x in range(5) for y in range(5) if (x-2)!=0 or (y-2)!=0]
      for dh,dw in dp:
        if 0<=h+dh<H and 0<=w+dw<W and maze[h+dh][w+dw]==".":
          if (dh,dw) in [(-1,0),(1,0),(0,-1),(0,1)]:
            q1.appendleft((h+dh,w+dw,n))
          else:
            q1.append((h+dh,w+dw,n+1))
  return cost[g[0]][g[1]]
print(BFS(maze,(CH-1,CW-1,0),(DH-1,DW-1)))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
memo = {"R":0,"G":0,"B":0}
ans = 0
if len(set(S))==3:
  for i,s in enumerate(S):
    a = 1
    for k,v in memo.items():
      a *= 1 if k==s else v
    for j in range(i//2):
      if len(set(s+S[:i+1][::-(j+1)][:3]))==3: a-=1
    ans += a
    memo[s] += 1
print(ans)

Fixed submission:
N = int(input())
S = input()
memo = {"R":0,"G":0,"B":0}
ans = 0
if len(set(S))==3:
  for i,s in enumerate(S):
    a = 1
    for k,v in memo.items(): a *= 1 if k==s else v
    b = sum([len(set(s+S[:i+1][::-(j+1)][:3]))//3 for j in range(i//2)])
    ans += a-b
    memo[s] += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = input()
a,b = [0]*N,[0]*N
for i in range(N):
  c = A.count(str(i+1))
  a[i] = c*(c-1)//2
  b[i] = (c-1)*(c-2)//2
d = sum(a)
for i in map(int,A.split()): print(d-a[i-1]+b[i-1])

Fixed submission:
N = int(input())
As = list(map(int, input().split()))
a,b,c = [0]*N,[0]*N,[0]*N
for i in As:
  a[i-1] += 1
  b[i-1] = a[i-1]*(a[i-1]-1)//2
  c[i-1] = (a[i-1]-1)*(a[i-1]-2)//2

d = sum(b)
print("\n".join([str(d-b[i-1]+c[i-1]) for i in As]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N = int(input())
As = np.array(list(map(int, input().split())))
a,b = [0]*N,[0]*N
for i in range(N):
  c = np.sum(As==(i+1))
  a[i] = c*(c-1)//2
  b[i] = (c-1)*(c-2)//2
d = sum(a)
for i in As: print(d-a[i-1]+b[i-1])

Fixed submission:
N = int(input())
As = list(map(int, input().split()))
a,b,c = [0]*N,[0]*N,[0]*N
for i in As:
  a[i-1] += 1
  b[i-1] = a[i-1]*(a[i-1]-1)//2
  c[i-1] = (a[i-1]-1)*(a[i-1]-2)//2

d = sum(b)
print("\n".join([str(d-b[i-1]+c[i-1]) for i in As]))
Buggy python submission (verdict: Runtime Error):
from functools import reduce
from operator import and_
N = int(input())
ans = reduce(and_, [(i%3==0) or (i%5==0) for i in map(int,input().split()) if i%2==0])
print("APPROVED" if ans else "DENIED")

Fixed submission:
from functools import reduce
from operator import and_
N = int(input())
As = [(i%3==0) or (i%5==0) for i in map(int,input().split()) if i%2==0]
ans = reduce(and_, As, True) if len(As)>0 else True
print("APPROVED" if ans else "DENIED")

Buggy python submission (verdict: Runtime Error):
from functools import reduce
from operator import and_
N = int(input())
ans = reduce(and_, [(i%3==0) or (i%5==0) for i in map(int,input().split()) if i%2==0])
print("APPROVED" if ans else "DENIED")

Fixed submission:
from functools import reduce
from operator import and_
N = int(input())
As = [(i%3==0) or (i%5==0) for i in map(int,input().split()) if i%2==0]
ans = reduce(and_, As, True) if len(As)>0 else True
print("APPROVED" if ans else "DENIED")
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
N = int(input())
XL = [list(map(int,input().split())) for _ in range(N)]
area = sorted([[x-l,x+l] for x,l in XL], key=lambda x: x[1])
area_ = [area[0]]
for i in range(1,N):
  if reduce(lambda x,y: x and (area[i][0]>=y[1]), area_, True):
    area_.append(area[i])
print(len(area_))

Fixed submission:
N = int(input())
XL = sorted([list(map(int,input().split())) for _ in range(N)], key=lambda x: x[0]+x[1])
n = 1
S_,T_ = XL[0][0]-XL[0][1], XL[0][0]+XL[0][1]
for i in range(1,N):
  S,T = XL[i][0]-XL[i][1], XL[i][0]+XL[i][1]
  if not (S < T_):
    n += 1
    S_,T_ = S,T
print(n)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = map(int, input().split())
n,m = 1,0
if 1 not in A: print(-1)
else:
  for i in range(N):
    if A[i] == n: n += 1
    else: m += 1
  print(m)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
n,m = 1,0
if 1 not in A: print(-1)
else:
  for i in range(N):
    if A[i] == n: n += 1
    else: m += 1
  print(m)
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))
s = 0
for i in range(60):
  B = [((1<<i)&Ai)>0  for Ai in A]
  s += (1<<i) * B.count(True) * B.count(False)
print(s%MOD)

Fixed submission:
MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))
s = 0
n = len(bin(max(A))[2:])
for i in range(n):
  b = 1<<i
  N_t = sum([1 for Ai in A if (b&Ai)>0])
  N_f = N-N_t
  s += (1<<i) * N_t * N_f
print(s%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))
s = 0
for i in range(N-1):
    for j in range(i+1, N):
      s = (s + (A[i]^A[j])) % MOD
print(s)

Fixed submission:
MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))
s = 0
n = len(bin(max(A))[2:])
for i in range(n):
  b = 1<<i
  N_t = sum([1 for Ai in A if (b&Ai)>0])
  N_f = N-N_t
  s += (1<<i) * N_t * N_f
print(s%MOD)
Buggy python submission (verdict: Runtime Error):
from functools import reduce
from operator import add
X = int(input())
if X < 100: print(0)
else:
  dp = [1] + [0]*X
  dp[100] = dp[101] = dp[102] = dp[103] = dp[104] = dp[105] = 1
  for i in range(106,X+1): dp[i] = min(reduce(add, [dp[i-(100+j)] for j in range(6)]),1)
  print(dp[-1])

Fixed submission:
from functools import reduce
from operator import add
X = int(input())
if X <= 105:
  if X<100:  print(0)
  else: print(1)
else:
  dp = [1] + [0]*X
  dp[100] = dp[101] = dp[102] = dp[103] = dp[104] = dp[105] = 1
  for i in range(106,X+1): dp[i] = min(reduce(add, [dp[i-(100+j)] for j in range(6)]),1)
  print(dp[-1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = map(int, input().split())
xyz,T = [0,0,0],1
for Ai in A:
  n_Ai = xyz.count(Ai)
  T = (T*n_Ai))%1000000007
  if n_Ai>0: xyz.count(Ai)xyz[xyz.index(Ai)] += 1
print(T)

Fixed submission:
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  if xyz.count(Ai)>0:
    T = (T * xyz.count(Ai))%1000000007
    xyz[xyz.index(Ai)] += 1
  else:
    T = 0
    break
print(T)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = map(int, input().split())
xyz = [0,0,0]
T = 1
for Ai in A:
  T = (T*xyz.count(Ai))%1000000007
  xyz[xyz.index(Ai)] += 1
print(T)


Fixed submission:
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  if xyz.count(Ai)>0:
    T = (T * xyz.count(Ai))%1000000007
    xyz[xyz.index(Ai)] += 1
  else:
    T = 0
    break
print(T)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = map(int, input().split())
xyz,T = [0,0,0],1
for Ai in A:
  T = (T*xyz.count(Ai))%1000000007
  xyz[xyz.index(Ai)] += 1
print(T)

Fixed submission:
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  if xyz.count(Ai)>0:
    T = (T * xyz.count(Ai))%1000000007
    xyz[xyz.index(Ai)] += 1
  else:
    T = 0
    break
print(T)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  T = (T * sum([i==Ai for i in xyz]))%1000000007
  xyz[xyz.index(Ai)] += 1
print(T)


Fixed submission:
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  if xyz.count(Ai)>0:
    T = (T * xyz.count(Ai))%1000000007
    xyz[xyz.index(Ai)] += 1
  else:
    T = 0
    break
print(T)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  T = ((T%1000000007) * (sum([i==Ai for i in xyz])%1000000007))%1000000007
  xyz[xyz.index(Ai)] += 1
print(T)

Fixed submission:
N = int(input())
A = map(int, input().split())

T = 1
xyz = [0]*3
for Ai in A:
  if xyz.count(Ai)>0:
    T = (T * xyz.count(Ai))%1000000007
    xyz[xyz.index(Ai)] += 1
  else:
    T = 0
    break
print(T)
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
from operator import mul
N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N-1)]
E_n = {k: 0 for k in ab}
for i in range(1,N+1):
    items = {k:v for k,v in E_n.items() if i in k}
    c = 1
    if items == {}: continue
    elif reduce(mul, items.values()) != 0: continue
    for k in items.keys():
      if items[k] != 0: continue
      while True:
        if c not in items.values():
          items[k] = c
          break
        else: c += 1
    E_n.update(items)
    #if reduce(mul, E_n.values()) != 0: break

print(len(set(E_n.values())))
for k in ab: print(E_n[k])


Fixed submission:
N = int(input())
edge = {}
tree = [[] for i in range(N)]
colors = [1]*(N-1)
c_p = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    tree[a].append(b)
    edge[(a,b)] = i

for i in range(N):
    c = 1
    for v_c in tree[i]:
      while c in c_p[i]: c+=1
      colors[edge[(i,v_c)]] = c
      c_p[v_c].append(c)
      c+=1

print(max(colors))
print(*colors, sep="\n")

Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
from operator import mul
N = int(input())
ab = [tuple(map(int, input().split())) for _ in range(N-1)]
E_n = {k: 0 for k in ab}
for i in range(1,N+1):
    items = {k:v for k,v in E_n.items() if i in k}
    c = 1
    if reduce(mul, items.values()) == 0:
      for k in items.keys():
        if items[k] != 0: continue
        while True:
          if c not in items.values():
            items[k] = c
            break
          else: c += 1
      for k,v in items.items(): E_n[k] = v
    if reduce(mul, E_n.values()) != 0: break

print(len(set(E_n.values())))
for k in ab: print(E_n[k])

Fixed submission:
N = int(input())
edge = {}
tree = [[] for i in range(N)]
colors = [1]*(N-1)
c_p = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    tree[a].append(b)
    edge[(a,b)] = i

for i in range(N):
    c = 1
    for v_c in tree[i]:
      while c in c_p[i]: c+=1
      colors[edge[(i,v_c)]] = c
      c_p[v_c].append(c)
      c+=1

print(max(colors))
print(*colors, sep="\n")