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


def comb(n,r):
    if n >= r:
        numer = math.factorial(n) % mod
        denom_inv = pow(((math.factorial(r) % mod) * (math.factorial(n-r) % mod)) % mod, mod-2, mod)
        return (numer * denom_inv) % mod
    else:
        return 0


N, A, B, C = map(int, input().split())
mod = 10 ** 9 + 7

G = fractions.gcd(A, B)
A = int(A / G)
B = int(B / G)

P = 0
A_N = pow(A, N, mod)
B_N = pow(B, N, mod)
for i in range(N):
    P = (P + ((N + i) * ((((A_N * pow(B, i, mod)) % mod) * ((pow(A+B, N-1-i, mod) * comb(N + i - 1, i)) % mod)) % mod
                        + (((B_N * pow(A, i, mod)) % mod) * ((pow(A+B, N-1-i, mod) * comb(N + i - 1, i)) % mod)) % mod)) % mod) % mod
P = (P * 100) % mod
Q = (pow(A+B, 2*N-1, mod) * (100 - C)) % mod
print((P * pow(Q, mod-2, mod)) % mod)


Fixed submission:
import math
import fractions
mod = 10 ** 9 + 7

nmax = 2 * 10 ** 5 + 1  # change here
fac = [0] * (nmax + 1)
finv = [0] * (nmax + 1)
inv = [0] * (nmax + 1)
fac[0] = 1
fac[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
for i in range(2, nmax):
    fac[i] = fac[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod


def comb(n, r):
    if n < r:
        return 0
    else:
        return (fac[n] * ((finv[r] * finv[n - r]) % mod)) % mod


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


G = fractions.gcd(A, B)
A = int(A / G)
B = int(B / G)

P = 0
A_N = pow(A, N, mod)
B_N = pow(B, N, mod)
for i in range(N):
    P = (P + ((N + i) * ((((A_N * pow(B, i, mod)) % mod) * ((pow(A+B, N-1-i, mod) * comb(N + i - 1, i)) % mod)) % mod
                        + (((B_N * pow(A, i, mod)) % mod) * ((pow(A+B, N-1-i, mod) * comb(N + i - 1, i)) % mod)) % mod)) % mod) % mod
P = (P * 100) % mod
Q = (pow(A+B, 2*N-1, mod) * (100 - C)) % mod
print((P * pow(Q, mod-2, mod)) % mod)
Buggy python submission (verdict: Time Limit Exceeded):
R,G,B,N = map(int, input().split())

ans = 0
for r in range((N//R)+1):
  for g in range((N//G)+1):
    if (N-R*r-G*g) >= 0 and (N-R*r-G*g) % B == 0:
      ans += 1
print(ans)

Fixed submission:
R,G,B,N = map(int, input().split())

ans = 0
for r in range((N//R)+1):
  for g in range((N//G)+1):
    if (N-R*r-G*g) >= 0 and (N-R*r-G*g) % B == 0:
      ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

ans = 0
k = 1
while True:
  if N % k == 0:
    m = (N//k) - 1
    if m > k:
      ans += (N//k) - 1
    else:
      break
  k += 1
print(ans)

Fixed submission:
N = int(input())

ans = 0
k = 1
while True:
  m = (N//k) - 1
  if m <= k:
    break
  if N % k == 0:
    ans += m
  k += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
a=input()
b=input()
c=input()
d=input()
e=input()
f=input()

if e-a > k:
  print(':(')
else:
  print('Yay!')

Fixed submission:
a=int(input())
b=int(input())
c=int(input())
d=int(input())
e=int(input())
k=int(input())

if e-a>k:
  print(':(')
else:
  print('Yay!')
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())

num = [N, 0, 0, 0, 0, 0]
time = 0
while num[5] < N:
  if num[4] < E:
    num[5] += num[4]
    num[4] = 0
  else:
    num[5] += E
    num[4] -= E
  if num[3] < D:
    num[4] += num[3]
    num[3] = 0
  else:
    num[4] += D
    num[3] -= D
  if num[2] < C:
    num[3] += num[2]
    num[2] = 0
  else:
    num[3] += C
    num[2] -= C
  if num[1] < B:
    num[2] += num[1]
    num[1] = 0
  else:
    num[2] += B
    num[1] -= B
  if num[0] < A:
    num[1] += num[0]
    num[0] = 0
  else:
    num[1] += A
    num[0] -= A
  time += 1
  
print(time)

Fixed submission:
import math

N=int(input())
A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())

print(math.ceil(N/min([A,B,C,D,E]) + 4))
Buggy python submission (verdict: Time Limit Exceeded):
X,Y,Z,K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))
taste_list = []

for i in range(X):
  for j in range(Y):
    for k in range(Z):
      taste_list.append(A_list[i]+B_list[j]+C_list[k])
taste_list.sort(reverse=True)
for i in range(K):
  print(taste_list[i])

Fixed submission:
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

AB = []
for a in A:
    for b in B:
        AB.append(a+b)
AB.sort(reverse=True)
AB = AB[:K]

ABC = []
for ab in AB:
    for c in C:
        ABC.append(ab+c)
ABC.sort(reverse=True)

for i in range(K):
    print(ABC[i])
Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()
AC_list = [0]*(N-1)
for i in range(N-1):
  if S[i:i+2] == 'AC':
    AC_list[i] = 1
for q in range(Q):
  l,r = map(int, input().split())
  print(sum(AC_list[l-1:r-1]))

Fixed submission:
N,Q = map(int, input().split())
S = input()
AC_cum = [0]
for i in range(N-1):
  if S[i:i+2] == 'AC':
    AC_cum.append(AC_cum[-1]+1)
  else:
    AC_cum.append(AC_cum[-1])
for q in range(Q):
  l,r = map(int, input().split())
  print(AC_cum[r-1]-AC_cum[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()
for q in range(Q):
  l,r = map(int, input().split())
  Sq = S[l-1:r]
  print(Sq.count('AC'))

Fixed submission:
N,Q = map(int, input().split())
S = input()
AC_cum = [0]
for i in range(N-1):
  if S[i:i+2] == 'AC':
    AC_cum.append(AC_cum[-1]+1)
  else:
    AC_cum.append(AC_cum[-1])
for q in range(Q):
  l,r = map(int, input().split())
  print(AC_cum[r-1]-AC_cum[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int, input().split())
S = input()
for q in range(Q):
  cnt = 0
  l,r = map(int, input().split())
  Sq = S[l-1:r]
  for i in range(r-l):
    if Sq[i:i+2] == 'AC':
      cnt += 1
  print(cnt)

Fixed submission:
N,Q = map(int, input().split())
S = input()
AC_cum = [0]
for i in range(N-1):
  if S[i:i+2] == 'AC':
    AC_cum.append(AC_cum[-1]+1)
  else:
    AC_cum.append(AC_cum[-1])
for q in range(Q):
  l,r = map(int, input().split())
  print(AC_cum[r-1]-AC_cum[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline

    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, R, L = map(int, input().split())
    P = list(map(int, input().split()))

    val2idx = [0] * (N+1)
    for i, p in enumerate(P):
        val2idx[p] = i + 1
    inf = 10**14
    dp = [[inf] * (N+1) for _ in range(N+1)]
    dp[0][0] = 0
    bit = Bit(N+1)
    for p in range(1, N+1):
        i = val2idx[p]
        for j in range(N+1):
            if i > j:
                dp[p][j] = min(dp[p][j], dp[p-1][j] + L)
                dp[p][i] = min(dp[p][i], dp[p-1][j] + R * (i - j - 1 - (bit.sum(i) - bit.sum(j))))
            else:
                dp[p][j] = min(dp[p][j], dp[p - 1][j])
        bit.add(i, 1)
    print(min(dp[-1]))
    #[print(i, dp[i]) for i in range(N+1)]


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys
    input = sys.stdin.readline

    N, R, L = map(int, input().split())
    P = list(map(int, input().split()))

    val2idx = [0] * (N+1)
    for i, p in enumerate(P):
        val2idx[p] = i + 1
    bigger = [[0] * (N+1) for _ in range(N+1)]
    for p in range(1, N+1):
        for j in range(N):
            if P[j] > p:
                bigger[p][j+1] = bigger[p][j] + 1
            else:
                bigger[p][j + 1] = bigger[p][j]
    #print(bigger)
    inf = 10**14
    dp = [[inf] * (N+1) for _ in range(N+1)]
    dp[0][0] = 0

    for p in range(1, N+1):
        i = val2idx[p]
        for j in range(N+1):
            if i > j:
                dp[p][j] = min(dp[p][j], dp[p-1][j] + L)
                dp[p][i] = min(dp[p][i], dp[p-1][j] + R * (bigger[p][i] - bigger[p][j]))
            else:
                dp[p][j] = min(dp[p][j], dp[p - 1][j])
    print(min(dp[-1]))
    #[print(i, dp[i]) for i in range(N+1)]


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math

A,B = map(int, input().split())
ans = int(0)
N = math.floor(math.log2(B)+1)

A_mod = A % 4
B_mod = B % 4
if A_mod < 2:
  if B_mod ==1 or B_mod == 2:
    ans += 1
else:
  if B_mod ==3 or B_mod == 0:
    ans += 1

for i in range(2,N+1):
  A_mod = A % (2**i)
  B_mod = B % (2**i)
  if A_mod < 2**(i-1):
    if B_mod >= 2**(i-1):
      ans += ((B_mod+1)%2)*(2**(i-1))
  else:
    if B_mod < 2**(i-1):
      ans += (A_mod%2)*(2**(i-1))
    else:
      ans += ((A_mod+B_mod+1)%2)*(2**(i-1))

print(ans)

Fixed submission:
import math

A,B = map(int, input().split())
ans = int(0)
if B==0:
  ans = 0
else:
  N = math.floor(math.log2(B)+1)
  
  A_mod = A % 4
  B_mod = B % 4
  if A_mod < 2:
    if B_mod ==1 or B_mod == 2:
      ans += 1
  else:
    if B_mod ==3 or B_mod == 0:
      ans += 1

  for i in range(2,N+1):
    A_mod = A % (2**i)
    B_mod = B % (2**i)
    if A_mod < 2**(i-1):
      if B_mod >= 2**(i-1):
        ans += ((B_mod+1)%2)*(2**(i-1))
    else:
      if B_mod < 2**(i-1):
        ans += (A_mod%2)*(2**(i-1))
      else:
        ans += ((A_mod+B_mod+1)%2)*(2**(i-1))

print(ans)

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

A,B = map(int, input().split())
ans = int(0)
N = math.floor(math.log2(B)+1)

A_mod = A % 4
B_mod = B % 4
if A_mod < 2:
  if B_mod ==1 or B_mod == 2:
    ans += 1
else:
  if B_mod ==3 or B_mod == 0:
    ans += 1

for i in range(2,N+1):
  A_mod = A % (2**i)
  B_mod = B % (2**i)
  if A_mod < 2**(i-1):
    if B_mod >= 2**(i-1):
      ans += ((B_mod+1)%2)*(2**(i-1))
  else:
    if B_mod < 2**(i-1):
      ans += (A_mod%2)*(2**(i-1))
    else:
      ans += ((A_mod+B_mod+1)%2)*(2**(i-1))

print(ans)

Fixed submission:
import math

A,B = map(int, input().split())
ans = int(0)
if B==0:
  ans = 0
else:
  N = math.floor(math.log2(B)+1)
  
  A_mod = A % 4
  B_mod = B % 4
  if A_mod < 2:
    if B_mod ==1 or B_mod == 2:
      ans += 1
  else:
    if B_mod ==3 or B_mod == 0:
      ans += 1

  for i in range(2,N+1):
    A_mod = A % (2**i)
    B_mod = B % (2**i)
    if A_mod < 2**(i-1):
      if B_mod >= 2**(i-1):
        ans += ((B_mod+1)%2)*(2**(i-1))
    else:
      if B_mod < 2**(i-1):
        ans += (A_mod%2)*(2**(i-1))
      else:
        ans += ((A_mod+B_mod+1)%2)*(2**(i-1))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
A,B = map(int, input().split())
ans = int(0)
for i in range(A,B+1):
  ans = ans ^ i
print(ans)

Fixed submission:
import math

A,B = map(int, input().split())
ans = int(0)
if B==0:
  ans = 0
else:
  N = math.floor(math.log2(B)+1)
  
  A_mod = A % 4
  B_mod = B % 4
  if A_mod < 2:
    if B_mod ==1 or B_mod == 2:
      ans += 1
  else:
    if B_mod ==3 or B_mod == 0:
      ans += 1

  for i in range(2,N+1):
    A_mod = A % (2**i)
    B_mod = B % (2**i)
    if A_mod < 2**(i-1):
      if B_mod >= 2**(i-1):
        ans += ((B_mod+1)%2)*(2**(i-1))
    else:
      if B_mod < 2**(i-1):
        ans += (A_mod%2)*(2**(i-1))
      else:
        ans += ((A_mod+B_mod+1)%2)*(2**(i-1))

print(ans)
Buggy python submission (verdict: Runtime Error):
(N,A,B,C)=map(int,input().split())
2
L=[]
3
for i in range(N):
4
  L.append(int(input()))
5
  
6
L_sums=[]
7
costs=[]
8
#bin_is=[]
9
for i in range(2**N+1,2**N+2**N):
10
  bin_i=str(bin(i))[2:]
11
  L_sum=0
12
  cost=-1
13
  for j in range(N):
14
    L_sum+=L[j]*int(bin_i[j+1])
15
    cost+=int(bin_i[j+1])
16
  L_sums.append(L_sum)
17
  costs.append(10*cost)
18
  #bin_is.append(bin_i)
19
​
20
cost_A=[]
21
for i in range(2**N-1):
22
  cost_A.append(costs[i]+abs(L_sums[i]-A))
23
min_cost_A=min(cost_A)
24
min_cost_A_index=cost_A.index(min(cost_A))+1
25
min_cost_A_index_bin=str(bin(min_cost_A_index+2**N))[2:]
26
for i in range(N):
27
  if min_cost_A_index_bin[i+1]=='1':
28
    del(L[i])
29
N=len(L)
30
#-------------------
31
L_sums=[]
32
costs=[]
33
#bin_is=[]
34
for i in range(2**N+1,2**N+2**N):
35
  bin_i=str(bin(i))[2:]
36
  L_sum=0
37
  cost=-1
38
  for j in range(N):
39
    L_sum+=L[j]*int(bin_i[j+1])
40
    cost+=int(bin_i[j+1])
41
  L_sums.append(L_sum)
42
  costs.append(10*cost)
43
  #bin_is.append(bin_i)
44
​
45
cost_B=[]
46
for i in range(2**N-1):
47
  cost_B.append(costs[i]+abs(L_sums[i]-B))
48
min_cost_B=min(cost_B)
49
min_cost_B_index=cost_B.index(min(cost_B))+1
50
min_cost_B_index_bin=str(bin(min_cost_B_index+2**N))[2:]
51
for i in range(N):
52
  if min_cost_B_index_bin[i]=='1':
53
    del(L[i])
54
N=len(L)
55
#-----------------
56
L_sums=[]
57
costs=[]
58
#bin_is=[]
59
for i in range(2**N+1,2**N+2**N):
60
  bin_i=str(bin(i))[2:]
61
  L_sum=0
62
  cost=-1
63
  for j in range(N):
64
    L_sum+=L[j]*int(bin_i[j+1])
65
    cost+=int(bin_i[j+1])
66
  L_sums.append(L_sum)
67
  costs.append(10*cost)
68
  #bin_is.append(bin_i)
69
​
70
cost_C=[]
71
for i in range(2**N-1):
72
  cost_C.append(costs[i]+abs(L_sums[i]-C))
73
min_cost_C=min(cost_C)
74
#min_cost_C_index=cost_C.index(min(cost_C))+1
75
#min_cost_C_index_bin=str(bin(min_cost_C_index))[2:]
76
#for i in range(N):
77
#  if min_cost_C_index_bin[i]=='1':
78
#    del(L[i])
79
#N=len(L)
80
#----------------
81
print(min_cost_A+min_cost_B+min_cost_C)

Fixed submission:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N, A, B, C = map(int, input().split())
    L = []
    for _ in range(N):
        L.append(int(input()))

    ans = 10**9
    for i in range(4 ** N):
        AA = []
        BB = []
        CC = []
        ii = i
        for j in range(N):
            k = ii % 4
            if k == 0:
                AA.append(L[j])
            elif k == 1:
                BB.append(L[j])
            elif k == 2:
                CC.append(L[j])
            ii >>= 2
        if len(AA) * len(BB) * len(CC) == 0:
            continue
        ans_new = abs(sum(AA) - A) + 10*(len(AA)-1) + abs(sum(BB) - B) + 10*(len(BB)-1) + abs(sum(CC) - C) + 10*(len(CC)-1)
        ans = min(ans, ans_new)
    print(ans)


if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

N=input()
A=list(map(int, input().split()))       

def gcd(*numbers):
  return reduce(math.gcd, numbers)

print(gcd(A))

Fixed submission:
import fractions
from functools import reduce

N=input()
A=list(map(int, input().split()))       

def gcd(numbers):
  return reduce(fractions.gcd, numbers)

print(gcd(A))
Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())
A=list(map(int, input().split()))
inf = float('inf')

number = [6,2,5,5,4,5,6,3,7,6]


dp = [-1*inf]*(N+1)
dp[0] = 0
for i in range(1,N+1):
  dp_new = 0
  for j in A:
    if i - number[j] >= 0:
      if dp[i-number[j]] +1 > dp_new:
        dp[i] = dp[i-number[j]] +1
        dp_new = dp[i]

A.sort(reverse=True)
ans = ""
while N > 0:
  for j in A:
    if dp[N] == dp[N-number[j]] + 1:
      ans += str(j)
      N -= number[j]
      break

print(int(ans))

Fixed submission:
N,M=map(int, input().split())
A=list(map(int, input().split()))
inf = float('inf')

number = [6,2,5,5,4,5,6,3,7,6]


dp = [-1*inf]*(N+1)
dp[0] = 0
for i in range(1,N+1):
  dp_new = 0
  for j in A:
    if i - number[j] >= 0:
      if dp[i-number[j]] +1 > dp_new:
        dp[i] = dp[i-number[j]] +1
        dp_new = dp[i]

A.sort(reverse=True)
ans = ""
while N > 0:
  for j in A:
    if N - number[j] >= 0:
      if dp[N] == dp[N-number[j]] + 1:
        ans += str(j)
        N -= number[j]
        break

print(int(ans))

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())
A=list(map(int, input().split()))
inf = float('inf')

number = [6,2,5,5,4,5,6,3,7,6]


dp = [-1*inf]*(N+1)
dp[0] = 0
for i in range(1,N+1):
  dp_new = 0
  for j in A:
    if i - number[j] >= 0:
      if dp[i-number[j]] +1 > dp_new:
        dp[i] = dp[i-number[j]] +1

A.sort(reverse=True)
ans = ""
while N > 0:
  for j in A:
    if dp[N] == dp[N-number[j]] + 1:
      ans += str(j)
      N -= number[j]
      break

print(int(ans))

Fixed submission:
N,M=map(int, input().split())
A=list(map(int, input().split()))
inf = float('inf')

number = [6,2,5,5,4,5,6,3,7,6]


dp = [-1*inf]*(N+1)
dp[0] = 0
for i in range(1,N+1):
  dp_new = 0
  for j in A:
    if i - number[j] >= 0:
      if dp[i-number[j]] +1 > dp_new:
        dp[i] = dp[i-number[j]] +1
        dp_new = dp[i]

A.sort(reverse=True)
ans = ""
while N > 0:
  for j in A:
    if N - number[j] >= 0:
      if dp[N] == dp[N-number[j]] + 1:
        ans += str(j)
        N -= number[j]
        break

print(int(ans))
Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())
A=list(map(int, input().split()))
inf = float('inf')

number = [6,2,5,5,4,5,6,3,7,6]


dp = [-1*inf]*(N+1)
dp[0] = 0
for i in range(1,N+1):
  dp_new = 0
  for j in A:
    if dp[i-number[j]] +1 > dp_new:
      dp[i] = dp[i-number[j]] +1

A.sort(reverse=True)
ans = ""
while N > 0:
  for j in A:
    if dp[N] == dp[N-number[j]] + 1:
      ans += str(j)
      N -= number[j]
      break

print(int(ans))

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

max_A = max(A)
if max_A == 0:
  max_A_bin_len = 1
else:
  max_A_bin_len = len(bin(max_A)) - 2
  if K == 0:
    K_bin_len = 1
  else:
    K_bin_len = len(bin(K)) - 2
max_bin_len = max(max_A_bin_len, K_bin_len)
A_bin = [bin(a)[2:].zfill(max_bin_len) for a in A]
K_bin = bin(K)[2:].zfill(max_bin_len)

ans_cand1 = 0
ans_cand2 = 0
can_choose = 0
can_choose_forever = 0
calc_ans_cand2 = 0
start_calc_ans_cand2 = 0
for bit in range(max_bin_len):
  if not can_choose:
    if K_bin[bit] == '1':
      can_choose = 1
  A_one_num = 0
  for a in A_bin:
    if a[bit] == '1':
      A_one_num += 1
  if can_choose:
    if A_one_num < N-A_one_num:
      if not can_choose_forever:
        can_choose = 0
        if not calc_ans_cand2:
          ans_cand2 = ans_cand1 + (2**(max_bin_len-bit-1)) * A_one_num
          start_calc_ans_cand2 = 1
    else:
      can_choose_forever = 1
    ans_cand1 += (2**(max_bin_len-bit-1)) * (max(A_one_num, N-A_one_num))
  else:
    ans_cand1 += (2**(max_bin_len-bit-1)) * A_one_num
  if calc_ans_cand2:
    ans_cand2 += (2**(max_bin_len-bit-1)) * (max(A_one_num, N-A_one_num))
  if start_calc_ans_cand2:
    calc_ans_cand2 = 1
print(max(ans_cand1, ans_cand2))
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

mod = 10**9 + 7

A.sort(reverse=True)
B.sort(reverse=True)
A.append(N*M + 1)
B.append(N*M + 1)

space = 0
A_used = 0
B_used = 0
ans = 1
for i in range(M * N, 0, -1):
    if i == A[0]:
        if i == B[0]:
            space += (A_used + B_used)
            A_used += 1
            B_used += 1
            del A[0]
            del B[0]
        else:
            space += (B_used - 1)
            A_used += 1
            del A[0]
            ans = (ans * B_used) % mod
    elif i == B[0]:
        space += (A_used - 1)
        B_used += 1
        del B[0]
        ans = (ans * A_used) % mod
    else:
        ans *= space
        space -= 1
        if space < 0:
            break

print(ans % mod)

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

mod = 10**9 + 7

A.sort(reverse=True)
B.sort(reverse=True)
A.append(N*M + 1)
B.append(N*M + 1)

space = 0
A_used = 0
B_used = 0
ans = 1
for i in range(M * N, 0, -1):
    if i == A[0]:
        if i == B[0]:
            space += (A_used + B_used)
            A_used += 1
            B_used += 1
            del A[0]
            del B[0]
        else:
            space += (B_used - 1)
            A_used += 1
            del A[0]
            ans = (ans * B_used) % mod
    elif i == B[0]:
        space += (A_used - 1)
        B_used += 1
        del B[0]
        ans = (ans * A_used) % mod
    else:
        ans = (ans * space) % mod
        space -= 1
        if space < 0:
            break

print(ans % mod)
Buggy python submission (verdict: Runtime Error):
import sys
import bisect
from collections import defaultdict
input = sys.stdin.buffer.readline

H, W, N = map(int, input().split())
block = [defaultdict(int) for _ in range(W+1)]
for _ in range(N):
    x, y = map(int, input().split())
    block[y][x] = 1
for w in range(1, W+1):
    block[w][H+1] = 1

h, w = 1, 1
ans = min(list(block[1].keys())) - 1
if ans <= 1:
    print(1)
    exit()
while True:
    if block[w][h+1] == 1:
        ans = min(ans, h)
        break
    h += 1
    if w == W:
        break
    if block[w+1][h] == 0:
        w += 1
        block_w = []
        for k in block[w].keys():
            if block[w][k] == 1:
                block_w.append(k)
        hmin = block_w[bisect.bisect_left(block_w, h)]
        ans = min(ans, hmin-1)

print(ans)


Fixed submission:
import sys
import bisect
from collections import defaultdict
input = sys.stdin.buffer.readline

H, W, N = map(int, input().split())
block = [defaultdict(int) for _ in range(W+1)]
for _ in range(N):
    x, y = map(int, input().split())
    block[y][x] = 1
for w in range(1, W+1):
    block[w][H+1] = 1

h, w = 1, 1
ans = min(list(block[1].keys())) - 1
if ans <= 1:
    print(1)
    exit()
while True:
    if block[w][h+1] == 1:
        ans = min(ans, h)
        break
    h += 1
    if w == W:
        break
    if block[w+1][h] == 0:
        w += 1
        block_w = []
        for k in block[w].keys():
            if block[w][k] == 1:
                block_w.append(k)
        block_w.sort()
        hmin = block_w[bisect.bisect_left(block_w, h)]
        ans = min(ans, hmin-1)

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000)

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

def burger(N):
  if N == 0:
    return 'P'
  else:
    subburger = burger(N-1)
    return 'B' + subburger + 'P' + subburger + 'B'
print(burger(N)[:X].count('P'))

Fixed submission:
import sys
sys.setrecursionlimit(10000)

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

ans = 0
for n in range(N,-1,-1):
  if X > (2**(n+2) - 3) / 2 :
    ans += 2**n #- 1 + 1
    X = X - (2**(n+1) - 3) - 2
  else:
    X -= 1
print(ans)

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

def burger(N):
  if N == 0:
    return 'P'
  else:
    subburger = burger(N-1)
    return 'B' + subburger + 'P' + subburger + 'B'
print(burger(N)[:X+1].count('P'))

Fixed submission:
import sys
sys.setrecursionlimit(10000)

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

ans = 0
for n in range(N,-1,-1):
  if X > (2**(n+2) - 3) / 2 :
    ans += 2**n #- 1 + 1
    X = X - (2**(n+1) - 3) - 2
  else:
    X -= 1
print(ans)