qyliang/CodeNanoFix huggingface dataset:
Buggy code:
rom itertools import accumulate
def myAnswer(N:int,A:list) -> int:
   accum = list(accumulate(A))
   ans = 10**10
   now = accum.pop()
   for a in accum:
      if(abs(now - a - a) < ans):
         ans = abs(now - 2*a)
   return ans

   

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Fixed code:
from itertools import accumulate
def myAnswer(N:int,A:list) -> int:
   accum = list(accumulate(A))
   ans = 10**10
   now = accum.pop()
   for a in accum:
      if(abs(now - a - a) < ans):
         ans = abs(now - 2*a)
   return ans

   

def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()
Buggy code:
n=int(input())
l=list(map(int,input().split()))
tot=sum(l)
f=0
ans=0
for i in range(n):
    ans+=l[i]
    f=0
    if ans*2==tot:
        f=1
        break
if f:
    print(0)
else:
    ans=0
    tr=10**9+7
    for i in range(n):
        ans+=l[i]
        print(ans,tr)
        tr=min(tr,abs(ans-(tot-ans)))
    print(tr)

Fixed code:
for tc in [0]*1:

    n=int(input())
    l=list(map(int,input().split()))
    tot=sum(l)
    f=0
    ans=0
    for i in range(n):
        ans+=l[i]
        f=0
        if ans*2==tot:
            f=1
            break
    if f:
        print(0)
    else:
        ans=0
        tr=10**9+7+2020202020
        for i in range(n):
            ans+=l[i]
            #print(ans,tr)
            tr=min(tr,abs(ans-abs(tot-ans)))
        print(tr)
Buggy code:
N=int(input())
A=list(map(int,input().split()))
s=ans=sum(A)
k=0
for i in range(N-1):
    k+=A[i]
    ans=min(ans,abs(s-2*k))
    print(k,ans)
print(ans)

Fixed code:
N=int(input())
A=list(map(int,input().split()))
ans=float("inf")
s=sum(A)
k=0
for i in range(N-1):
    k+=A[i]
    ans=min(ans,abs(s-2*k))
print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n-1):
  a[i+1] += a[i]
ans = 100000000000000
for i in range(n-1):
  ans = min(ans, a[-1] - 2*a[i])
  


Fixed code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n-1):
  a[i+1] += a[i]
ans = 100000000000000
for i in range(n-1):
  ans = min(ans, abs(a[-1] - 2*a[i]))

print(ans)
Buggy code:
n=int(input())
arr=list(map(int,input().split()))
total=sum(arr)
acum=[arr[0]]
for i in range(1,n):
  acum.append(arr[i]+acum[-1])
ans=10**18
for i in range(n):
  tmp=abs(acum[i]-(total-acu[i]))
  ans=min(ans,tmp)
print(ans)

Fixed code:
n=int(input())
arr=list(map(int,input().split()))
total=sum(arr)
acum=[arr[0]]
for i in range(1,n):
  acum.append(arr[i]+acum[-1])
ans=10**18
for i in range(n):
  tmp=abs(acum[i]-(total-acum[i]))
  ans=min(ans,tmp)
print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
l = sum(a)
b = list()
t = 0
ans = 10**10
for i in a:
    t += i
    n.append(t)
for j in b:
    x = abs(l-(2*j))
    if x < ans:
        ans = x

print(ans)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
l = sum(a)
b = list()
t = 0
ans = 10**10
for i in a:
    t += i
    b.append(t)
for j in b:
    x = abs(l-(2*j))
    if x < ans:
        ans = x

print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))

print(int(N/2))

left_length = sum(A[:int(N/2)])
right_length = sum(A[int(N/2):])
if left_length > right_length :
  price = left_length - right_length
  for i in range(int(N/2)-1,0,-1) :
    var = sum(A[i:]) - sum(A[:i])
    if price > var :
      price = var
    else :
      break
else :
  price = right_length - left_length
  for i in range(int(N/2)+1,N) :
    var = sum(A[:i]) - sum(A[i:])
    if price > var :
      price = var
    else :
      break

print(price)

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

left_length = sum(A[:int(N/2)])
right_length = sum(A[int(N/2):])
if left_length > right_length :
  price = left_length - right_length
  for i in range(int(N/2)-1,0,-1) :
    var = abs(sum(A[:i]) - sum(A[i:]))
    if price > var :
      price = var
    else :
      break
else :
  price = right_length - left_length
  for i in range(int(N/2)+1,N) :
    var = abs(sum(A[i:]) - sum(A[:i]))
    if price > var :
      price = var
    else :
      break

print(price)
Buggy code:
N = int(input())
A = list(map(int, input().split()))

Asum = sum(A)

s=0
t=0
for i,Ai in enumerate(A):
    s+=Ai
    if s == Asum/2:
        print(0)
        exit()
    if s >= Asum/2:
        s = i
        break
for i,Ai in enumerate(A[::-1]):
    t+=Ai
    if t >= Asum/2:
        t = i
        break

ans1 = sum(A[:s])-(Asum-sum(A[:s]))
ans2 = sum(A[:t])-(Asum-sum(A[:t]))

print(min(ans1,ans2))


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

Asum = sum(A)

s=0
t=0
for i,Ai in enumerate(A):
    s+=Ai
    if s == Asum/2:
        print(0)
        exit()
    if s >= Asum/2:
        s = i
        break
for i,Ai in enumerate(A[::-1]):
    t+=Ai
    if t >= Asum/2:
        t = i
        break

ans1 = abs(sum(A[:s])-(Asum-sum(A[:s])))
ans2 = abs(sum(A[::-1][:t])-(Asum-sum(A[::-1][:t])))

print(min(ans1,ans2))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
i=ans1=0
while True:
    if ans1<x/2 and i<n:
        ans1+=a[i]
        i+=1
    else:
        break
if i==n-1:
    print(2*a[i-1]-x)
else:
    i-=1
    ans2=x-ans1
    ans1-=a[i]
    print(a[i]-abs(ans1-ans2))

Fixed code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
i=ans1=0
while True:
    if ans1<x/2 and i<n:
        ans1+=a[i]
        i+=1
    else:
        break
if i==n:
    print(2*a[i-1]-x)
else:
    i-=1
    ans2=x-ans1
    ans1-=a[i]
    print(a[i]-abs(ans1-ans2))
Buggy code:
import numpy as np

N=int(input())
A=np.array(list(map(int, input().split())))
S=sum(A)
X=A[0]
ans=abs(S-X)
for i in range(1,N-1):
  X+=A[i]
  ans=min(ans,abs(S-X))
print(ans)

Fixed code:
import numpy as np

N=int(input())
A=np.array(list(map(int, input().split())))
S=sum(A)-A[0]
X=A[0]
ans=abs(S-X)
for i in range(1,N-1):
  X+=A[i]
  S-=A[i]
  ans=min(ans,abs(S-X))
print(ans)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
b = sum(a)
l = 0
r = 0
x = 0
ans = 0
for i in a:
  l += i
  r = b - l
  if l==r:
    break;
  elif l>r:
    ans = min(l-r,y-x)
    print(l-r,y-x)
    break;
  x = l
  y = r
print(ans)

Fixed code:
n = int(input())
a = list(map(int,input().split()))
b = sum(a)
l = 0
r = 0
x = 0
ans = 0
for i in a:
  l += i
  r = b - l
  if l==r:
    break;
  elif l>r:
    ans = min(l-r,y-x)
    break;
  x = l
  y = r
print(ans)
Buggy code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
ruiseki = [0]+list(itertools.accumulate(A))
i = bisect.bisect_left(ruiseki,sum(A)/2)
print(i)
print(min(abs(sum(A[:i])-sum(A[i:])),abs(sum(A[:i-1])-sum(A[i-1:]))))


Fixed code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
A = l()
ruiseki = [0]+list(itertools.accumulate(A))
i = bisect.bisect_left(ruiseki,sum(A)/2)
print(min(abs(sum(A[:i])-sum(A[i:])),abs(sum(A[:i-1])-sum(A[i-1:]))))
Buggy code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    from itertools import accumulate

    N = int(input())  # len(A)
    A = list(map(int, input().split()))
    zenbu=sum(A)
    B = [0] + A
    B = list(accumulate(B))
    C = [zenbu]*N
    import numpy as np
    B=np.array(B[1:])
    C=np.array(C)
    D=C-B
    ans=10**18
    for i in range(1, N+1):
        ans = min(abs(B[i]-D[i]),ans)
    print(ans)
resolve()

Fixed code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    from itertools import accumulate

    N = int(input())  # len(A)
    A = list(map(int, input().split()))
    zenbu=sum(A)
    B = [0] + A
    B = list(accumulate(B))
    C = [zenbu]*(N+1)
    import numpy as np
    B=np.array(B)
    C=np.array(C)
    D=C-B
    ans=10**18
    for i in range(1, N):
        ans = min(abs(B[i]-D[i]),ans)
    print(ans)
resolve()
Buggy code:
n=int(input())

x=list(map(int,input().split()))

s=0

for i in range(len(x)):

	s+=x[i]i=0

l=0

while (l<(s//2)):

	l+=x[i]

	i+=1

r=abs((s-l)-l) 

i=len(x)-1

l=0

while(l<(s//2)):

	l+=x[i]

	i-=1

if abs((s-l)-l)<r:

	r=abs((s-l)-l) 

print (r)

Fixed code:
n=int(input())
x=list(map(int,input().split()))

s=0
for i in range(len(x)):
	s+=x[i]
i=0
l=0
while (l<(s//2)):
	l+=x[i]
	i+=1
r=abs(s-l-l) 
i=len(x)-1
l=0
while(l<(s//2)):
	l+=x[i]
	i-=1
if abs((s-l)-l)<r:
	r=abs(s-l-l) 
print (r)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 0
f
for i in range(n):
  k += a[i]
  if k >= a//2:
    f = i
    break
print(2*k - s)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 0
f = 0
for i in range(n):
  k += a[i]
  if k >= s//2:
    f = i
    break
k = max(k, s - k)
k2 = k - a[f]
k2 = max(k2, s - k2)
print(min(2*k - s, 2*k2 - s))
Buggy code:
import sys
n = int(input())
a = list(map(int, input().split()))
tmp=sys.maxsize
s = sum(a)
for i in range(n):
    t += a[i]
    tmp = min(tmp,abs(s-t*2))
print(tmp)

Fixed code:
import sys
n = int(input())
a = list(map(int, input().split()))
tmp=sys.maxsize
s = sum(a)
t = 0
for i in range(n):
    t += a[i]
    tmp = min(tmp,abs(s-t*2))
print(tmp)
Buggy code:
from itertools import accumulate as ac
n=int(input())
a=list(ac(list(map(int,input().split()))))
d=float("INF")
for i in range(n):
    d=min(d,abs(d-2*a[i]))
print(d)

Fixed code:
from itertools import accumulate as ac
n=int(input())
a=list(ac(list(map(int,input().split()))))
d=float("INF")
for i in range(n):
    d=min(d,abs(a[-1]-2*a[i]))
print(d)
Buggy code:
X,Y = list(map(int,input().split()))

n = (-X+2*Y)//3
m = (2*X-Y)//3
MOD = 10**9+7
    
def comb(n,r,MOD):
    x = n+1
    y = min(r,n-r)
    
    numer = 1
    denom = 1
    
    for i in range(1,r+1):
        numer = numer*(x-i)%MOD
        denom = denom*(i)%MOD
    
    return numer * pow(denom,MOD-2,MOD) % MOD
  
import sys

if n < 0 or m < 0:
    print(0)
    sys.exit()
if X+Y%3 != 0:
    print(0)
    sys.exit()
    
print(comb(n+m,n,MOD))

Fixed code:
X,Y = list(map(int,input().split()))

n = (-X+2*Y)//3
m = (2*X-Y)//3
MOD = 10**9+7
    
def comb(n,r,MOD):
    x = n+1
    y = min(r,n-r)
    
    numer = 1
    denom = 1
    
    for i in range(1,r+1):
        numer = numer*(x-i)%MOD
        denom = denom*(i)%MOD
    
    return numer * pow(denom,MOD-2,MOD) % MOD
  
import sys

if n < 0 or m < 0:
    print(0)
    sys.exit()
if (X+Y)%3 != 0:
    print(0)
    sys.exit()
    
print(comb(n+m,n,MOD))
Buggy code:
x,y=map(int,input().split())
mod=10**9+7
n_max=10**6+1
F,FI=[0]*(n_max+1),[0]*(n_max+1)
F[0],FI[0]=1,1
for i in range(n_max):
  F[i+1]=(F[i]*(i+1))%mod
  FI[i+1]=pow(F[i+1],mod-2,mod)
def comb(x,y):
  return (F[x]*FI[x-y]*FI[y])%mod
a,b=2*y-x,2*x-y
if a<0 or b<0:
  print(0)
elif a%3!=0 or b%3!=0:
  print(0)
else:
  a//=3
  b//=3
  print(comb(a+b,a))


Fixed code:
x,y=map(int,input().split())
mod=10**9+7
n_max=10**6+1
F=[0]*(n_max+1)
F[0]=1
for i in range(n_max):
  F[i+1]=(F[i]*(i+1))%mod
def comb(x,y):
  return (F[x]*pow(F[x-y],mod-2,mod)*pow(F[y],mod-2,mod))%mod
a,b=2*y-x,2*x-y
if a<0 or b<0:
  print(0)
elif a%3!=0 or b%3!=0:
  print(0)
else:
  a//=3
  b//=3
  print(comb(a+b,a))
Buggy code:
import sys
MOD = 10**9 + 7

def mod_inv(n, m):
    return pow(n, m-2, m)

def nCr_mod_m(n, r, m):
    if r < 0 or n < r:
        return 0
    # n!
    fact = fact_prev = 1 # 0! = 1! = 1
    for i in range(2, n+1):
        fact = fact_prev * i % m
        fact_prev = fact
    # n!^(-1)
    fact_inv = [0]*(max(r, n-r)+1)
    fact_inv[0] = fact_inv[1] = 1  # 1 = 1 mod m
    for i in range(2, max(r, n-r)+1):
        fact_inv[i] = fact_inv[i-1] * mod_inv(i, m) % m
    return (fact * fact_inv[r] % m) * fact_inv[n-r] % m

def main():
    X, Y = map(int, input().split())
    if ((X+Y)%3 != 0) or (min(X,Y)*2 < max(X,Y)):
        print(0)
        sys.exit()

    base = (X + Y) // 3
    x = X - base
    y = Y - base
    print(x, y)

    print(nCr_mod_m(x+y, x, MOD))
main()

Fixed code:
import sys
MOD = 10**9 + 7

def mod_inv(n, m):
    return pow(n, m-2, m)

def nCr_mod_m(n, r, m):
    if r < 0 or n < r:
        return 0
    # n!
    fact = fact_prev = 1 # 0! = 1! = 1
    for i in range(2, n+1):
        fact = fact_prev * i % m
        fact_prev = fact
    # n!^(-1)
    fact_inv = [0]*(max(r, n-r)+1)
    fact_inv[0] = fact_inv[1] = 1  # 1 = 1 mod m
    for i in range(2, max(r, n-r)+1):
        fact_inv[i] = fact_inv[i-1] * mod_inv(i, m) % m
    return (fact * fact_inv[r] % m) * fact_inv[n-r] % m

def main():
    X, Y = map(int, input().split())
    if ((X+Y)%3 != 0) or (min(X,Y)*2 < max(X,Y)):
        print(0)
        sys.exit()

    base = (X + Y) // 3
    x = X - base
    y = Y - base

    print(nCr_mod_m(x+y, x, MOD))
main()
Buggy code:
a,b=map(int,input().split())
if (2*b-a)%3!=0 or (2*a-b)%3!=0:
  print(0)
n=(2*b-a)//3
m=(2*a-b)//3
p = 10**9+7
def nCr(n,r):
    if r*2>n:
        return nCr(n,n-r)
    else:
        a = b = 1
        for i in range(r):
            a = a * (n - i) % p
            b = b * (r - i) % p
        return (a * pow(b,p-2,p)) % p

print(nCr((m+n),m))

Fixed code:
a,b=map(int,input().split())
if (2*b-a)%3==0 and (2*a-b)%3==0 and (2*b-a)>=0 and (2*a-b)>=0:
  n=(2*b-a)//3
  m=(2*a-b)//3
  p = 10**9+7
  def nCr(n,r):
    if r*2>n:
        return nCr(n,n-r)
    else:
        a = b = 1
        for i in range(r):
            a = a * (n - i) % p
            b = b * (r - i) % p
        return (a * pow(b,p-2,p)) % p

  print(nCr((m+n),m))
else:
  print(0)
Buggy code:
X, Y = map(int,input().split())

MOD = 10**9 + 7

def comb(n, r):
    if n < r or n < 0 or r < 0:
        return 0

    n += 1
    over = 1
    under = 1
    for i in range(1,r + 1):
      over *= (n - i) % MOD
      under *= i % MOD
    return over * pow(under, MOD - 2, MOD) % MOD

if (X + Y) % 3 != 0:
    print(0)
else:
    a = (2 * X - Y) // 3
    b = (2 * Y - X) // 3
    print(comb(a+b, a))



Fixed code:
X, Y = map(int,input().split())

MOD = 10**9 + 7

def comb(n, r):
    if n < r or n < 0 or r < 0:
        return 0

    n += 1
    over = 1
    under = 1
    for i in range(1,r + 1):
      over = over * (n - i) % MOD
      under = under * i % MOD
    return over * pow(under, MOD - 2, MOD) % MOD

if (X + Y) % 3 != 0:
    print(0)
else:
    a = (2 * X - Y) // 3
    b = (2 * Y - X) // 3
    print(comb(a+b, a))
Buggy code:
import sys
input = lambda: sys.stdin.readline().rstrip()

mod = 1000000007
X,Y = map(int, input().split())


def combination(n, r, mod):
    r = min(r, n-r)

    numer = 1
    denom = 1

    for i in range(r):
        numer = numer * (n-i) % mod
        denom = denom * (r-i) % mod

    return numer * pow(denom, mod-2, mod) % mod

m_n = (X+Y) // 3
m = X - m_n
n = Y - m_n

if X+Y % 3 == 0 or m < 0 or n < 0:
    print(0)
    exit()

print(combination(m_n, m, mod))

Fixed code:
import sys
input = lambda: sys.stdin.readline().rstrip()

MOD = 1000000007

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

if (X+Y) % 3 != 0:
    print(0)
    exit()

def combination(n,r,mod):
    r = min(r, n-r)
    numer = denom = 1

    for i in range(r):
        numer = numer*(n-i)%mod
        denom = denom*(i+1)%mod
    return numer * pow(denom, mod-2, mod) % mod


tmp = (X+Y) // 3
m = X - tmp
n = Y - tmp
if m < 0 or n < 0:
    print(0)
    exit() 

ans = combination(m+n, m, MOD)
print(ans)
Buggy code:
# coding: utf-8


def solve(*args: str) -> str:
    x, y = map(int, args[0].split())
    ret = 0
    mod = 1000000007

    if (2*y-x) % 3 == 0 and (2*x-y) % 3 == 0:
        ret = 1
        a = (2*y-x)//3
        b = (2*x-y)//3
        for n in range(2, a+b+1):
            ret = (ret * (n % mod)) % mod

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))


Fixed code:
# coding: utf-8


def solve(*args: str) -> str:
    x, y = map(int, args[0].split())
    ret = 0
    mod = 1000000007

    if (x+y) % 3 == 0 and not (x > 2*y or y > 2*x):
        ret = 1
        a = (2*y-x)//3
        b = (2*x-y)//3
        for n in range(a+b, a, -1):
            ret = (ret * (n % mod)) % mod

        div = 1
        for k in range(b, 1, -1):
            div = (div * (k % mod)) % mod

        ret = (ret * pow(div, mod-2, mod)) % mod

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))
Buggy code:
mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]


def init(n):
    for i in range(2, n):
        fac.append(fac[-1] * i % mod)
        inv.append(-inv[mod % i] * (mod // i) % mod)
        finv.append(finv[-1] * inv[-1] % mod)


def com(n, k, mod):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod


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

if not (X+Y) % 3 == 0:
    print(0)
else:
    n = (Y - 2 * X) // -3
    m = X - (2 * n)
    if n < 0 or m < 0:
        print(0)
    else:
        init(n + m)
        print(com(n + m, n, mod))


Fixed code:
mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]


def init(n):
    for i in range(2, n + 1):
        fac.append(fac[-1] * i % mod)
        inv.append(-inv[mod % i] * (mod // i) % mod)
        finv.append(finv[-1] * inv[-1] % mod)


def com(n, k, mod):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod


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

if not (X+Y) % 3 == 0:
    print(0)
else:
    n = (Y - 2 * X) // -3
    m = X - (2 * n)
    if n < 0 or m < 0:
        print(0)
    else:
        init(n + m)
        print(com(n + m, n, mod))
Buggy code:
import sys
input = sys.stdin.readline

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

X, Y = [int(x) for x in input().strip().split()]
p = (X, 2 * X)
n1 = X
n2 = 0
f = True
while p != (X, Y):
    n1 -= 2
    n2 += 1
    p = (n1 + 2 * n2, 2 * n1 + n2)
    if p[1] <= 0:
      	f = False
        break
if f:
    print(combination(n1 + n2, n1) % (10 ** 9 + 7))
else:
    print(0)

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

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

X, Y = [int(x) for x in input().strip().split()]
p = (X, 2 * X)
n1 = X
n2 = 0
f = True
while p != (X, Y):
    n1 -= 2
    n2 += 1
    p = (n1 + 2 * n2, 2 * n1 + n2)
    if p[1] <= 0:
        f = False
        break

if n1 < 0 or n2 < 0:
    print(0)
else:
    print(combination(n1 + n2, n1) % (10 ** 9 + 7))
Buggy code:
x, y = map(int, input().split())
z = x + y
mod = 10 ** 9 + 7
if z % 3 == 0:
  w = z // 3
  a, b = sorted([x - w, y - w])
  inv_mod = [1] * a
  num = b + 1
  den = 1
  for i in range(2, a + 1):
    num = num * (b + i) % mod
    inv_mod[i] = -(mod // i) * inv_mod[mod % i] % mod
    den = den * inv_mod[i] % mod
  print(num * den % mod)
else:
  print(0)


Fixed code:
x, y = map(int, input().split())
z = x + y
mod = 10 ** 9 + 7
if z % 3 == 0:
  w = z // 3
  a, b = sorted([x - w, y - w])
  if a < 0:
    print(0)
  elif a == 0:
    print(1)
  else:
    inv_mod = [1] * (a + 1)
    num = b + 1
    den = 1
    for i in range(2, a + 1):
      num = num * (b + i) % mod
      inv_mod[i] = -inv_mod[mod % i] * (mod // i) % mod
      den = den * inv_mod[i] % mod
    print(num * den % mod)
else:
  print(0)
Buggy code:
def comb(n, r, mod):
    if n < 0 or r < 0 or r > n: return 0
    nu = 1
    de = 1
    for i in range(r):
        nu = nu * (n-i) % mod
        de = de * (i+1) % mod
    return nu * inv(de, mod) % mod
 
def inv(a, mod):
    b = mod
    p, q = 1, 0
    while b > 0:
        c = a//b
        d = a; a = b; b = d % b
        d = p; p = q; q = d-c*q
    return p+mod if p < 0 else p

x, y = map(int, input().split())

m = (2 * x -y) // 3
n = (2 * y -x) // 3
mod = 10**9+7
if n >= 0 and m >= 0:
  ans = comb(n+m, m, mod)
  print(ans)
else:
  print(0)
  

Fixed code:
def comb(n, r, mod):
    if n < 0 or r < 0 or r > n: return 0
    nu = 1
    de = 1
    for i in range(r):
        nu = nu * (n-i) % mod
        de = de * (i+1) % mod
    return nu * inv(de, mod) % mod
 
def inv(a, mod):
    b = mod
    p, q = 1, 0
    while b > 0:
        c = a//b
        d = a; a = b; b = d % b
        d = p; p = q; q = d-c*q
    return p+mod if p < 0 else p

x, y = map(int, input().split())

m = (2 * x -y) // 3
n = (2 * y -x) // 3
mod = 10**9+7
if n >= 0 and m >= 0 and (x + y) % 3 == 0:
  ans = comb(n+m, m, mod)
  print(ans)
else:
  print(0)
Buggy code:
import sys
X, Y = map(int, input().split())

MOD = 1000000007
if (2*X-Y)%3!=0 or (2*X-Y)<0:
    print(0)
    sys.exit()

A = int((2*X-Y)/3)
B = int(Y-2*A)

if A>B:

ans = 1
inv = 1
for i in range(B):
    ans = ans * (A+B-i) %MOD
    inv = inv * (i+1) %MOD

ans = ans * pow(inv, MOD-2, MOD) %MOD
print(ans)


Fixed code:
import sys
X, Y = map(int, input().split())

MOD = 1000000007
if (2*X-Y)%3!=0:
    print(0)
    sys.exit()

A = int((2*X-Y)/3)
B = int(X-2*A)

if A<0 or B<0:
    print(0)
    sys.exit()

ans = 1
inv = 1
for i in range(B):
    ans = ans * (A+B-i) %MOD
    inv = inv * (i+1) %MOD

ans = ans * pow(inv, MOD-2, MOD) %MOD
print(ans)
Buggy code:
Z = 10 ** 9 + 7


def z_pow(a, n):
    # a ^ n
    if n == 0:
        return 1
    if n == 1:
        return a % Z
    m = n // 2
    i = n - m * 2
    x = z_pow(a, m)
    return (x * x * z_pow(a, i)) % Z


def n_comb(n, i):
    if n - i < i:
        return n_comb(n, n - i)
    e = [1 for j in range(n)]
    f = list(e)
    for j in range(n - 1):
        e[j + 1] = (e[j] * (j + 2)) % Z
        f[j + 1] = z_pow(e[j + 1], Z - 2)
    return (e[n - 1] * f[i - 1] * f[n - i - 1]) % Z


def calc(X, Y):
    if (X + Y) % 3 != 0:
        return 0
    n = (X + Y) // 3
    if X < n or Y < n:
        return 0
    return n_comb(n, X - n)


(X, Y) = tuple([int(s) for s in input().split()])
print(calc(X, Y))

Fixed code:
Z = 10 ** 9 + 7


def z_pow(a, n):
    # a ^ n
    if n == 0:
        return 1
    if n == 1:
        return a % Z
    m = n // 2
    i = n - m * 2
    x = z_pow(a, m)
    return (x * x * z_pow(a, i)) % Z


def n_comb(n, i):
    if n - i < i:
        return n_comb(n, n - i)
    e = [1 for j in range(n + 1)]
    for j in range(n):
        e[j + 1] = (e[j] * (j + 1)) % Z
    return (e[n] * z_pow(e[i], Z - 2) * z_pow(e[n - i], Z - 2)) % Z


def calc(X, Y):
    if (X + Y) % 3 != 0:
        return 0
    n = (X + Y) // 3
    if X < n or Y < n:
        return 0
    return n_comb(n, X - n)


(X, Y) = tuple([int(s) for s in input().split()])
print(calc(X, Y))