Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):

MOD = 998244353
def resolve():
    # 多項式 解
    N, S = map(int, input().split())
    A = list(map(int, input().split()))

    q = [0]*(S+1)
    ans = 0
    for i in range(N):
        q[0] += 1
        q2 = [0]*(S+1)
        for j in range(S+1):
            # q *= (1 + x^A[i])
            q2[j] += q[j]
            if j + A[i] <= S:
                q2[j + A[i]] += q[j]
                q2[j + A[i]] %= MOD
        q = q2
        ans += q[S]
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    resolve()


Fixed submission:

MOD = 998244353
def resolve():
    # 多項式 解
    N, S = map(int, input().split())
    A = list(map(int, input().split()))

    q = [0]*(S+1)
    ans = 0
    for i in range(N):
        q[0] += 1
        q2 = [0]*(S+1)
        for j in range(S+1):
            # q *= (1 + x^A[i])
            q2[j] += q[j]
            if j + A[i] <= S:
                q2[j + A[i]] += q[j]
                q2[j + A[i]] %= MOD
        q = q2
        ans += q[S]
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 
MOD = 10**9+7

N, a, b = map(int, inp().split())
ball = ""
while len(ball) < N:
  ball += "b"*a
  ball += "r"*b
  
ans = ball[:N].count("b")
print(ans)  
  

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 
MOD = 10**9+7

N, a, b = map(int, inp().split())
ans = N//(a+b) * a
rest = N%(a+b)
ans += min(a, rest)
print(ans)
Buggy python submission (verdict: Runtime Error):

def resolve():
    l, P = map(int, input().split())
    S = input()[::-1]

    ans = 0
    # 10^r: 2^r * 5^r の為、10と同じ素数
    if P == 2 or P == 5:
        for i in range(l):
            s = int(S[i])
            if s % P == 0:
                ans += i + 1
        return print(ans)

    cnts = [0] * (l + 1)
    cnts[0] = 1
    num, d = 0, 1
    for i in range(l):
        s = int(S[i])
        num += s * d
        d *= 10
        num %= P
        d %= P
        cnts[num] += 1

    for cnt in cnts:
        ans += cnt * (cnt - 1) // 2
    print(ans)


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
    l, P = map(int, input().split())
    S = input()

    ans = 0
    # 10^r: 2^r * 5^r の為、10と同じ素数
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += i + 1
        return print(ans)

    cnts = [0] * P
    cnts[0] = 1
    num, d = 0, 1
    for s in S[::-1]:
        s = int(s)
        num = (num + (s * d)) % P
        d = (10 * d) % P
        cnts[num] += 1

    for cnt in cnts:
        ans += cnt * (cnt - 1) // 2
    print(ans)


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Runtime Error):

from heapq import heappush, heappop
from collections import Counter
class HeapSet:
    def __init__(self):
        self.minQue = []
        self.maxQue = []
        self.counter = Counter()

    def insert(self, x):
        heappush(self.minQue, x)
        #heappush(self.maxQue, -x)
        self.counter[x] += 1

    def erase(self, x):
        self.counter[x] -= 1

    def max(self):
        while self.maxQue and self.counter[-self.maxQue[0]] == 0:
            heappop(self.maxQue)
        return -self.maxQue[0] if self.maxQue else None

    def min(self):
        while self.minQue and self.counter[self.minQue[0]] == 0:
            heappop(self.minQue)
        return self.minQue[0] if self.minQue else None

MOD = 998244353
def resolve():
    def dfs(v):
        res = 1
        for to in to_edge[v]:
            res *= dfs(to)
            res %= MOD
        return res +1

    N = int(input())
    AB = [tuple(map(int, input().split())) for _ in range(N)]
    AB.sort()
    st = HeapSet()
    to_edge = [[] for _ in range(N)]
    # treeを作成
    for i in range(N-1, -1, -1):
        x = AB[i][0] # 初期位置
        d = AB[i][1] # 移動距離

        while st.min() and st.min()[0] < x + d:
            to_edge[i].append(st.min()[1])
            st.erase(st.min())
        st.insert((x,i)) # 初期位置、頂点番号

    ans = 1
    for (x, v) in st.minQue:
        ans *= dfs(v)
        ans %= MOD
    print(ans)

if __name__ == "__main__":
    resolve()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from collections import Counter
class HeapSet:
    def __init__(self):
        self.minQue = []
        self.maxQue = []
        self.counter = Counter()

    def insert(self, x):
        heappush(self.minQue, x)
        #heappush(self.maxQue, -x)
        self.counter[x] += 1

    def erase(self, x):
        self.counter[x] -= 1

    def max(self):
        while self.maxQue and self.counter[-self.maxQue[0]] == 0:
            heappop(self.maxQue)
        return -self.maxQue[0] if self.maxQue else None

    def min(self):
        while self.minQue and self.counter[self.minQue[0]] == 0:
            heappop(self.minQue)
        return self.minQue[0] if self.minQue else None

MOD = 998244353
def resolve():
    def dfs(v):
        res = 1
        for to in to_edge[v]:
            res *= dfs(to)
            res %= MOD
        return res +1

    N = int(input())
    AB = [tuple(map(int, input().split())) for _ in range(N)]
    AB.sort()
    st = HeapSet()
    to_edge = [[] for _ in range(N)]
    # treeを作成
    for i in range(N-1, -1, -1):
        x = AB[i][0] # 初期位置
        d = AB[i][1] # 移動距離

        while st.min() and st.min()[0] < x + d:
            to_edge[i].append(st.min()[1])
            st.erase(st.min())
        st.insert((x,i)) # 初期位置、頂点番号

    ans = 1
    for (x, v) in st.minQue:
        ans *= dfs(v)
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

N, M = map(int, inp().split())
SC = [tuple(map(int, inp().split())) for _ in range(M)]
SC = sorted(SC, key=lambda x:(x[0],-(x[1])))
max_N = (SC[0][1]+1)*(10**(N-1))
min_N = 10**(N-1)

ans = 1005
for i in range(max_N, min_N, -1):
  x = str(i)
  cnt = 0
  for s, c in SC:
    if int(x[s-1]) == c:
      cnt += 1
  if cnt == M:
    ans = min(ans, i)
print(ans if ans < 1005 else -1)    
    
  



Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

def check(n):
  x = str(n)
  if len(x) != N:
    return False
  for s, c in SC:
    if x[s-1] != str(c):
      return False
  return True
  
N, M = map(int, inp().split())
SC = [tuple(map(int, inp().split())) for _ in range(M)]

for i in range(1001):
  if check(i):
    print(i)
    exit()
print(-1)
Buggy python submission (verdict: Time Limit Exceeded):

import sys
sys.setrecursionlimit(10 ** 7)

def resolve():
    N, M,K = map(int, input().split())
    frind = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(lambda x:int(x)-1, input().split())
        frind[a].append(b)
        frind[b].append(a)

    block = [[] for _ in range(N)]
    for _ in range(K):
        a, b = map(lambda x: int(x) - 1, input().split())
        block[a].append(b)
        block[b].append(a)


    D = {}
    parent = [-1]*N
    visited = [False]*N
    for root in range(N):
        if visited[root]:
            continue
        D[root] = set([root])
        stack = [root]
        while stack:
            n = stack.pop()
            visited[n] = True
            parent[n] = root
            for to in frind[n]:
                if visited[to]:
                    continue
                D[root].add(to)
                stack.append(to)

    ans = [0]*N
    for i in range(N):
        gr = D[parent[i]]
        tmp = len(gr)-len(frind[i]) - 1
        for bl in block[i]:
            if bl in gr:
                tmp -= 1
        ans[i] = tmp
    print(*ans)

if __name__ == "__main__":
    resolve()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

N, M, K = map(int, inp().split())
friends = [set() for _ in range(N+1)]
blocked = [set() for _ in range(N+1)]

for _ in range(M):
  a, b = map(int, inp().split())
  friends[a].add(b)
  friends[b].add(a)
for _ in range(K):
  c, d = map(int, inp().split())
  blocked[c].add(d)
  blocked[d].add(c)  

checked = set()
ans = [0]*(N+1)
for i in range(1, N+1):
  if i in checked:
    continue
  chain = set()
  
  stack = list(friends[i])
  while stack:
    friend = stack.pop()
    if friend in checked:
      continue
      
    checked.add(friend)
    chain.add(friend)
    stack.extend(friends[friend])
  cnt = len(chain) - 1 # 自分を引く
  for c in chain:
    ans[c] += cnt - len(chain & (friends[c] | blocked[c]))
    
print(*ans[1:])
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
A = list(map(int, input().split()))

ev = [(a+1)/2 for a in A]
ans = 0
for i in range(len(A)-K+1):
  ans = max(ans, sum(ev[i:i+K]))
print(ans)  

Fixed submission:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, K = map(int, readline().split())
A = np.array(readline().split(), dtype=np.int64)
A = np.array([(1+a)/2 for a in A])

Acum = np.zeros(N+1, np.float64)
Acum[1:] = A.cumsum()

x = Acum[K:] - Acum[:-K]
print(x.max())
Buggy python submission (verdict: Runtime Error):

class Combination:
    def __init__(self, n_max, mod=10 ** 9 + 7):
        # O(n_max + log(mod))
        self.mod = mod
        f = 1
        self.fac = fac = [f]
        for i in range(1, n_max + 1):
            f = f * i % mod
            fac.append(f)
        f = pow(f, mod - 2, mod)
        self.facinv = facinv = [f]
        for i in range(n_max, 0, -1):
            f = f * i % mod
            facinv.append(f)
        facinv.reverse()

    # "n 要素" は区別できる n 要素
    # "k グループ" はちょうど k グループ

    def __call__(self, n, r):  # self.C と同じ
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod

    def nCr(self, n, r):
        if not 0 <= r <= n:
            return 0
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod

def resolve():
    def f(r, c):
        return cmb.nCr(r+c+2, r+1) - 1

    MOD = 10**9+7
    r1,c1,r2,c2 = map(int, input().split())
    r1 -= 1
    c1 -= 1

    cmb = Combination(10**6+5)
    ans = f(r2, c2)
    ans -= f(r1, c2)
    ans -= f(r2, c1)
    ans += f(r1, c1)
    print(ans)


if __name__ == "__main__":
    resolve()


Fixed submission:


class Combination:
    def __init__(self, n_max, mod=10 ** 9 + 7):
        # O(n_max + log(mod))
        self.mod = mod
        f = 1
        self.fac = fac = [f]
        for i in range(1, n_max + 1):
            f = f * i % mod
            fac.append(f)
        f = pow(f, mod - 2, mod)
        self.facinv = facinv = [f]
        for i in range(n_max, 0, -1):
            f = f * i % mod
            facinv.append(f)
        facinv.reverse()

    # "n 要素" は区別できる n 要素
    # "k グループ" はちょうど k グループ

    def __call__(self, n, r):  # self.C と同じ
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod

    def nCr(self, n, r):
        if not 0 <= r <= n:
            return 0
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod

def resolve():
    def f(r, c):
        return cmb.nCr(r+c+2, r+1) - 1

    MOD = 10**9+7
    r1,c1,r2,c2 = map(int, input().split())
    r1 -= 1
    c1 -= 1

    cmb = Combination(r2+c2+5)
    ans = f(r2, c2)
    ans -= f(r1, c2)
    ans -= f(r2, c1)
    ans += f(r1, c1)
    print(ans % MOD)


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
    INF = 1<<60
    H, N = map(int, input().split())
    A, B = [], []
    for i in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    dp = [INF]*(H+1)
    dp[0] = 0

    for h in range(H):
        for i in range(N):
            to = min(H, A[i]+h)
            dp[to] = min(dp[to], dp[h]+B[i])

    print(dp[H])


if __name__ == "__main__":
    resolve()


Fixed submission:

def resolve():
    INF = 1<<60
    H, N = map(int, input().split())
    A, B = [], []
    for i in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    dp = [[INF]*(H+1) for _ in range(N+1)]
    dp[0][0] = 0

    for i in range(N):
        for j in range(H+1):
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
            dp[i+1][min( j + A[ i ], H)] = min(dp[i+1][min( j + A[ i ], H)], dp[i+1][j] + B[i])
    print(dp[N][H])


if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
    INF = 1<<60
    H, N = map(int, input().split())
    A, B = [], []
    for i in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    dp = [[INF]*(H+1) for _ in range(N+1)]
    for i in range(N):
        dp[i][0] = 0

    for i in range(N):
        for j in range(H+1):
            if j < A[i]:
                # 体力がマイナスなっても魔法は使用できる
                dp[i + 1][j] = min(dp[i][j], B[i])
            else:
                dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - A[i]] + B[i])

    print(dp[N][H])

if __name__ == "__main__":
    resolve()


Fixed submission:

def resolve():
    INF = 1<<60
    H, N = map(int, input().split())
    A, B = [], []
    for i in range(N):
        a, b = map(int, input().split())
        A.append(a)
        B.append(b)

    dp = [[INF]*(H+1) for _ in range(N+1)]
    dp[0][0] = 0

    for i in range(N):
        for j in range(H+1):
            dp[i+1][j] = min(dp[i+1][j], dp[i][j])
            dp[i+1][min( j + A[ i ], H)] = min(dp[i+1][min( j + A[ i ], H)], dp[i+1][j] + B[i])
    print(dp[N][H])


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
    MOD = 10 ** 9 + 7
    N = int(input())
    C = sorted(map(int, input().split()))
    P = [1] * (N + 1)
    for i in range(N):
        P[i + 1] *= P[i] * 2
    ans = 0
    for i in range(N):
        # 各ビットの係数: (2^r + 2^r-1 * r) * 2^l
        l, r = i, N - i - 1
        now = P[r]
        if r != 0:
            now += P[r - 1] * r % MOD
        now *= P[l]
        now *= C[i]
        ans += now % MOD
        ans %= MOD
    ans *= P[N]
    print(ans % MOD)


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
    MOD = 10 ** 9 + 7
    N = int(input())
    C = sorted(map(int, input().split()))
    P = [1] * (N + 1)
    for i in range(N):
        P[i + 1] *= P[i] * 2
        P[i + 1] %= MOD
    ans = 0
    for i in range(N):
        # 各ビットの係数: (2^r + 2^r-1 * r) * 2^l
        l, r = i, N - i - 1
        now = P[r]
        if r != 0:
            now += P[r - 1] * r
        now *= P[l]
        now *= C[i]
        ans += now
        ans %= MOD
    ans *= P[N]
    print(ans % MOD)


if __name__ == "__main__":
    resolve()

Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
    MOD = 10 ** 9 + 7
    N = int(input())
    C = sorted(map(int, input().split()))
    P = [1] * (N + 1)
    for i in range(N):
        P[i + 1] *= P[i] * 2
    ans = 0
    for i in range(N):
        # 各ビットの係数: (2^r + 2^r-1 * r) * 2^l
        l, r = i, N - i - 1
        now = P[r]
        if r != 0:
            now += P[r - 1] * r % MOD
        now *= P[l]
        now *= C[i]
        ans += now % MOD
        ans %= MOD
    ans *= P[N]
    print(ans % MOD)


if __name__ == "__main__":
    resolve()

Fixed submission:

def resolve():
    MOD = 10 ** 9 + 7
    N = int(input())
    C = sorted(map(int, input().split()))
    P = [1] * (N + 1)
    for i in range(N):
        P[i + 1] *= P[i] * 2
        P[i + 1] %= MOD
    ans = 0
    for i in range(N):
        # 各ビットの係数: (2^r + 2^r-1 * r) * 2^l
        l, r = i, N - i - 1
        now = P[r]
        if r != 0:
            now += P[r - 1] * r
        now *= P[l]
        now *= C[i]
        ans += now
        ans %= MOD
    ans *= P[N]
    print(ans % MOD)


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Time Limit Exceeded):

from bisect import bisect_left
from itertools import accumulate

def resolve():
    def getCount(x):
        cnt = 0
        for a in A:
            # X未満の個数
            idx = bisect_left(A, x - a)
            # X以上の個数
            cnt += N - idx
        # X以上を使う！と決めた時、M個以上になるか
        return cnt >= M

    N, M = map(int, input().split())
    A = sorted(map(int, input().split()))

    ng = 10 ** 18
    ok = 0
    # X以上を使う！と決めるとX以上の個数が自明にわかる。
    while ng - ok > 1:
        mid = (ok + ng) // 2
        if getCount(mid):
            ok = mid
        else:
            ng = mid

    # M個以上あることがわかれば、Mよりも余分な数(同じ値が複数ある場合)
    # はすべてXである事がわかるため、差分だけ引けば良い。
    B = [0] + list(accumulate(A))
    ans = 0
    cnt = 0
    for i in range(N):
        idx = bisect_left(A, ok - A[i])
        ans += B[N] - B[idx] + A[i] * (N - idx)
        cnt += N - idx

    rest = (cnt - M) * ok
    print(ans - rest)


if __name__ == "__main__":
    resolve()

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
input = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines  

import numpy as np

def shake_cnt(x):
  # 両手x - 左手の価値 = 右手の価値
  # x - a[i]未満の個数(行わない握手)を数える
  # X以上の和がM個未満かどうか
  X = np.searchsorted(A, x-A)
  return (N - X).sum()
  

N, M = map(int, input().split())
A = np.array(input().split(),np.int64)
A.sort()

left = 0 # 握手の回数がM以上
right = 10 ** 6 # 握手の回数がM未満
while left + 1 < right:
    x = (left + right) // 2
    if shake_cnt(x) < M:
        right = x
    else:
        left = x

# 終了時にngにX-1,okにXが入っている。

X = np.searchsorted(A,right-A) # 行わない人数
Acum = np.zeros(N+1,np.int64) # 人数 -> 累積和
Acum[1:] = np.cumsum(A)

# right(最大値) 以上の握手を全て行ったとして、回数と総和を計算
# N**2通りの握手から価値の高い上位の握手  

shake = N * N - X.sum() # 総数-行わない人数
happy = (Acum[-1] - Acum[X]).sum() + (A * (N - X)).sum()
happy += (M - shake) *left
print(happy)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return input().rstrip().decode()
readlines = sys.stdin.buffer.readlines  

N = int(inp())
A = list(map(int, input().split()))
MOD = 10**9+7

ans = 0
for k in range(60):
  x0 = 0 # k桁目のbitが0になる総数
  x1 = 0 # k桁目のbitが1になる総数
  # k桁目が 0, 1 のどちらかを数えておく
  for a in A:
    if a>>k & 1:
      x1 += 1
    else:
      x0 += 1
  p = 1
  for i in range(k):
    p *= 2
    p %= MOD
  # x0 * x1 が 0, 1 の組み合わせ数
  cnt = (x0 * x1) % MOD
  ans += cnt * p
  ans %= MOD
print(ans)  
    
      

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return input().rstrip().decode()
readlines = sys.stdin.buffer.readlines  

import numpy as np

N = int(inp())
A = np.array(inp().split(), dtype=np.int64)
MOD = 10**9+7

ans = 0
for k in range(60):
  x0 = 0 # k桁目のbitが0になる総数
  x1 = 0 # k桁目のbitが1になる総数
  B = (A>>k) & 1 # k桁目が1になる要素
  
  # k桁目が 0, 1 のどちらかを数えておく
  x1 = np.count_nonzero(B)
  x0 = N - x1
  # x0 * x1 が 0, 1 の組み合わせ数
  cmb = x0 * x1
  
  for _ in range(k): # 10進数に変換
    cmb *= 2
    cmb %= MOD
  ans += cmb
print(ans%MOD)
Buggy python submission (verdict: Time Limit Exceeded):

def resolve():
    H, W = map(int, input().split())
    A = [tuple(map(int, input().split())) for _ in range(H)]
    B = [tuple(map(int, input().split())) for _ in range(H)]

    # 移動直前の偏りをkとし、移動先のマス(i, j)
    # についてd = abs(A[i][j] - B[i][j])
    # とした際に、移動後の偏りをabs(k - d)
    # とするかabs(k + d)とするか」を考えることと同値である。
    # dp[i][j][k] : マス (i, j) までの経路の偏りが k になることがあるか？
    max_k = 160 * 80
    dp = [[[False] * (max_k + 1) for _ in range(W + 1)] for _ in range(H + 1)]

    dp[0][1][0] = True
    dp[1][0][0] = True

    for i in range(H):
        for j in range(W):
            for k in range(max_k + 1):
                d = abs(A[i][j] - B[i][j])
                # i+1移動
                if k + d <= max_k:
                    dp[i + 1][j + 1][k + d] |= dp[i + 1][j][k]
                if abs(k - d) <= max_k:
                    dp[i + 1][j + 1][abs(k - d)] |= dp[i + 1][j][k]
                # j+1移動
                if k + d <= max_k:
                    dp[i + 1][j + 1][k + d] |= dp[i][j + 1][k]
                if abs(k - d) <= max_k:
                    dp[i + 1][j + 1][abs(k - d)] |= dp[i][j + 1][k]

    for k in range(max_k + 1):
        if dp[H][W][k]:
            return print(k)


if __name__ == "__main__":
    resolve()


Fixed submission:

def resolve():
    # (O):80（縦最大） × 80（横最大） × 80（各マスの最大の偏り）× 160（最大何回移動する必要があるか）
    H, W = map(int, input().split())
    A = [tuple(map(int, input().split())) for _ in range(H)]
    B = [tuple(map(int, input().split())) for _ in range(H)]
    Sub = [[0] * W for i in range(H)]
    for i in range(H):
        for j in range(W):
            Sub[i][j] = abs(A[i][j] - B[i][j])

    # 移動直前の偏りをkとし、移動先のマス(i, j)
    # についてd = abs(A[i][j] - B[i][j])とした際に、
    # 移動後の偏りを abs(k - d) とするか abs(k + d) とするか」を考えることと同値である。
    # dp[i][j][k] : マス (i, j) までの経路の偏りが k になることがあるか？

    # Bitset演算 (3次元TLE)
    max_k = 80 * (H + W)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (max_k + Sub[0][0]) | 1 << (max_k - Sub[0][0])

    for i in range(H):
        for j in range(W):
            d = Sub[i][j]
            if i:
                dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
            if j:
                dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d

    ans = 10 ** 18
    for i in range(max_k * 2 + 1):
        if dp[H - 1][W - 1] & (1 << i):
            ans = min(ans, abs(max_k - i))
    print(ans)


if __name__ == "__main__":
    resolve()
Buggy python submission (verdict: Runtime Error):

def resolve():
    MOD = 998244353
    N = int(input())
    D = list(map(int, input().split()))

    if D[0] != 0:
        print(0)
        return

    CNT = Counter(D)
    if CNT[0] > 1:  # 距離0は1個
        print(0)
        return

    ans = 1
    for i in range(1, max(D) + 1):
        ans *= pow(CNT[i - 1], CNT[i], MOD)
        ans %= MOD

    print(ans)


if __name__ == "__main__":
    resolve()


Fixed submission:
from collections import Counter

N = int(input())
D = list(map(int, input().split()))
MOD = 998244353

# 条件をみたす木が存在するには D1 = 0 が必要
if D[0] != 0:
  print(0)
  exit()
# 0 == 1
ctr = Counter(D)
if ctr[0] != 1:
  print(0)
  exit()
  
ans = 1
for i in range(len(ctr)-1):
  ans *= pow(ctr[i], ctr[i+1], MOD)
  ans %= MOD
print(ans)