Problem:
 Score : 400 points    Problem Statement There is a knight - the chess piece - at the origin (0, 0) of a two-dimensional grid.  When the knight is at the square (i, j) , it can be moved to either (i+1,j+2) or (i+2, j+1) .  In how many ways can the knight reach the square (X, Y) ?  Find the number of ways modulo 10^9 + 7 .      Constraints  1 \leq X \leq 10^6  1 \leq Y \leq 10^6  All values in input are integers.         Input Input is given from Standard Input in the following format:  X  Y       Output Print the number of ways for the knight to reach (X, Y) from (0, 0) , modulo 10^9 + 7 .        Sample Input 1 3 3      Sample Output 1 2  There are two ways: (0,0) \to (1,2) \to (3,3) and (0,0) \to (2,1) \to (3,3) .       Sample Input 2 2 2      Sample Output 2 0  The knight cannot reach (2,2) .       Sample Input 3 999999 999999      Sample Output 3 151840682  Print the number of ways modulo 10^9 + 7 .

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect
import functools


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v

def comb(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 bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False



dp = None


def main():
    x, y = li_input()

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

    n = 0
    m1, m2 = 0, 0
    while x != 0 and y != 0:
        n += 1
        if x >= y:
            x -= 2
            y -= 1
            m1 += 1
        else:
            x -= 1
            y -= 2
            m2 += 1

    if not (x == 0 and y == 0):
        print(0)
        return(0)

    print(comb(n, m1) % (10 ** 9 + 7))


main()

Solution (Python):
x, y = [int(i) for i in input().split()]

mod = 10 ** 9 + 7
g1 = [1, 1]  
g2 = [1, 1]  
inverse = [0, 1]  

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



def com(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


if (2*x-y) % 3 == 0 and (2*y-x) % 3 == 0:
    a = (2*x-y) // 3
    b = (2*y-x) // 3
    
    ans = 0
    n = a + b
    r = min(a, b)
    print(com(n, r, mod))

else:
    print(0)

Solution (Python):
x, y = map(int, input().split())

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 
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1]

if (x+y)%3 != 0 :
  print(0)
else:
  n = (x + y) // 3
  if x >= n and y >= n:
    a = x - n
    b = y - n
    
    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 )
    
    answer = cmb(a+b,a,mod)
    print(answer)
  else:
    print(0)

Solution (Python):
import math
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

X,Y = map(int, input().split()) 
mod = 10**9+7 
N = 10**4
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

if (X+Y)%3!=0:
  print(0)
else:
  N=(X+Y)//3
  if X>=N and Y>=N:
    a=X-N
    b=Y-N
    
    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 )

    C = cmb(a+b,a,mod)
    print(C)
  else:
    print(0)

Solution (Python):
import math
import sys
sys.setrecursionlimit(10**5)
x, y = map(int, input().split())
mod = 10**9+7

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if (x + y) % 3 != 0:
    print(0)
    sys.exit()

if y > 2 * x or y < x / 2:
    print(0)
    sys.exit()

cnt = (x + y) // 3

p = (2 * x - y) // 3



comb = Combination(1000000)
ans = comb(cnt, p)

print(ans)

Solution (Python):
from math import factorial as f
p = int(1e9+7)
X, Y = map(int, input().split())
n = (2 * X - Y) // 3
m = (2 * Y - X) // 3
if(n < 0 or m < 0 or (X + Y) % 3 > 0):
  print(0)
else:
  fac = [0] * (n+m+1)
  inv = [0] * (n+m+1)
  finv = [0] * (n+m+1)

  fac[0] = fac[1] = 1
  inv[1] = 1
  finv[0] = finv[1] = 1
  
  for i in range(2,n+m+1):
    fac[i] = fac[i-1] * i % p
    inv[i] = p - inv[p%i] * (p // i) % p
    finv[i] = finv[i-1] * inv[i] % p

  print(fac[n+m] * finv[n] * finv[m] % p)

Solution (Python):
mod=1000000007
fact=[None for i in range(1000000)]
fact[0]=1
for i in range(1,1000000):
  fact[i]=fact[i-1]*i
  fact[i]%=mod

def inv(x):
  n = mod-2
  ans = 1
  while n!=0:
    if n&1:
      ans = ans*x % mod
    x = x*x % mod
    n //= 2
  return ans
 
def comb(n,r):
  return (fact[n]*inv(fact[r])*inv(fact[n-r]))%mod

a,b=map(int,input().split())
j=None
for i in range(1000000):
  j=b-i*2
  if j<0:
    break
  if i+j*2==a:
    print(comb(i+j, i))
    break
if j<0:
  print(0)

Solution (Python):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]
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

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 = na()



if ((x+y) % 3) == 0:
    i = (x+y)//3
    print(cmb(i,x-i, mod))
else:
    print(0)

Solution (Python):
x,y  = map(int, input().split())


def modconb(n,k,mod):
    
    fac = [0]*(n+1)
    finv = [0]*(n+1)
    inv = [0]*(n+1)  

    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1

    for i in range(2,n+1):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod%i] * (mod//i) % mod
        finv[i] = finv[i-1] * inv[i] %mod
    
    if n<k: return 0
    if n<0 or k<0: return 0
    return(fac[n]*(finv[k]*finv[n-k]%mod)%mod)
    
mod = 10**9 + 7 
if (x+y) % 3 != 0:
    print(0)
    exit()
i = (x+y)//3
j = (x-i)
print(modconb(i,j,mod))

Solution (Python):
import math
M = 10**9+7
X,Y = map(int,input().split())
if (X+Y)/3 != (X+Y)//3:
    print(0)
    exit()
n = (X+Y)//3
r = Y-n
if r<0 or r>n:
    print(0)
    exit()



def nCr(n, r, mod=10**9+7):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod

print(nCr(n,r,M))

Solution (Python):
x, y = [int(i) for i in input().split()]

mod = 10 ** 9 + 7
g1 = [1, 1]  
g2 = [1, 1]  
inverse = [0, 1]  

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



def com(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


if (2*x-y) % 3 == 0 and (2*y-x) % 3 == 0:
    a = (2*x-y) // 3
    b = (2*y-x) // 3
    
    ans = 0
    n = a + b
    r = min(a, b)
    print(com(n, r, mod))

else:
    print(0)

Solution (Python):
def nCr(n, r, mod=10**9+7):
    r = min(r, n-r)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n+1-i) % mod
        denom = denom * i % mod
    return numer * pow(denom, mod-2, mod) % mod


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

if (x + y) % 3 != 0:
    print(0)
    exit()
if x > 2*y or y > 2 * x:
    print(0)
    exit()





print(nCr((x + y)//3, (-x+2*y)//3))

Solution (Python):
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

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

if (2*y-x)%3==0:
    a=(2*y-x)//3
    b=(2*x-y)//3
    mod = 10**9+7 
    N = a+b
    r=min(a,b)
    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 )

    print(cmb(N,r,mod))
else:
    print(0)

Solution (Python):
import math
import itertools
import sys

x, y = [int(x) for x in input().split()]

if (x+y) % 3:
  print(0)
  sys.exit()

total_move = (x+y) // 3
move_a = 2 * total_move - x

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 = total_move
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(total_move,move_a,mod)

print(a)

Solution (Python):
MOD = 10 ** 9 + 7


def modpow(a, b, mod):
    ret = 1
    while b > 0:
        if b % 2 == 1:
            ret = ret * a % mod
        a = a * a % mod
        b //= 2
    return ret


X, Y = map(int, input().split())
if (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
    print(0)
elif (2 * X - Y) // 3 < 0 or (2 * Y - X) // 3 < 0:
    print(0)
else:
    A, B = (2 * X - Y) // 3, (2 * Y - X) // 3
    ans = 1
    for n in range(A + B, A, -1):
        ans = ans * n % MOD
    for n in range(1, B + 1):
        ans = ans * modpow(n, MOD - 2, MOD) % MOD
    print(ans % MOD)

Solution (Python):
def combnk_mod(n, k, p):
    
    
    
    

    if  2 * k > n:
        k = n - k
    x = 1 
    y = 1 
    for i in range(1, k + 1):
        x *= pow(i, p - 2, p)
        y *= n - i + 1
        x %= p
        y %= p
    return (x * y) % p

MOD = 10 ** 9 + 7
x, y = map(int, input().split())
m = 2 * x - y
n = 2 * y - x
if m < 0 or n < 0:
    ans = 0
elif m % 3 > 0 or n % 3 > 0:
    ans = 0
else:
    m = m // 3
    n = n // 3
    ans = combnk_mod(m + n, min(m, n), MOD)
print(ans)

Solution (Python):
def combnk_mod(n, k, p):
    
    
    
    

    if  2 * k > n:
        k = n - k
    x = 1 
    y = 1 
    for i in range(1, k + 1):
        x *= pow(i, p - 2, p)
        y *= n - i + 1
        x %= p
        y %= p
    return (x * y) % p

MOD = 10 ** 9 + 7
x, y = map(int, input().split())
m = 2 * x - y
n = 2 * y - x
if m < 0 or n < 0:
    ans = 0
elif m % 3 > 0 or n % 3 > 0:
    ans = 0
else:
    m = (2 * x - y) // 3
    n = (2 * y - x) // 3
    ans = combnk_mod(m + n, min(m, n), MOD)
print(ans)

Solution (Python):
def cmb(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


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

b = max(x, y)
s = min(x, y)

n = (2 * y - x) // 3
m = (2 * x - y) // 3

ans = 0
if (x+y) % 3 != 0 or b / s > 2:
    print(ans)
else:
    print(cmb(n+m, n) % (10 ** 9 + 7) )

Solution (Python):
from sys import stdin


def cmb(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 = [int(x) for x in stdin.readline().rstrip().split()]
    if (X + Y) % 3:
        print(0)
    else:
        m = (2 * X - Y) // 3
        n = X - 2 * m
        if n < 0 or m < 0:
            print(0)
        else:
            ans = cmb(n + m, min(n, m))
            print(ans % (10 ** 9 + 7))


if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin

MAX = 10 ** 6
MOD = 10 ** 9 + 7
fac = [0] * MAX
finv = fac[:]
inv = fac[:]


def cmb_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        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 cmb(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD


def main():
    X, Y = [int(x) for x in stdin.readline().rstrip().split()]
    if (X + Y) % 3:
        print(0)
    else:
        m = (2 * X - Y) // 3
        n = X - 2 * m
        if n < 0 or m < 0:
            print(0)
        else:
            cmb_init()
            ans = cmb(n + m, min(n, m))
            print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math
import functools

law = 10 ** 9 + 7
def mul_mod(x, y):
  return x * y % law

def prod_mod(seq):
  return functools.reduce(mul_mod, seq, 1)

def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    
    return [w[0],w[1]]


def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m
  
def comb(n, k):
  return prod_mod(i for i in range(n - k + 1, n + 1)) * mod_inv(prod_mod(i for i in range(1, k + 1)), law) % law

if __name__ == "__main__":
  
  x, y = sorted(map(int, input().split()), reverse=True)
  
  if (x + y) % 3 != 0:
    print(0)
  else:
    print(comb((x + y) // 3, x - (x + y) // 3))

Solution (Python):

import sys
import math

MOD = 10**9+7  

def combi_count(n, r):
    x = 1
    y = 1
    for i in range(1, r+1):
        x = x * (n-i) % MOD
        y = y * i % MOD

    
    

    
    
    

    return x * pow(y, MOD-2, MOD) % MOD

def solve(X: int, Y: int):
    a = int((2*Y - X)/3)
    b = int((2*X - Y)/3)
    if ( ((2*Y - X)%3 != 0) and ((2*X - Y)%3 != 0) )  or ((a<0) or (b<0)):
        ans = 0
    else:
        ans = combi_count(a+b+1, min(a, b))
    print(ans)


def main():
    X, Y = map(int, input().split())
    solve(X, Y)

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
import numpy as np
mod = 10**9+7
X, Y = [int(x) for x in stdin.readline().rstrip().split()]


def mul(a, b):
    return ((a % mod) * (b % mod)) % mod


def div(a, b):
    return mul(a, pow(b, mod-2, mod))


def fact(n):
    f = 1
    for i in range(1, n+1):
        f *= i
        f %= mod
    return f


A = np.matrix([[2, 1], [1, 2]])
Y = np.matrix([[X], [Y]])
if np.linalg.matrix_rank(np.c_[A, Y]) == np.linalg.matrix_rank(A):
    x, y = np.linalg.solve(A, Y)
    x = float(x)
    y = float(y)
    if x < 0 or y < 0:
        print(0)
    elif (not x.is_integer()) or (not y.is_integer()):
        print(0)
    else:
        x = int(x)
        y = int(y)
        ans = fact(x+y)
        ans = div(ans, fact(x))
        ans = div(ans, fact(y))
        print(ans % mod)
else:
    print(0)

Solution (Python):
sosu = 10 ** 9 + 7
X, Y = map(int, input().split())
amari = (X + Y) % 3
sho = (X + Y) // 3
x = 2 * sho - X
if amari == 0 and x >= 0 and x <= sho:
    fac = [1 for i in range(sho+1)]
    finv = [1 for i in range(sho+1)]
    inv = [1 for i in range(sho+1)]
    for i in range(2, sho+1):
        fac[i] = fac[i-1] * i % sosu
        inv[i] = sosu - inv[sosu%i] * (sosu//i) % sosu
        finv[i] = finv[i-1] * inv[i] % sosu
    
    
    ans = int(fac[sho] * (finv[x] * finv[sho-x] % sosu) % sosu)
else:
    ans = 0
print(ans)

Solution (Python):
X,Y=map(int, input().split())
if (X+Y)%3!=0:
  print(0)
  exit()
n=(X+Y)//3
flg=True
for i in range(n+1):
  if 2*i +(n-i) == X:
    right=i
    up=n-i
    flg=False
    break
  elif 2*(n-i)+i == X:
    up=i
    right=n-i
    flg=False
    break
if flg:
  print(0)
  exit()
  

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 
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,right,mod)
print(a)

Solution (Python):
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

import itertools

X, Y = two_int()

def comb(n,r,mod, N):
    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 
    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 )

    return cmb(n,r,mod)

def calc(a, b, p):
    if b==0:
        return 1
    elif b%2==0:
        d=calc(a,b//2, p)
        return (d*d)%p
    else:
        return (a*calc(a,b-1,p))%p

dan = (X+Y)/3
if dan.is_integer():
    n = int(dan+1)
    k = Y-(n-1)+1
    print(int(comb(n-1,k-1,(10**9+7),X)))
else:
    print(0)

Solution (Python):
X, Y = map(int, input().split())
M = 10**9+7
if (X + Y) % 3:
    print(0)
else:
    m = (2 * X - Y)//3
    n = (2 * Y - X)//3
    if m < 0 or n < 0:
        print(0)
    else:
        a = 1
        for i in range(1, n+m+1):
            a =  a * i % M
        b = 1
        for i in range(1, n+1):
            b = b * i % M
        c = 1
        for i in range(1, m+1):
            c = c * i % M
        print(a * pow(b, M-2, M) * pow(c, M-2, M) % M)

Solution (Python):

import itertools
import math

import sys

def modc(a, b, m):
    c = 1
    for i in range(b):
        c = c * (a - i) % m
        c = c * modinv(i + 1, m) % m
    return c

def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)

def modinv(a, m):
    (inv, q, gcd_val) = egcd(a, m)
    return inv % m


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




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

n = int((2 * Y - X) / 3)
m = int((2 * X - Y) / 3)

if n < 0 or m < 0 :
    print(0)
    sys.exit()

ans = modc(n + m, n, 10 ** 9 + 7)
print(ans)

Solution (Python):
import math

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

comb_list = {}
mod_param = 10**9 + 7

def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)


def modinv(a, m):
    (inv, q, gcd_val) = egcd(a, m)
    return inv % m

def comb(a, b):
  ans = 1
  for i in range(b+1, a+1):
    ans *= i
    ans %= mod_param
  for i in range(1, a-b+1):
    k = modinv(i, mod_param)
    ans *= k
    ans %= mod_param
  return ans

if (abs(2*X-Y)%3 != 0):
  print(0)
else:
  alpha = (2*Y-X)//3
  beta = (2*X-Y)//3
  if alpha > beta:
    print(comb(alpha+beta, beta))
  else:
    print(comb(alpha+beta, alpha))

Solution (Python):
def cmb(m, n):
    if m - n < n:
        n = m - n
    if n == 0:
        return 1
    if n == 1:
        return m
    
    numerator = [m - n + k + 1 for k in range(n)]
    denominator = [k + 1 for k in range(n)]

    for p in range(2, n+1):
        pivot = denominator[p-1]
        if pivot > 1:
            offset = (m-n) % p
            for k in range(p-1, n, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    
    result = 1
    for k in range(n):
        if numerator[k] > 1:
            result = result * int(numerator[k]) % (10**9 + 7)

    return result

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

if (X+Y) % 3 != 0:
    print(0)
else:
    m = int((2*X - Y) / 3)
    n = int((2*Y - X) / 3)
    if m < 0 or n < 0:
        print(0)
    else:
        ans = cmb(m+n, n)
        print(int(ans))

Solution (Python):
import sys

X, Y = map(int, input().split())
MOD = 10 ** 9 + 7

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

n1 = (2 * X - Y) // 3
n2 = (2 * Y - X) // 3

if n1 < 0 or n2 < 0:
    print(0)
    sys.exit()

P = 10 ** 9 + 7
N = 10 ** 6 + 5
inv_t = [0]+[1]
for i in range(2, N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

m = 10 ** 9 + 7
res = 1
for i in range(2, n1 + n2 + 1):
    res = res * i % m
for i in range(1, n2 + 1):
    res = res * inv_t[i] % m
for i in range(1, n1 + 1):
    res = res * inv_t[i] % m
print(res)

Solution (Python):
MAX = 1000000
MOD = 1000000007

fac = [None] * MAX
finv = [None] * MAX
inv = [None] * MAX


def comb_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        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, k):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD


comb_init()
x, y = [int(x) for x in input().split()]


def solve(x, y):

    if (x + y) % 3 != 0:
        return 0

    n = (2 * y - x) // 3
    m = (2 * x - y) // 3
    if n < 0 or m < 0:
        return 0

    return comb(n + m, n)


print(solve(x, y))

Solution (Python):
def main():
    X, Y = map(int, input().split())
    a = (X+Y)//3
    ans = 0
    mod = 10**9+7

    f = [1]
    for i in range(a):
        f.append(f[i]*(i+1)%mod)

    if (X+Y)%3 == 0:
        for i in range(a+1):
            x = i + 2 * (a - i)
            y = (2 * i) + (a - i)
            if X == x and Y == y:
                 ans += f[a] * pow(f[i], mod-2, mod)%mod * pow(f[a-i], mod-2, mod)%mod
                                                             
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
MAX = 1000000
MOD = 1000000007

fac = [None] * MAX
finv = [None] * MAX
inv = [None] * MAX


def comb_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        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, k):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD


comb_init()
x, y = [int(x) for x in input().split()]


def solve(x, y):

    if (x + y) % 3 != 0:
        return 0

    n = (2 * y - x) // 3
    m = (2 * x - y) // 3
    if n < 0 or m < 0:
        return 0

    return comb(n + m, n)


print(solve(x, y))

Solution (Python):
X, Y = map(int, input().split())

def mod_Combination(n, k, mod):
    def ext_gcd(a, b):
        if b == 0:
            return a, 1, 0
        else:
            d,x,y = ext_gcd(b,a%b)
            x-=(a//b)*y 
        return d,y,x
    p,q=1,1
    for i in range(n-k+1, n+1):
        p=(p*i)%mod
    for i in range(2, k+1):
        q=(q*i)%mod
    return int(p*(ext_gcd(q, mod)[1]%mod)%mod)

a = (2*X - Y)/3
b = (2*Y - X)/3

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

elif a<0 or b<0:
    ans = 0

else:
    ans = mod_Combination(int(a + b), int(b), 10**9 + 7)

print(ans)

Solution (Python):
X, Y = map(int, input().split())

def mod_Combination(n, k, mod):
    def ext_gcd(a, b):
        if b == 0:
            return a, 1, 0
        else:
            d,x,y = ext_gcd(b,a%b)
            x-=(a//b)*y 
        return d,y,x
    p,q=1,1
    for i in range(n-k+1, n+1):
        p=(p*i)%mod
    for i in range(2, k+1):
        q=(q*i)%mod
    return int(p*(ext_gcd(q, mod)[1]%mod)%mod)

a = (2*X - Y)/3
b = (2*Y - X)/3

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

elif a<0 or b<0:
    ans = 0

else:
    ans = mod_Combination(int(a + b), int(a), 10**9 + 7)

print(ans)

Solution (Python):
X, Y = map(int, input().split())

def mod_Combination(n, k, mod):
    def ext_gcd(a, b):
        if b == 0:
            return a, 1, 0
        else:
            d,x,y = ext_gcd(b,a%b)
            x-=(a//b)*y 
        return d,y,x
    p,q=1,1
    for i in range(n-k+1, n+1):
        p=(p*i)%mod
    for i in range(2, k+1):
        q=(q*i)%mod
    return int(p*(ext_gcd(q, mod)[1]%mod)%mod)

if X > Y:
    X, Y = Y, X

a = (2*X - Y)/3
b = (2*Y - X)/3

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

elif a<0 or b<0:
    ans = 0

else:
    ans = mod_Combination(int(a + b), int(a), 10**9 + 7)

print(ans)

Solution (Python):
from sys import stdin
import numpy as np
mod = 10**9+7
X,Y = [int(x) for x in stdin.readline().rstrip().split()]

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
 
def power(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1
 
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x % mod
        x *= x % mod
        n //= 2
 
    return K * x % mod
 
def div(a, b):
    return mul(a, power(b, mod-2))
 
def fact(n):
    f = 1
    for i in range(1,n+1):
        f *= i
        f %= mod
    return f

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

else:
    A = np.matrix([[2, 1],[1, 2]])
    Y = np.matrix([[X],[Y]])
    x,y = np.linalg.solve(A,Y)
    x = int(x)
    y = int(y)
    ans = fact(x+y)
    ans = div(ans,fact(x))
    ans = div(ans,fact(y))
    if x < 0 or y <0:
        print(0)
    else:
        print(ans%mod)

Solution (Python):
X, Y = map(int, input().split())

def mod_Combination(n, k, mod):
    def ext_gcd(a, b):
        if b == 0:
            return a, 1, 0
        else:
            d,x,y = ext_gcd(b,a%b)
            x-=(a//b)*y 
        return d,y,x
    p,q=1,1
    for i in range(n-k+1, n+1):
        p=(p*i)%mod
    for i in range(2, k+1):
        q=(q*i)%mod
    return int(p*(ext_gcd(q, mod)[1]%mod)%mod)

if X > Y:
    X, Y = Y, X

a = (2*X - Y)/3
b = (2*Y - X)/3

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

elif Y > 2*X:
    ans = 0

else:
    ans = mod_Combination(int(a + b), int(a), 10**9 + 7)

print(ans)

Solution (Python):
import math
X, Y = map(int, input().split())
MOD = 10 ** 9 + 7



if (2 * Y - X) % 3 != 0 or (2 * X - Y) % 3 != 0 or (2 * Y - X) // 3 < 0 or (2 * X - Y) // 3 < 0:
	print(0)
else:
	a = (2 * Y - X) // 3
	b = (2 * X - Y) // 3
	factorial = [1]
	inverse = [1]
	for i in range(1, a + b + 5):
		factorial.append(factorial[-1] * i % MOD)
		inverse.append(pow(factorial[-1], MOD - 2, MOD))
	print(factorial[a + b] * inverse[a] * inverse[b] % MOD)

Solution (Python):
import sys
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

x,y=map(int,input().split())
if (x+y)%3!=0:
  print(0)
  sys.exit()
n=(x+y)//3

mod = 10**9+7 
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 )

print(cmb(n,x-n,mod))

Solution (Python):
import sys

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

if (x + y) % 3 != 0:
  print (0)
  sys.exit(0)

mod = 10 ** 9 + 7

def inv(x):
  return pow(x, mod-2, mod)

def fac(n):
  ans = 1
  for i in range(1, n+1):
    ans = (ans * i) % mod
  return ans

a = y - (x + y) // 3
b = x - (x + y) // 3

if a < 0 or b < 0:
  print (0)
  sys.exit(0)

ans = fac(a+b)
ans = (ans * inv(fac(a))) % mod
ans = (ans * inv(fac(b))) % mod
print (ans)

Solution (Python):
X, Y = [int(i) for i in input().split(' ')]
mod = 10**9+7

x =0
y =0
x += X
y += Y
counter =[0,0]
while x > 0 or y > 0:
    if x >= y:
        x -= 2
        y -= 1
        counter[0] +=1 
    else:
        x -= 1
        y -= 2
        counter[1] +=1 
        

def nCr(n,r,mod):
    if (r<0 or r>n):
        return 0

    return (n_legular[n]*r_inverse[r]*r_inverse[n-r]) % mod

N =counter[0]+counter[1]
n_legular = [1,1]
r_inverse = [1,1] 
inverse_calc = [0,1]

for i in range(2,N+1):
    n_legular.append(n_legular[-1]*i%mod)
    inverse_calc.append(-inverse_calc[mod % i] * (mod//i)  % mod)
    r_inverse.append(r_inverse[-1]*inverse_calc[-1] % mod)
    
if (X+Y) % 3 != 0:
    print(0)
elif (X > 2*Y) or (Y > 2*X):
    print(0)
    
else:
    print(nCr(counter[0]+counter[1],counter[0],mod))

Solution (Python):
X, Y = list(map(int, input().split()))

def f(x, p):
    a = 1
    for i in range(1, x + 1):
        a *= i
        a %= p
    return a
def nCr(a, b, p):
    if a < 0 or b < 0:
        return 0
    elif a < b:
        return 0
    c = 1
    c *= f(a, p)
    c *= pow(f(b, p), p - 2, p)
    c *= pow(f(a - b, p), p - 2, p)
    return c % p

if (2*X-Y)%3 != 0 or (2*Y-X)%3 != 0 or Y > X*2 or X > Y*2:
  print(0)
else:
  m = (2*X-Y)//3
  n = (2*Y-X)//3
  print(nCr(m+n, n, 10**9+7))

Solution (Python):
from math import factorial
mod = int(1e+9 + 7)

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

X2 = 2*X - Y
Y2 = -X + 2*Y

if X2 < 0:
    print "0"
    exit()
elif Y2 < 0:
    print "0"
    exit()

if X2%3 != 0:
    print "0"
    exit()
elif Y2%3 != 0:
    print "0"
    exit()

X2 = X2/3
Y2 = Y2/3

ans = 1

n = X2+Y2
m = Y2

for i in range(n-m+1, n+1):
    ans = (ans*i)%mod


new = 1
for i in range(2, m+1):
    new = (new*i)%mod

new = pow(new, mod-2, mod)

ans = (ans*new)%mod

print ans