Problem p02862 (python)

Fixed submission:
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



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

if (2*y-x)%3 == 0 and (2*x-y)%3 == 0:
    m = (2*y-x)//3
    n = (2*x-y)//3

    if n >= 0 or m >= 0:
        print( str(int(cmb(n+m,n, mod) % (10e9+7)) ))
    else:
        print(0)
else:
    print(0)

Buggy submission (verdict: Runtime Error):
#import itertools
from scipy.special import comb

def cmb_1(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def main():
    x, y = map(int, input().split())

    if x < y:
        x_ = y
        y_ = x

        x = y_
        y = x_
    
    if (x+y) % 3 != 0:
        print(0)

    else:
        n = (x+y)//3
        r = x//2

        #a = len(list(itertools.combinations(range(n), r)))
        #a = comb(n, r, exact=True)
        #a = comb_1(n, r)
        def cmb(n, r, mod):
            if ( r<0 or r>n ):
                return 0
            r = min(r, n-r)
            return g1[n] * g2[r] * g2[n-r] % mod

        mod = 10**9+7 #出力の制限
        N = 10**6
        g1 = [1, 1] # 元テーブル
        g2 = [1, 1] #逆元テーブル
        inverse = [0, 1] #逆元テーブル計算用テーブル

        for i in range( 2, N + 1 ):
            g1.append( ( g1[-1] * i ) % mod )
            inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
            g2.append( (g2[-1] * inverse[-1]) % mod )

        a = cmb(n,r,mod)

        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

def main():
    x, y = map(int, input().split())

    if x < y:
        x_ = y
        y_ = x

        x = y_
        y = x_
    
    if (x+y) % 3 != 0:
        print(0)

    else:
        n = (x+y)//3
        r = x//2

        #a = len(list(itertools.combinations(range(n), r)))
        #a = comb(n, r, exact=True)
        #a = comb_1(n, r)
        def cmb(n, r, mod):
            if ( r<0 or r>n ):
                return 0
            r = min(r, n-r)
            return g1[n] * g2[r] * g2[n-r] % mod

        mod = 10**9+7 #出力の制限
        N = 10**4
        g1 = [1, 1] # 元テーブル
        g2 = [1, 1] #逆元テーブル
        inverse = [0, 1] #逆元テーブル計算用テーブル

        for i in range( 2, N + 1 ):
            g1.append( ( g1[-1] * i ) % mod )
            inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
            g2.append( (g2[-1] * inverse[-1]) % mod )

        a = cmb(n,r,mod)

        print(a)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#import itertools
from scipy.special import comb

def cmb_1(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def main():
    x, y = map(int, input().split())

    if x < y:
        x_ = y
        y_ = x

        x = y_
        y = x_
    
    if (x+y) % 3 != 0:
        print(0)

    else:
        n = (x+y)//3
        r = x//2

        #a = len(list(itertools.combinations(range(n), r)))
        #a = comb(n, r, exact=True)
        #a = comb_1(n, r)
        def cmb(n, r, mod):
            if ( r<0 or r>n ):
                return 0
            r = min(r, n-r)
            return g1[n] * g2[r] * g2[n-r] % mod

        mod = 10**9+7 #出力の制限
        N = 10**4
        g1 = [1, 1] # 元テーブル
        g2 = [1, 1] #逆元テーブル
        inverse = [0, 1] #逆元テーブル計算用テーブル

        for i in range( 2, N + 1 ):
            g1.append( ( g1[-1] * i ) % mod )
            inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
            g2.append( (g2[-1] * inverse[-1]) % mod )

        a = cmb(n,r,mod)

        print(a)

        #print(int(a) % ((1e9) + 7))

if __name__ == '__main__':
    main()