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

Fixed submission:
import queue
n = int(input())

node = [[] for _ in range(n)]
color = [-1] * n
color[0] = 0

for _ in range(n-1):
    u, v, w = map(int, input().split())
    node[u-1].append((v-1, w))
    node[v-1].append((u-1, w))

q = queue.Queue()
q.put(0)

while not q.empty():
    i = q.get()
    c = color[i]
    for j, w in node[i]:
        if color[j] < 0:
            color[j] = c if w % 2 == 0 else (c + 1) % 2
            q.put(j)

for c in color:
    print(c)

Buggy submission (verdict: Runtime Error):
from collections import deque
n = int(input())
tree = [[] for _ in range(n)]
for _ in range(n-1):
    u, v, w = map(int, input().split())
    tree[u].append((v - 1, w))
    tree[v].append((u - 1, w))
q = deque()
q.append(0)
color = [-1] * n
color[0] = 0
while q:
    v = q.popleft()
    for child, w in tree[v]:
        if color[child] < 0:
            color[child] = (color[v] + w) % 2
            q.append(child)
for i in color:
    print(i)
Problem p03138 (python)

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

digit_n = 0
kk = k
while kk > 0:
    kk >>= 1
    digit_n += 1

count = [0] * digit_n
mask = ~((1 << digit_n) - 1)

ans = 0
for i in range(n):
    ans += a[i] & mask
    for j in range(digit_n):
        if (a[i] >> j) & 1:
            count[j] += 1 

dp = [[0] * (digit_n + 1) for _ in range(2)]

for i in range(1, digit_n + 1):
    j = digit_n - i
    bit = (k >> j) & 1
    dp[0][i] = (dp[0][i-1] << 1) + (n - count[j] if bit else count[j])
    if dp[1][i-1]:
        dp[1][i] = (dp[1][i-1] << 1) + max(n - count[j], count[j])
    if bit == 1:
        dp[1][i] = max(dp[1][i], (dp[0][i-1] << 1) + count[j])
ans += max(dp[0][-1], dp[1][-1])
print(ans)

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

digit_n = 0
kk = k
while kk > 0:
    kk >>= 1
    digit_n += 1

count = [0] * digit_n
mask = ~((1 << digit_n) - 1)

ans = 0
for i in range(n):
    ans += a[i] & mask
    for j in range(digit_n):
        if (a[i] >> j) & 1:
            count[j] += 1 

dp = [[0] * (digit_n + 1) for _ in range(2)]

for i in range(1, digit_n + 1):
    j = n - i
    bit = (k >> j) & 1
    dp[0][i] = (dp[0][i-1] << 1) + (n - count[j] if bit else count[j])
    dp[1][i] = (dp[1][i-1] << 1) + max(n - count[j], count[j])
    if bit == 1:
        dp[1][i] = max(dp[1][i], (dp[0][i-1] << 1) + count[j])
ans += max(dp[0][-1], dp[1][-1])
print(ans)
Problem p03409 (python)

Fixed submission:
n = int(input())
red  = [tuple(map(int, input().split())) for _ in range(n)]
blue = [tuple(map(int, input().split())) for _ in range(n)]

red.sort(key=lambda x:x[1])
blue.sort()

count = 0
for xb, yb in blue:
    max_y = -1
    max_i = -1
    for i in range(n):
        xr, yr = red[i]
        if xb > xr and yb > yr and max_y < yr:
            max_y, max_i = yr, i
    if max_y >= 0:
        red[max_i] = (201, 201)
        count += 1
print(count)

Buggy submission (verdict: Runtime Error):
n = int(input())
red  = [tuple(map(int, input().split())) for _ in range(n)]
blue = [tuple(map(int, input().split())) for _ in range(n)]
red.sort(key=lambda x:-x[1])
blue.sort()
count = 0
for xb, yb in blue:
    for xr, yr in red:
        if xb > xr and yb > yr:
            red[max_i] = (201, 201)
            count += 1
            break
print(count)
Problem p03565 (python)

Fixed submission:
s = input()
t = input()
sn = len(s)
tn = len(t)

if sn < tn:
    print("UNRESTORABLE")
    quit()
    
for i in range(sn-tn, -1, -1):
    matched = True
    for j in range(tn):
        if s[i+j] != t[j] and s[i+j] != '?':
            matched = False
            break
    if matched:
        break
        
if not matched:
    print("UNRESTORABLE")
    quit()

ans = ['a'] * sn
for j in range(0, i):
    if s[j] != '?':
        ans[j] = s[j]
for j in range(tn):
    ans[i + j] = t[j]
for j in range(i + tn, sn):
    if s[j] != '?':
        ans[j] = s[j]

print("".join(ans))

Buggy submission (verdict: Runtime Error):
s = input()
t = input()
sn = len(s)
tn = len(t)
for i in range(sn-tn, -1, -1):
    matched = True
    for j in range(tn):
        if s[i+j] != t[j] and s[i+j] != '?':
            matched = False
            break
    if matched:
        break
if not matched:
    print("UNRESTORABLE")
    quit()

ans = ['a'] * sn
for j in range(0, i):
    if s[j] != '?':
        ans[j] = s[j]
for j in range(tn):
    ans[i + j] = t[j]
for j in range(i + tn, sn):
    if s[j] != '?':
        ans[j] = s[j]

print("".join(ans))
Problem p02553 (python)

Fixed submission:
a, b, c, d= map(int, input().split())

nums = [a * c, a * d, b * c, b * d]

print(max(nums))

Buggy submission (verdict: Runtime Error):
    import numpy as np
  	l = map(int, input().split())
    [a, b, c, d] = l
    [A, B, C, D] = np.abs(l)
    [ab, cd, abcd, bc, Bb, ac] = [a*b, c*d, a*b*c*d, b*c, B*b, a*c]
    if abcd>=0:
        if bc<0:
            if Bb<0:
                print(b*c)
            elif a<0:
                print(b*d)
            else:
                print(a*d)
        elif a>=0:
            print(b*d)
        else:
            print(a*c)
    elif Bb<0:
        print(a*c)
    elif bc<0:
        if ac<0:
            print(b*d)
        else:
            print(a*c)
    
    else:
        print(b*d)
Problem p02687 (python)

Fixed submission:
S = input()
print('ABC' if S == 'ARC' else 'ARC')

Buggy submission (verdict: Runtime Error):
S = input()
print('ABC' if S = 'ARC' else 'ARC')
Problem p02727 (python)

Fixed submission:
X, Y, A, B, C = map(int, input().split())
p = sorted(list(map(int, input().split())), reverse = True)
q = sorted(list(map(int, input().split())), reverse = True)
r = sorted(list(map(int, input().split())), reverse = True)
S = sorted(p[:X] + q[:Y] + r[:X+Y], reverse = True)
print(sum(S[: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()))
s = p + q + r
S = sorted(s, reverse = True)
i = 0
j = 0
ans = 0
a = 0
b = 0
c = 0
while i < (X + Y):
  if S[j] in r:
    c += 1
    r.remove(s[j])
    ans += S[j]
    i += 1
  elif S[j] in p and a < X:
    a += 1
    p.remove(S[j])
    ans += S[j]
    i += 1
  elif S[j] in q and b < Y:
    b += 1
    q.remove(S[j])
    ans += S[j]
    i += 1
  j += 1
print(ans)

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()))
s = p + q + r
S = sorted(s, reverse = True)
print(S)
i = 0
j = 0
ans = 0
a = 0
b = 0
c = 0
while i < (X + Y):
  if S[j] in r:
    c += 1
    r.remove(s[j])
    ans += S[j]
    i += 1
  elif S[j] in p and a < X:
    a += 1
    p.remove(S[j])
    ans += S[j]
    i += 1
  elif S[j] in q and b < Y:
    b += 1
    q.remove(S[j])
    ans += S[j]
    i += 1
  j += 1
print(ans)
Problem p02761 (python)

Fixed submission:
N, M = map(int, input().split())
A  = [-1] * N
for i in range(M):
  t = list(map(int, input().split()))
  if A[t[0] - 1] == -1:
    A[t[0] - 1] = t[1]
  elif A[t[0] - 1] != t[1]:
    print(-1)
    exit()
if N != 1 and A[0] == 0:
  print(-1)
  exit()
elif A.count(-1) == N and N != 1:
  print(1 * 10 ** (N - 1))
  exit()
elif A[0] == -1:
  if N == 1:
    A[0] = 0
  else:
    A[0] = 1
B = [A[j] * 10 ** (N - 1 - j) for j in range(N) if A[j] > 0]
print(sum(B))

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = [-1] * N
for i in range(M):
  t = list(map(int, input().split()))
  if t == [1, 0] and N != 1:
    print(-1)
    exit()
  elif A[t[0] - 1] == -1:
    A[t[0] - 1] = t[1]
  elif A[t[0] - 1] != t[1]:
    print(-1)
    exit()
B = [A[k] * (10 ** (M - 1 - k)) for k in range(M) if A[k] > 0]
print(sum(B))
Problem p02811 (python)

Fixed submission:
A = input().split()
if 500 * int(A[0]) >= int(A[1]):
  print('Yes')
else:
  print('No')

Buggy submission (verdict: Runtime Error):
A=input().split()
if int(A[0])*500 => int(A[1]):
  print('Yes')
else:
  print('No')
Problem p02953 (python)

Fixed submission:
import sys
N = int(input())
H = input().split()
S = 0
for i in range(N):
  if int(H[i]) > S:
    S = int(H[i])
  elif S - int(H[i]) >= 2:
    print('No')
    exit()
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
H = input().split()
S = 0
M = H.index(max(H))
for i in range(M):
  if int(H[i]) - int(H[i + 1]) > 0:
    if int(H[i]) - int(H[i + 1]) > 1:
      print('No')
      exit()
    S.append(i)
for j in range(M, N - 1):
  if int(H[M]) - int(H[j]) > 1:
    print('No')
print('Yes')

Buggy submission (verdict: Runtime Error):
import sys

N = int(input())
H = input().split()
S = 0

for i in range(H.index(max(H))):
  if int(H[i]) - int(H[i + 1]) > 0:
    if int(H[i]) - int(H[i + 1]) > 1:
      print('No')
      exit()
    S.append(i)
for i in range(H.index(max(H)), N):
  if int(H[i]) - int(H[i + 1]) > 0:
    S += 1
    if int(H[i]) - int(H[i + 1]) > 1 or S == 2:
      print('No')
      exit()
Problem p02981 (python)

Fixed submission:
a = input().split()
 
N = int(a[0])
A = int(a[1])
B = int(a[2])
 
if A * N  >= B:
  print (B)
else:
  print (A * N)

Buggy submission (verdict: Runtime Error):
a = input().split()

N = int(a[1])
A = int(a[2])
B = int(a[3])

if A * N  >= B * N:
  print B * N
else:
  print A * N
Problem p03076 (python)

Fixed submission:
A = 0
B = []
B.append(0)
for i in range(5):
  I = int(input())
  A += I
  T = I % 10
  if T != 0:
    B.append(10 - T)
print(A + sum(B) - max(B))

Buggy submission (verdict: Runtime Error):
A = 0
B = []
for i in range(5):
  I = int(input())
  A += I
  T = I % 10
  if T != 0:
    B.append(10 - T)
print(A + sum(B) - max(B))
Problem p03251 (python)

Fixed submission:
N, M, X, Y = map(int, input().split())
x = max(list(map(int, input().split())))
y = min(list(map(int, input().split())))
print('No War' if x < y and X < y and x < Y else 'War')

Buggy submission (verdict: Runtime Error):
N, M, X, Y = map(int, input().split())
x = max(list(map(int, input().split())))
y = min(list(map(int, input().split())))
print('No War' if x < y and X < y else and x < Y 'War')
Problem p03284 (python)

Fixed submission:
I = input().split()

N = int(I[0])
K = int(I[1])

if N % K == 0:
  print(0)
else:
  print(1)

Buggy submission (verdict: Runtime Error):
I = input().split()

N = int(I[0])
K = int(I[1])

if K != N:
  print(1)
elif K = N:
  print(0)
Problem p03294 (python)

Fixed submission:
N=int(input())
print(sum(list(map(int, input().split())))-N)

Buggy submission (verdict: Runtime Error):
N=int(input)
print(sum(list(map(int, input().split())))-N)
Problem p02585 (python)

Fixed submission:
from itertools import accumulate

N, K, *PC = map(int, open(0).read().split())
P, C = [0] + PC[:N], [0] + PC[N:]

ans = float("-inf")
for start in range(1, N + 1):
    path = []

    cur = P[start]
    path = [C[start]]
    while cur != start:
        path.append(C[cur])
        cur = P[cur]

    A = list(accumulate(path))
    q, r = divmod(K, len(path))
    res = 0
    if A[-1] >= 0:
        if r == 0:
            q -= 1
            r += len(path)
        res += A[-1] * q
    elif q >= 1:
        r = len(path)

    ans = max(ans, res + max(A[:r]))

print(ans)

Buggy submission (verdict: Runtime Error):
N, K, *PC = map(int, open(0).read().split())
P, C = [0] + PC[:N], [0] + PC[N:]

ans = float("-inf")
for start in range(1, N + 1):
    acc = 0
    cur = start
    cycle = 0
    for l in range(K):
        cur = P[cur]
        acc += C[cur]
        ans = max(ans, acc)
        if cur == start:
            cycle = l + 1
            break

    q, r = divmod(K, cycle)
    if r == 0 and q > 1:
        r = cycle
        q -= 1

    res = acc * q
    ans = max(ans, res)
    for l in range(r):
        cur = P[cur]
        res += C[cur]
        ans = max(ans, res)

print(ans)
Problem p02588 (python)

Fixed submission:
N, *A = open(0).read().split()

S = [[0] * 21 for _ in range(21)]

ans = 0
for a in A:
    idx = a.find(".")
    if idx == -1:
        c2 = c5 = 0
    else:
        c2 = c5 = -(len(a) - idx - 1)

    a = int(a.replace(".", ""))
    while a % 2 == 0:
        c2 += 1
        a //= 2
    while a % 5 == 0:
        c5 += 1
        a //= 5
    c2 = min(c2, 10)
    c5 = min(c5, 10)
    ans += sum(S[i][j] for i in range(10 - c2, 21) for j in range(10 - c5, 21))

    S[c2 + 10][c5 + 10] += 1

print(ans)

Buggy submission (verdict: Runtime Error):
N, *A = open(0).read().split()

S = [[0] * 21 for _ in range(21)]

ans = 0
for a in A:
    if (idx := a.find(".")) == -1:
        c2 = c5 = 0
    else:
        c2 = c5 = -(len(a) - idx - 1)

    a = int(a.replace(".", ""))
    while a % 2 == 0:
        c2 += 1
        a //= 2
    while a % 5 == 0:
        c5 += 1
        a //= 5
    c2 = min(c2, 10)
    c5 = min(c5, 10)
    ans += sum(S[i][j] for i in range(10 - c2, 21) for j in range(10 - c5, 21))

    S[c2 + 10][c5 + 10] += 1

print(ans)
Problem p02608 (python)

Fixed submission:
N = int(input())

ans = [0] * (N + 1)
for x in range(1, 105):
    for y in range(1, 105):
        for z in range(1, 105):
            v = x * x + y * y + z * z + x * y + y * z + z * x
            if v <= N:
                ans[v] += 1

for i in range(1, N + 1):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())

def solve():
    memo = Counter()
    for x in range(1, N):
        for y in range(x, N):
            res = x * x + y * y + x * y
            if res >= N:
                return
            for z in range(y, N):
                if x == y == z:
                    cnt = 1
                elif x < y < z:
                    cnt = 6
                else:
                    cnt = 3
                memo[res + z * z + y * z + x * z] += cnt

C = solve()
for i in range(1, N + 1):
    print(C[i])
Problem p02626 (python)

Fixed submission:
from functools import reduce, lru_cache

INF = float("inf")

N, a, b, *A = map(int, open(0).read().split())
c = reduce(lambda a, b: a ^ b, A, 0)

@lru_cache(None)
def solve(a, b, c):
    if (a % 2) ^ (b % 2) != (c % 2):
        return INF
    if c == 0:
        return INF if a < b else (a - b) // 2

    return min(
        2 * solve(a // 2, b // 2, c // 2),
        1 + 2 * solve((a - 1) // 2, (b + 1) // 2, c // 2)
    )

print(-1 if (res := solve(a, b, c)) >= a else res)

Buggy submission (verdict: Runtime Error):
from functools import reduce

INF = float("inf")

N, a, b, *A = map(int, open(0).read().split())
c = reduce(lambda a, b: a ^ b, A, 0)

def solve(a, b, c):
    if (a % 2) ^ (b % 2) != (c % 2):
        return INF
    if c == 0:
        return INF if a < b else (a - b) // 2

    return min(
        2 * solve(a // 2, b // 2, c // 2),
        1 + 2 * solve((a - 1) // 2, (b + 1) // 2, c // 2)
    )

print(-1 if (res := solve(a, b, c)) >= a else res)
Problem p02662 (python)

Fixed submission:
mod = 998244353

N, S, *A = map(int, open(0).read().split())

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

for i, a in enumerate(A):
    for j in range(S + 1):
        dp[i + 1][j] += 2 * dp[i][j]
        dp[i + 1][j] %= mod

        if j + a <= S:
            dp[i + 1][j + a] += dp[i][j]
            dp[i + 1][j + a] %= mod

print(dp[N][S])

Buggy submission (verdict: Runtime Error):
mod = 998244353

N, S, *A = map(int, open(0).read().split())

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

for i, a in enumerate(A, 1):
    for j in range(S + 1):
        dp[i][j] = 2 * dp[i - 1][j] + dp[i - 1][j - a] * (j >= a)

print(dp[N][S])

Buggy submission (verdict: Runtime Error):
mod = 998244353

N, S, *A = map(int, open(0).read().split())

dp = [1] + [0] * S
for i, a in enumerate(A):
    for j in reversed(range(S + 1)):
        dp[j] = (2 * dp[j] + dp[j - a] * (j >= a)) % mod

print(dp[S])

Buggy submission (verdict: Runtime Error):
mod = 998244353

N, S, *A = map(int, open(0).read().split())

dp = [1] + [0] * S
for i, a in enumerate(A):
    for j in reversed(range(S + 1)):
        dp[j] = 2 * dp[j] + dp[j - a] * (j >= a)

print(dp[S])
Problem p02692 (python)

Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(10 ** 6)

stdin = open(0)

N, A, B, C = map(int, stdin.readline().split())
S = stdin.read().split()

ans = [""] * N
def dfs(i, a, b, c):
    if a < 0 or b < 0 or c < 0:
        return

    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    if S[i] == "AB":
        ans[i] = "A"
        dfs(i + 1, a + 1, b - 1, c)

        ans[i] = "B"
        dfs(i + 1, a - 1, b + 1, c)

    elif S[i] == "BC":
        ans[i] = "B"
        dfs(i + 1, a, b + 1, c - 1)

        ans[i] = "C"
        dfs(i + 1, a, b - 1, c + 1)
    else:
        ans[i] = "A"
        dfs(i + 1, a + 1, b, c - 1)

        ans[i] = "C"
        dfs(i + 1, a - 1, b, c + 1)

dfs(0, A, B, C)
print("No")

Buggy submission (verdict: Runtime Error):
stdin = open(0)

N, A, B, C = map(int, stdin.readline().split())
S = stdin.read().split()

ans = [""] * N
def dfs(i, a, b, c):
    if a < 0 or b < 0 or c < 0:
        return

    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    if S[i] == "AB":
        ans[i] = "A"
        dfs(i + 1, a + 1, b - 1, c)

        ans[i] = "B"
        dfs(i + 1, a - 1, b + 1, c)

    elif S[i] == "BC":
        ans[i] = "B"
        dfs(i + 1, a, b + 1, c - 1)

        ans[i] = "C"
        dfs(i + 1, a, b - 1, c + 1)
    else:
        ans[i] = "A"
        dfs(i + 1, a + 1, b, c - 1)

        ans[i] = "C"
        dfs(i + 1, a - 1, b, c + 1)

dfs(0, A, B, C)
print("No")
Problem p02821 (python)

Fixed submission:
from itertools import accumulate

MAX = 2 * 10 ** 5 + 5

N, M, *A = map(int, open(0).read().split())

B = [0] * MAX
for a in A:
    B[a] += 1

C = list(accumulate(reversed(B)))[::-1]

ng, ok = 1, MAX
while abs(ok - ng) > 1:
    m = (ok + ng) // 2
    if sum(C[max(0, m - a)] for a in A) >= M:
        ng = m
    else:
        ok = m

D = list(accumulate(reversed(list(i * b for i, b in enumerate(B)))))[::-1]
print(sum(D[max(0, ng - a)] - (ng - a) * C[max(0, ng - a)] for a in A) + ng * M)

Buggy submission (verdict: Runtime Error):

import sys
sys.setrecursionlimit(200200)

mod = 10 ** 9 + 7

N, *AB = map(int, open(0).read().split())

E = [[] for _ in range(N + 1)]
for i, (a, b) in enumerate(zip(*[iter(AB)] * 2)):
    E[a - 1].append((b - 1, i))
    E[b - 1].append((a - 1, i))

X = [0] * N

def dfs(u, p):
    X[p] = 1 + sum(dfs(v, c) for v, c in E[u] if p != c)
    return X[p]

dfs(0, -1)

I = [1] * (N + 1)
inv = pow(2, mod - 2, mod)
for i in range(N):
    I[i + 1] = I[i] * inv % mod

ans = (I[N] + 1) * (N - 1) - inv * N - I[N] + 1
for e in range(N - 1):
    ans -= I[X[e]] + I[N - X[e]]
    ans %= mod

print(ans)
Problem p02887 (python)

Fixed submission:
from itertools import groupby

N = int(input())
S = input()

G = [len(tuple(v)) for k, v in groupby(S)]
print(len(G))

Buggy submission (verdict: Runtime Error):
N, *D = map(int, open(0).read().split())

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        ans += D[i] * D[j]

print(ans)
Problem p02900 (python)

Fixed submission:
def divisors(N):
    U = int(N ** 0.5) + 1
    L = [i for i in range(1, U) if N % i == 0]
    return L + [N // i for i in reversed(L) if N != i * i]

def is_prime(N):
    if N < 2 or N % 2 == 0:
        return (N == 2)
    i = 3
    while i * i <= N:
        if N % i == 0:
            return False
        i += 2
    return True

A, B = map(int, input().split())

if A > B:
    A, B = B, A

D = divisors(A)

ans = 0
for d in D:
    if B % d == 0 and is_prime(d):
        ans += 1

print(ans + 1)

Buggy submission (verdict: Runtime Error):
def divisors(N):
    U = int(N ** 0.5) + 1
    L = [i for i in range(1, U) if N % i == 0]
    return L + [N // i for i in reversed(L) if N != i * i]

def primes(n):
    memo = [True] * (n + 1)
    i = 2
    while i * i <= n:
        if memo[i]:
            j = i << 1
            while j <= n:
                memo[j] = False
                j += i
        i += 1
    return {i for i in range(n + 1) if memo[i] and i >= 2}

A, B = map(int, input().split())

if A > B:
    A, B = B, A

D = set(primes(A))
E = {d for d in divisors(A) if B % d == 0}

print(len(D & E) + 1)
Problem p02956 (python)

Fixed submission:
mod = 998244353

class BIT:
    def __init__(self, N):
        self.size = N
        self.tree = [0] * (N + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N, *XY = map(int, open(0).read().split())
X, Y = XY[::2], XY[1::2]

memo = {y: i for i, y in enumerate(sorted(Y))}
Y = [y for x, y in sorted(zip(X, map(memo.get, Y)), key=lambda p: p[0])]

P = [1]
for i in range(N):
    P.append(P[-1] * 2 % mod)

ans = P[N - 1] * N % mod
bit = BIT(N)
for i, y in enumerate(Y):
    bit.add(y + 1, 1)

    ld = bit.sum(y)
    lu = i - ld

    rd = y - ld
    ru = N - y - 1 - lu

    A, B, C, D = P[ru], P[lu], P[ld], P[rd]

    ans += ((C - 1) * (A - 1) % mod) * (B * D % mod) % mod
    ans += ((B - 1) * (D - 1) % mod) * (C * A % mod) % mod
    ans -= ((C - 1) * (A - 1) % mod) * ((D - 1) * (B - 1) % mod) % mod
    ans %= mod

print(ans)

Buggy submission (verdict: Runtime Error):
class BIT:
    def __init__(self, N):
        self.size = N
        self.tree = [0] * (N + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

N, *XY = map(int, open(0).read().split())
X, Y = XY[::2], XY[1::2]

memo = {y: i for i, y in enumerate(sorted(Y))}
Y = [y for x, y in sorted(zip(X, map(memo.get, Y)), key=lambda p: p[0])]

P = [1]
for i in range(N):
    P.append(P[-1] * 2 % mod)

ans = P[N - 1] * N % mod
bit = BIT(N)
for i, y in enumerate(Y):
    bit.add(y + 1, 1)

    ld = bit.sum(y)
    lu = i - ld

    rd = y - ld
    ru = N - y - 1 - lu

    A, B, C, D = P[ru], P[lu], P[ld], P[rd]

    ans += ((C - 1) * (A - 1) % mod) * (B * D % mod) % mod
    ans += ((B - 1) * (D - 1) % mod) * (C * A % mod) % mod
    ans -= ((C - 1) * (A - 1) % mod) * ((D - 1) * (B - 1) % mod) % mod
    ans %= mod

print(ans)
Problem p02974 (python)

Fixed submission:
mod = 10 ** 9 + 7

N, K = map(int, input().split())

dp = [[[0 for _ in range(2610)] for _ in range(51)] for _ in range(51)]
dp[0][0][0] = 1

for i in range(N):
    for j in range(N):
        for k in range(K + 1):
            dp[i + 1][j + 1][k + 2 * j + 2] += dp[i][j][k]
            if j > 0:
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j

            dp[i + 1][j][k + 2 * j] += dp[i][j][k]

            if j > 0:
                dp[i + 1][j - 1][k + 2 * j - 2] += dp[i][j][k] * j * j


print(dp[N][0][K] % mod)

Buggy submission (verdict: Runtime Error):

mod = 10 ** 9 + 7

N, K = map(int, input().split())

dp = [[[0 for _ in range(2510)] for _ in range(51)] for _ in range(51)]
dp[0][0][0] = 1

for i in range(N):
    for j in range(N):
        for k in range(K + 1):
            dp[i + 1][j + 1][k + 2 * j + 2] += dp[i][j][k]
            if j > 0:
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j

            dp[i + 1][j][k + 2 * j] += dp[i][j][k]

            if j > 0:
                dp[i + 1][j - 1][k + 2 * j - 2] += dp[i][j][k] * j * j


print(dp[N][0][K] % mod)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7

N, K = map(int, input().split())

dp = [[[0 for _ in range(2510)] for _ in range(51)] for _ in range(51)]
dp[0][0][0] = 1

for i in range(N):
    for j in range(N):
        for k in range(K + 1):
            dp[i + 1][j + 1][k + 2 * j + 2] += dp[i][j][k]
            if j > 0:
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j
                dp[i + 1][j][k + 2 * j] += dp[i][j][k] * j

            dp[i + 1][j][k + 2 * j] += dp[i][j][k]

            if j > 0:
                dp[i + 1][j - 1][k + 2 * j - 2] += dp[i][j][k] * j * j


print(dp[N][0][K] % mod)