Problem p02862 (python)

Fixed submission:
import sys


def alg_combination_mod(n, r, mod):
    r = min(n - r, r)
    if r == 0:
        return 1
    else:
        denominator = 1
        for i in range(n, n - r, -1):
            denominator = (denominator * i) % mod
        molecule = 1
        for i in range(1, r + 1):
            molecule = (molecule * i) % mod
        return denominator * pow(molecule, mod - 2, mod) % mod


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, y = list(map(int, readline().split()))
    if (x + y) % 3 == 0:
        t = (x + y) // 3
        for i in range(0, t + 1):
            tx = i * 2 + (t - i)
            ty = i + (t - i) * 2
            if tx == x and ty == y:
                print(alg_combination_mod(t, i, mod))
                exit()
        print(0)
    else:
        print(0)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys


def alg_combination_mod(n, r, mod):
    r = min(n - r, r)
    if r == 0:
        return 1
    else:
        denominator = 1
        for i in range(n, n - r, -1):
            denominator = (denominator * i) % mod
        molecule = 1
        for i in range(1, r + 1):
            molecule = (molecule * i) % mod
        return denominator * pow(molecule, mod - 2, mod) % mod


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, y = list(map(int, readline().split()))
    if x / y == 2 or y / x == 2:
        print(1)
    else:
        cnt = min(x // 3, y // 3)
        x -= cnt * 3
        y -= cnt * 3
        if x == 0 and y == 0:
            xcnt = cnt
            ycnt = cnt
        elif x % 2 == 0 and y % 2 == 0:
            if x != 0 and y / x == 2:
                    xcnt = cnt + x
                    ycnt = cnt
            elif y != 0 and x / y == 2:
                    xcnt = cnt
                    ycnt = cnt + y
        else:
            print(0)
            exit()
        print(alg_combination_mod(xcnt + ycnt, xcnt, mod))


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys


def alg_combination_mod(n, r, mod):
    r = min(n - r, r)
    if r == 0:
        return 1
    else:
        denominator = 1
        for i in range(n, n - r, -1):
            denominator = (denominator * i) % mod
        molecule = 1
        for i in range(1, r + 1):
            molecule = (molecule * i) % mod
        return denominator * pow(molecule, mod - 2, mod) % mod


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    x, y = list(map(int, readline().split()))
    if x / y == 2 or y / x == 2:
        print(1)
    else:
        cnt = min(x // 3, y // 3)
        x -= cnt * 3
        y -= cnt * 3
        if x == 0 and y == 0:
            xcnt = cnt
            ycnt = cnt
        elif x % 2 == 0 and y % 2 == 0:
            if y / x == 2:
                xcnt = cnt + x // 2
                ycnt = cnt
            elif x / y == 2:
                xcnt = cnt
                ycnt = cnt + y // 2
        else:
            print(0)
            exit()
        print(alg_combination_mod(xcnt + ycnt, xcnt, mod))


if __name__ == '__main__':
    solve()