Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    if x > s:
        continue
    for y in range(x, k + 1):
        if y > s:
            break
        for z in range(y, k + 1):
            if z > s:
                break
            if x + y + z == s:
                if x == y == z:
                    cnt += 1
                elif x == y or y == z:
                    cnt += 3
                else:
                    cnt += 3
print(cnt)


Fixed submission:
a, b, c = input().split(',')
print(a, b, c)

Buggy python submission (verdict: Runtime Error):
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    for y in range(k + 1):
        for z in range(k + 1):
            if x + y + z == s:
                cnt += 1
print(cnt)


Fixed submission:
a, b, c = input().split(',')
print(a, b, c)
Buggy python submission (verdict: Time Limit Exceeded):
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    if x > s:
        continue
    for y in range(x, k + 1):
        if y > s:
            break
        for z in range(y, k + 1):
            if z > s:
                break
            if x + y + z == s:
                if x == y == z:
                    cnt += 1
                elif x == y or y == z:
                    cnt += 3
                else:
                    cnt += 6
print(cnt)


Fixed submission:
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    if x > s:
        continue
    for y in range(x, k + 1):
        if y > s:
            break
        z = s - x - y
        if y <= z and z <= k:
            if x + y + z == s:
                if x == y == z:
                    cnt += 1
                elif x == y or y == z:
                    cnt += 3
                else:
                    cnt += 6
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    for y in range(k + 1):
        for z in range(k + 1):
            if x + y + z == s:
                cnt += 1
print(cnt)


Fixed submission:
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    if x > s:
        continue
    for y in range(x, k + 1):
        if y > s:
            break
        z = s - x - y
        if y <= z and z <= k:
            if x + y + z == s:
                if x == y == z:
                    cnt += 1
                elif x == y or y == z:
                    cnt += 3
                else:
                    cnt += 6
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    for y in range(k + 1):
        for z in range(k + 1):
            if x + y + z == s:
                cnt += 1
print(cnt)


Fixed submission:
k, s = [int(x) for x in input().split()]
cnt = 0
for x in range(k + 1):
    if x > s:
        continue
    for y in range(x, k + 1):
        if y > s:
            break
        z = s - x - y
        if y <= z and z <= k:
            if x + y + z == s:
                if x == y == z:
                    cnt += 1
                elif x == y or y == z:
                    cnt += 3
                else:
                    cnt += 6
print(cnt)
Buggy python submission (verdict: Runtime Error):
a, b, x = [int(x) for x in input().split()]
a //= x
b = (b + x - 1)//b
print(b - a + 1)


Fixed submission:
a, b, x = [int(x) for x in input().split()]
a = (a + x - 1)//x
b //= x
print(b - a + 1)
Buggy python submission (verdict: Runtime Error):
mod = 10**9+7
k = int(input())
s = input()
n = len(s)


def inv(a, mod):
    r = [1, 0, a]
    w = [0, 1, mod]
    while w[2] != 1:
        q = r[2]//w[2]
        r_new = [r[0]-q*w[0], r[1]-q*w[1], r[2]-q*w[2]]
        r = w
        w = r_new
    x, y = w[0], w[1]    # a*x+y*mod = 1
    return (mod+x % mod) % mod


max_num = 2*10**5+1
fact = [0 for _ in range(max_num)]
ifact = [0 for _ in range(max_num)]

fact[0] = fact[1] = 1
ifact[0] = ifact[1] = 1

for i in range(2, max_num):
    fact[i] = fact[i-1] * i % mod

ifact[max_num-1] = inv(fact[max_num-1], mod)

for i in range(2, max_num):
    ifact[max_num-i] = (ifact[max_num-i+1] * (max_num-i+1)) % mod


def comb(x, y):
    if x < 0 or y < 0 or x < y:
        return 0
    else:
        return fact[x] * ifact[y] * ifact[x-y] % mod


ans = 0
for i in range(k+1):
    ans += comb(n-1+i, i)*pow(25, i, mod)*pow(26, k-i, mod)
    ans %= mod

print(ans)


Fixed submission:
mod = 10**9+7
k = int(input())
s = input()
n = len(s)


def inv(a, mod):
    r = [1, 0, a]
    w = [0, 1, mod]
    while w[2] != 1:
        q = r[2]//w[2]
        r_new = [r[0]-q*w[0], r[1]-q*w[1], r[2]-q*w[2]]
        r = w
        w = r_new
    x, y = w[0], w[1]    # a*x+y*mod = 1
    return (mod+x % mod) % mod


max_num = 2*10**6+1
fact = [0 for _ in range(max_num)]
ifact = [0 for _ in range(max_num)]

fact[0] = fact[1] = 1
ifact[0] = ifact[1] = 1

for i in range(2, max_num):
    fact[i] = fact[i-1] * i % mod

ifact[max_num-1] = inv(fact[max_num-1], mod)

for i in range(2, max_num):
    ifact[max_num-i] = (ifact[max_num-i+1] * (max_num-i+1)) % mod


def comb(x, y):
    if x < 0 or y < 0 or x < y:
        return 0
    else:
        return fact[x] * ifact[y] * ifact[x-y] % mod


pow25 = [1]
pow26 = [1]
for i in range(10**6):
    pow25.append((pow25[-1]*25) % mod)
    pow26.append((pow26[-1]*26) % mod)

ans = 0
for i in range(k+1):
    tmp = comb(n-1+i, i)*pow25[i]
    tmp %= mod
    tmp *= pow26[k-i]
    tmp %= mod
    ans += tmp
    # ans += comb(n-1+i, i)*pow(25, i, mod)*pow(26, k-i, mod)
    ans %= mod

print(ans)
Buggy python submission (verdict: Runtime Error):
import math
from collections import Counter

mod = 10**9+7
n = int(input())
c = Counter()
cc = Counter()

for _ in range(n):
    a, b = map(int, input().split())
    a, b = a//math.gcd(a, b), b//math.gcd(a, b)
    if b < 0:
        a = -a
        b = -b
    if a == 0 and b == 0:
        c[-1] += 1
        continue

    key = a*(10**19)+b
    c[key] += 1
    cc[key] += 1

l = []

for r in cc.keys():
    if r == -1:
        continue
    ra = c[r]
    
    a, b = r//(10**19), r % (10**19)
    if a >= 0:
        r_inv = (-b)*10**19 + a
    else:
        r_inv = b*(10**19) - a
    rb = c[r_inv]

    if ra >= 0 and rb >= 0:
        l.append([ra, rb])
        c[r] -= 10**9
        c[r_inv] -= 10**9

ans = 1
for a, b in l:
    ans *= pow(2, a, mod)-1 + pow(2, b, mod)-1 + 1
    ans %= mod

ans = (ans-1) + c[-1]
ans %= mod

print(ans)


Fixed submission:
import math
from collections import Counter

mod = 10**9+7
n = int(input())
c = Counter()
cc = Counter()

for _ in range(n):
    a, b = map(int, input().split())
    if b < 0:
        a = -a
        b = -b
    if a == 0 and b == 0:
        c[-1] += 1
        continue
    elif a != 0 and b == 0:
        a, b = 1, 0
    elif a == 0 and b != 0:
        a, b = 0, 1
    else:
        a, b = a//math.gcd(a, b), b//math.gcd(a, b)

    c[(a, b)] += 1
    cc[(a, b)] += 1

l = []

for r in cc.keys():
    if r == -1:
        continue
    ra = c[r]

    a, b = r
    if a == 0:
        r_inv = (1, 0)
    elif b == 0:
        r_inv = (0, 1)
    elif a > 0:
        r_inv = (-b, a)
    else:
        r_inv = (b, -a)
    rb = c[r_inv]

    if ra >= 0 and rb >= 0:
        l.append([ra, rb])
        c[r] -= 10**9
        c[r_inv] -= 10**9

ans = 1
for a, b in l:
    ans *= pow(2, a, mod)-1 + pow(2, b, mod)-1 + 1
    ans %= mod

ans = (ans-1) + c[-1]
ans %= mod

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
k = int(input())

ans = 0

lst = [[0 for _ in range(1, k+1)] for __ in range(k+1)]
cnt = [0 for _ in range(1, k+1)]


for i in range(1, k+1):
    for j in range(1, k+1):
        tmp = gcd(i,j)
        for l in range(1,k+1):
            ans += gcd(tmp,l)
            
print(ans)


Fixed submission:
from fractions import gcd
k = int(input())

ans = 0

lst = [[0 for _ in range(1, k+1)] for __ in range(k+1)]
cnt = [0 for _ in range(1, k+1)]


for i in range(1, k+1):
    for j in range(1, k+1):
        lst[i-1][j-1] = [gcd(i, j)]
        cnt[gcd(i, j)-1] += 1

ans = 0

for i in range(k):
    for j in range(1, k+1):
        ans += cnt[i]*gcd((i+1),j)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
h, w = map(int, input().split())

g = []
for _ in range(h):
    tmp = list(input())
    g.append(tmp)

dp = [[1000000 for _ in range(w)] for __ in range(h)]

dp[0][0] = 0
q = deque([(0, 0)])

while q:
    x, y = q.popleft()
    if x+1 < h:
        if g[x+1][y] != g[x][y]:
            dp[x+1][y] = min(dp[x+1][y], dp[x][y]+1)
        else:
            dp[x+1][y] = min(dp[x+1][y], dp[x][y])
        q.append((x+1, y))
    if y+1 < w:
        if g[x][y+1] != g[x][y]:
            dp[x][y+1] = min(dp[x][y+1], dp[x][y]+1)
        else:
            dp[x][y+1] = min(dp[x][y+1], dp[x][y])
        q.append((x, y+1))

if g[0][0] == '.' and g[h-1][w-1] == '.':
    print(dp[h-1][w-1]//2)
elif g[0][0] != g[h-1][w-1]:
    print((dp[h-1][w-1]+1)//2)
else:
    print(1 + (dp[h-1][w-1]//2))

Fixed submission:
from collections import deque
h, w = map(int, input().split())

g = []
for _ in range(h):
    tmp = list(input())
    g.append(tmp)

dp = [[1000000 for _ in range(w)] for __ in range(h)]

dp[0][0] = 0
# q = deque([(0, 0)])

# while q:
#     x, y = q.popleft()
#     if x+1 < h:
#         if g[x+1][y] != g[x][y]:
#             dp[x+1][y] = min(dp[x+1][y], dp[x][y]+1)
#         else:
#             dp[x+1][y] = min(dp[x+1][y], dp[x][y])
#         q.append((x+1, y))
#     if y+1 < w:
#         if g[x][y+1] != g[x][y]:
#             dp[x][y+1] = min(dp[x][y+1], dp[x][y]+1)
#         else:
#             dp[x][y+1] = min(dp[x][y+1], dp[x][y])
#         q.append((x, y+1))

for i in range(h):
    for j in range(w):
        if i == 0 and j != 0:
            if g[i][j] != g[i][j-1]:
                dp[i][j] = dp[i][j-1] + 1
            else:
                dp[i][j] = dp[i][j-1]
        elif i != 0:
            if j == 0:
                if g[i][0] != g[i-1][0]:
                    dp[i][0] = dp[i-1][0] + 1
                else:
                    dp[i][0] = dp[i-1][0]
            else:
                if g[i][j] != g[i][j-1]:
                    dp[i][j] = min(dp[i][j], dp[i][j-1] + 1)
                else:
                    dp[i][j] = min(dp[i][j], dp[i][j-1])

                if g[i][j] != g[i-1][j]:
                    dp[i][j] = min(dp[i][j], dp[i-1][j]+1)
                else:
                    dp[i][j] = min(dp[i][j], dp[i-1][j])


if g[0][0] == '.' and g[h-1][w-1] == '.':
    print(dp[h-1][w-1]//2)
elif g[0][0] != g[h-1][w-1]:
    print((dp[h-1][w-1]+1)//2)
else:
    print(1 + (dp[h-1][w-1]//2))
Buggy python submission (verdict: Runtime Error):
A, B, M = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

ans = min(a)+min(b)

for i in range(M):
    x,y,c = map(int, input().split())
    ans = min(ans,a[x-1]+b[x-1]-c)

print(ans)

Fixed submission:
A, B, M = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

ans = min(a)+min(b)

for i in range(M):
    x, y, c = map(int, input().split())
    ans = min(ans, a[x-1]+b[y-1]-c)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = [int(x) for x in input()]

if p == 2:
    ans = 0
    for i in range(n):
        if s[i] % 2 == 0:
            ans += i+1
    print(ans)
    exit()

if p == 5:
    ans = 0
    for i in range(n):
        if s[i] == 0 or s[i] == 5:
            ans += i+1
    print(ans)
    exit()


po10 = [1]
for i in range(n-1):
    po10.append((po10[-1]*10) % p)

for i in range(n):
    s[i] *= po10[n-1-i]
    s[i] %= p

ss = [0]

for i in range(n):
    ss.append(ss[-1]+s[i])
    ss[-1] %= p

ans = 0

for i in range(p):
    tmp = ss.count(i)
    ans += tmp*(tmp-1)//2

print(ans)


Fixed submission:
n, p = map(int, input().split())
s = [int(x) for x in input()]

if p == 2:
    ans = 0
    for i in range(n):
        if s[i] % 2 == 0:
            ans += i+1
    print(ans)
    exit()

if p == 5:
    ans = 0
    for i in range(n):
        if s[i] == 0 or s[i] == 5:
            ans += i+1
    print(ans)
    exit()


po10 = [1]
for i in range(n-1):
    po10.append((po10[-1]*10) % p)

for i in range(n):
    s[i] *= po10[n-1-i]
    s[i] %= p

ss = [0]

for i in range(n):
    ss.append(ss[-1]+s[i])
    ss[-1] %= p


ss = sorted(ss)

ll = len(ss)

ans = 0
tmp = [0,0]
for i in range(ll):
    if ss[i] != tmp[0]:
        ans += (i-tmp[1])*(i-tmp[1]-1) //2
        tmp[0] = ss[i]
        tmp[1] = i

ans += (ll-tmp[1])*(ll-tmp[1]-1)//2

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, p = map(int, input().split())
s = [int(x) for x in input()]

if p == 2:
    ans = 0
    for i in range(n):
        if s[i] %2 == 0:
            ans += i+1
    print(ans)
    exit()

if p == 5:
    ans = 0
    for i in range(n):
        if s[i] == 0 or s[i] == 5:
            ans += i+1
    print(ans)
    exit()

for i in range(n):
    s[i] *= pow(10, (n-1-i))
    s[i] %= p

ss = [0]

for i in range(n):
    ss.append(ss[-1]+s[i])
    ss[-1] %= p

ans = 0

for i in range(p):
    tmp = ss.count(i)
    ans += tmp*(tmp-1)//2

print(ans)

Fixed submission:
n, p = map(int, input().split())
s = [int(x) for x in input()]

if p == 2:
    ans = 0
    for i in range(n):
        if s[i] % 2 == 0:
            ans += i+1
    print(ans)
    exit()

if p == 5:
    ans = 0
    for i in range(n):
        if s[i] == 0 or s[i] == 5:
            ans += i+1
    print(ans)
    exit()


po10 = [1]
for i in range(n-1):
    po10.append((po10[-1]*10) % p)

for i in range(n):
    s[i] *= po10[n-1-i]
    s[i] %= p

ss = [0]

for i in range(n):
    ss.append(ss[-1]+s[i])
    ss[-1] %= p


ss = sorted(ss)

ll = len(ss)

ans = 0
tmp = [0,0]
for i in range(ll):
    if ss[i] != tmp[0]:
        ans += (i-tmp[1])*(i-tmp[1]-1) //2
        tmp[0] = ss[i]
        tmp[1] = i

ans += (ll-tmp[1])*(ll-tmp[1]-1)//2

print(ans)
Buggy python submission (verdict: Runtime Error):

def solve(n,k):
    if k == 0:
        return 1
    for i in range(len(n)):
        if n[i] != 0:
            break
    n = n[i:]
    if n == []:
        return 0
    l = len(n)
    ans = 0

    # n-1桁からKこ取る
    tmp1 = 1
    for i in range(1, k+1):
        tmp1 = tmp1*9*(l-i)
    for i in range(1, k+1):
        tmp1 = tmp1 // i

    # 最上位から1つ（最上位の数以外）とる。
    tmp2 = n[0]-1
    for i in range(1, k):
        tmp2 = tmp2*9*(l-i)
    for i in range(1, k):
        tmp2 = tmp2 // i

    # 最上位からその数をとる。
    tmp3 = solve(n[1:],k-1)

    ans = tmp1 + tmp2 + tmp3
    return ans

n = list(map(int, input()))
k = int(input())

print(solve(n,k))


Fixed submission:

def solve(n,k):
    for i in range(len(n)):
        if n[i] != 0:
            break
    n = n[i:]
    if n == []:
        return 0
    l = len(n)
    ans = 0
    # n-1桁からKこ取る
    tmp = 1
    for i in range(1, k+1):
        tmp = tmp*9*(l-i)
    for i in range(1, k+1):
        tmp = tmp // i
    ans += tmp

    # 最上位から1つ（最上位の数以外）とる。
    tmp = n[0]-1
    for i in range(1, k):
        tmp = tmp*9*(l-i)
    for i in range(1, k):
        tmp = tmp // i
    ans += tmp

    # 最上位からその数をとる。
    tmp = 0
    if k == 3:
        tmp += solve(n[1:],k-1)

    elif k == 2:
        for i in range(1, l):
            if n[i] != 0:
                tmp += n[i] + 9*(l-i-1)
                break
    else:
        tmp += 1
    ans += tmp
    return ans

n = list(map(int, input()))
k = int(input())

print(solve(n,k))
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce
import fractions
n, m = map(int, input().split())
a = [int(x)//2 for x in input().split()]


def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


y = lcm_list(a)

for i in range(n):
    if (y // a[i]) % 2 == 0:
        print(0)
        exit()

x = m//y
if x % 2 == 0:
    print(x//2)
else:
    print((x+1)//2)


Fixed submission:
from functools import reduce
import fractions
n, m = map(int, input().split())
a = [int(x)//2 for x in input().split()]

y = a[0]

for i in range(1,n):
    y = y*a[i] // fractions.gcd(y,a[i])
    if y > m:
        print(0)
        exit()

for i in range(n):
    if (y // a[i]) % 2 == 0:
        print(0)
        exit()

x = m//y

if x % 2 == 0:
    print(x//2)
else:
    print((x+1)//2)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))
mod = 10**9+7

bt = [0]*60

ans = 0

for i in range(n):
    s = a[i]
    for j in range(60):
        if((s>>j) & 1):
            ans += (i-bt[j])*(2**j)
            ans %= mod
            bt[j] += 1
        else:
            ans += bt[j]*(2**j)
            ans %= mod


print(ans)


Fixed submission:
n = int(input())
a = list(map(int,input().split()))
mod = 10**9+7

bt = [0]*60

ans = 0

for j in range(60):
    for i in range(n):
        s = a[i]
        if((s>>j) & 1):
            ans += (i-bt[j])*(2**j)
            ans %= mod
            bt[j] += 1
        else:
            ans += bt[j]*(2**j)
            ans %= mod

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))
mod = 10**9 +7

if a[0] != 0:
    print(0)
    exit()

col = [1,0,0]

ans = 3
cnt = 2

for i in range(1,n):
    ans *= col.count(a[i])
    ans %= mod
    col[col.index(a[i])] += 1

print(ans)


Fixed submission:
n = int(input())
a = list(map(int,input().split()))
mod = 10**9 +7

col = [0,0,0]

ans = 1

for i in range(n):
    if not a[i] in col:
        print(0)
        exit()
    else:
        ans *= col.count(a[i])
        ans %= mod
        col[col.index(a[i])] += 1

if sum(col) != n:
    print(0)
    exit()

print(ans%mod)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
input = sys.stdin.readline

n = int(input())

edges = [[] for _ in range(n)]
l = []
for i in range(n-1):
    a,b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
    l.append([a-1,b-1])


ans = [[0 for _ in range(n)] for __ in range(n)]
visited = [False]*n

q = deque([(0,0)])
visited[0] = True

mx = 0
while q:
    key,v = q.popleft()
    i = 1
    for x in edges[v]:
        if not visited[x]:
            if key != i:
                visited[x] = True
                ans[v][x] = i
                ans[x][v] = i
                q.append((i,x))
                mx = max(mx,i)
                i += 1
            else:
                visited[x] = True
                i += 1
                ans[v][x] = i
                ans[x][v] = i
                q.append((i,x))
                mx = max(mx,i)
                i += 1

print(mx)
for i in range(n-1):
    print(ans[l[i][0]][l[i][1]])



Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline

n = int(input())

edges = [[] for _ in range(n)]
l = {}
for i in range(n-1):
    a,b = map(int,input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)
    l[(a-1,b-1)] = i
    l[(b-1,a-1)] = i

ans = [0]*(n-1)

visited = [-1]*n
q = deque([0])
visited[0] = 0

while q:
    v = q.popleft()
    i = 1
    for x in edges[v]:
        if visited[x] < 0:
            if i == visited[v]:
                i += 1
            visited[x] = i
            ans[l[(v,x)]] = i
            q.append(x)
            i += 1

print(max(ans))
for i in range(n-1):
    print(ans[i])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int,input().split()))

aa = [0]*(n+1)

for i in range(n):
    aa[i+1] = aa[i] + a[i]

ans = sum(a)
for i in range(n):
    ans = min(ans,abs(sum(a)-2*aa[i]))

print(ans)

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

aa = [0]*(n+1)

for i in range(n):
    aa[i+1] = aa[i] + a[i]

sm = sum(a)
ans = sm
for i in range(n):
    ans = min(ans,abs(sm-2*aa[i]))

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
d = list(map(int,input().split()))
mod = 998244353

if d[0] != 0:
    print(0)
    exit()

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

if n == 2:
    if d[1] == 1:
        print(1)
        exit()
    else:
        print(0)
        exit()


a = [0]*(n-1)

while d:
    s = d.pop()
    a[s] += 1

if a[0] >= 2:
    print(0)
    exit()

ans = 1

for i in range(n-2):
        ans *= a[i]**a[i+1]
        ans %= mod

print(ans % mod)

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

if d[0] != 0:
    print(0)
    exit()

if n == 1:
        print(1)
        exit()

if n == 2:
    if d[1] == 1:
        print(1)
        exit()
    else:
        print(0)
        exit()


a = [0]*n

while d:
    s = d.pop()
    a[s] += 1

if a[0] >= 2:
    print(0)
    exit()

ans = 1

for i in range(n-1):
        ans *= a[i]**a[i+1]
        ans %= mod

print(ans % mod)

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

if d[0] != 0:
    print(0)
    exit()

a = [0]*(n-1)

while d:
    s = d.pop()
    a[s] += 1

if a[0] >= 2:
    print(0)
    exit()

ans = 1

for i in range(n-2):
        ans *= a[i]**a[i+1]
        ans %= mod

print(ans % mod)

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

if d[0] != 0:
    print(0)
    exit()

if n == 1:
        print(1)
        exit()

if n == 2:
    if d[1] == 1:
        print(1)
        exit()
    else:
        print(0)
        exit()


a = [0]*n

while d:
    s = d.pop()
    a[s] += 1

if a[0] >= 2:
    print(0)
    exit()

ans = 1

for i in range(n-1):
        ans *= a[i]**a[i+1]
        ans %= mod

print(ans % mod)
Buggy python submission (verdict: Runtime Error):
from math import gcd

def div(n,m):
    divisors = [1]
    for i in range(2, 10**7+1):
        if n % i == 0 and m % i == 0:
            divisors.append(i)
            while n % i == 0 and m % i == 0:
                n = n // i
                m = m // i

    return divisors,n,m

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

d,n,m = div(a,b)

if gcd(n,m) > 1:
    print(len(d)+1)
else:
    print(len(d))


Fixed submission:
from fractions import gcd

def div(n,m):
    divisors = [1]
    for i in range(2, 10**7+1):
        if n % i == 0 and m % i == 0:
            divisors.append(i)
            while n % i == 0 and m % i == 0:
                n = n // i
                m = m // i

    return divisors,n,m

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

d,n,m = div(a,b)

if gcd(n,m) > 1:
    print(len(d)+1)
else:
    print(len(d))
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())

l = []

for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    bit_c = 0
    for x in c:
        bit_c = bit_c | (1<<(x-1))
    l.append([a,bit_c])


ans = 10**9

for i in range(2 ** m):
    cost = 0
    chk = 0
    for j in range(m):
        if ((i>>j) &1):
            cost += l[j][0]
            chk = chk | l[j][1]
    if chk == 2**n-1:
        ans = min(ans,cost)

if ans == 10**9:
    print(-1)
else:
    print(ans)

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

l = []

for i in range(m):
    a,b = map(int,input().split())
    c = list(map(int,input().split()))
    bit_c = 0
    for x in c:
        bit_c = bit_c | (1<<(x-1))
    l.append([a,bit_c])

dp = [[float("INF")]*(2**n+1) for _ in range(m+1)]

dp[1][0] = 0
dp[1][l[0][1]] = l[0][0]

for i in range(2,m):
    for j in range(2**n):
        dp[i][j] = dp[i-1][j]
    for j in range(2**n):
        dp[i][j | l[i-1][1]] = min(dp[i][j|l[i-1][1]],dp[i-1][j|l[i-1][1]],dp[i-1][j]+l[i-1][0])

if dp[m-1][2**n-1] >= 10**12:
    print(-1)
else:
    print(dp[m-1][2**n-1])