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):
x,y=map(int,input().split())
mod=10**9+7
if (x+y)%3:
    print(0)
    exit()
x,y=min(x,y),max(x,y)
sa=y-x
if sa>x:
    print(0)
    exit()
p=(x+y)//3

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

print(cmb(p,(p-sa)//2)%mod)

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


mod = 10**9+7 
N = 700000
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 Y > 2 * X or X > 2 * Y:
    print(0)

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

else:
    N = (X+Y) // 3
    x = X - N
    y = Y - N

    print(cmb(x+y,x,mod))

Solution (Python):
X, Y = map(int, input().split())
if (X+Y)%3 != 0:
  print(0)
  exit()
N = (X+Y)//3

MAX = 700000
MOD = 1000000007
fac = [0]*MAX
finv = [0]*MAX
inv = [0]*MAX
fac[0] = 1
fac[1] = 1
finv[0] = 1
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 COM(n, k):
  if n < 0 or k < 0 or n < k:
    return 0
  return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD
r = COM(N, X-N)
print(r)

Solution (Python):
x,y = map(int,input().split())
if (x+y) % 3 != 0:
  print(0)
elif x < 0 or y < 0:
  print(0)
elif x/y > 2 or y/x > 2:
  print(0)
else:
  n = (x+y) // 3
  m = x - n
  mod = 10**9 + 7
  def inv(x):
    y = 1
    while x != 1:
      y *= mod//x + 1
      y %= mod
      x -= mod%x
    return y
  
  ans = 1
  for i in range(m):
    ans *= (n-i)
    ans %= mod
    ans *= inv(m-i)
    ans %= mod
  
  print(ans)

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


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

NN = 10**6 
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

for i in range( 2, NN + 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 (X+Y)%3 != 0:
    print(0)
else:
    a = (X+Y)//3
    ans = cmb(a, X-a)
    print(ans)

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

m = 3*10**6 +1 
fac = [1]*m
ninv = [1]*m
finv = [1]*m
for i in range(2,m):
    fac[i] = fac[i-1]*i%mod
    ninv[i] = (-(mod//i)*ninv[mod%i])%mod
    finv[i] = finv[i-1]*ninv[i]%mod

def comb(n,k):
    return (fac[n]*finv[k]%mod)*finv[n-k]%mod

if (X+Y)%3!=0:
    ans = 0
else:
    N = (X+Y)//3
    a = X-N
    b = Y-N
    if a<0 or b<0:
        ans = 0
    else:
        ans = comb(a+b,a)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100000)

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

MOD = 10 ** 9 + 7

if x > y:
    x, y = y, x

z = y - x

if x - z < 0 or (x - z) % 3 != 0:
    print(0)
    exit()

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

def cmb(n, r):
    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 )

print(cmb(a+b, a))

Solution (Python):
[X,Y] = list(map(int,input().split()))
output = 0

Z = X+Y

def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

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

if (Z%3==0)and(Z/3<=X)and(Z/3<=Y):
    T = int(Z/3)
    S = abs(int(Z/3)-min(X,Y))
    output = combination(T,S, mod=10**9+7)

print(output)

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

def mod_inverse(a, p):
    mod = 1
    p_bin = bin(p-2)
    for i in range(len(p_bin) - 1, 1, -1):
        if i == len(p_bin) - 1:
            mul = a
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
        else:
            mul = mul ** 2
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
    return mod

def combination(n, k, p):
    a = 1
    for i in range(n, n - k, -1):
        a *= i
        a %= p
    b = 1
    for i in range(k):
        b *= (i + 1)
        b %= p
    return (a * mod_inverse(b, p)) % p

if (x + y) % 3 != 0:
    print(0)
elif x > y * 2 or y > x * 2:
    print(0)
else:
    total = (x + y) // 3
    a = x - total
    b = total - a
    c = min(a, b)
    print(combination(total, c, mod))

Solution (Python):
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
mod=10**9+7
x,y=map(int,input().split())
if (x+y)%3==0 and y>=0.5*x and y<=2*x:
    k=(x+y)//3
    h=y-k
    ans=1
    for i in range(k-h+1,k+1):
        ans*=i
        ans%=mod
    for i in range(1,h+1):
        ans*=mod_inv(i,mod)
        ans%=mod
    print(ans)
else:
    print(0)

Solution (Python):
def main():
    x,y=map(int,input().split())
    if (x+y)%3!=0:
        print(0)
        return 0
    n=(x+y)//3
    h=x-n
    w=y-n
    if h<0 or w<0:
        print(0)
        return 0
    def fact(n,mod=10**9+7):
        ans=1
        for i in range(1,n+1):ans=(ans*i)%mod
        return ans
    def inv_n(n,mod=10**9+7):return pow(n,mod-2,mod)
    def nPr(n,r,mod=10**9+7):return inv_n(fact(n-r,mod),mod)*fact(n,mod)%mod
    def nCr(n,r,mod=10**9+7):return inv_n(fact(n-r,mod)*fact(r,mod)%mod,mod)*fact(n,mod)%mod
    print(nCr(h+w,w))

main()

Solution (Python):
x, y = map(int, input().split())
if (2*x-y)%3 != 0 or (2*y-x)%3 != 0:
    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
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 = (2*x-y)//3
b = (2*y-x)//3
n = a+b
r = a
res = cmb(n,r,mod)
print(res)

Solution (Python):
import sys, math
mod = 10**9+7
X, Y = map(int, input().split())
if X>Y:
    X, Y = Y, X
if (X+Y)%3 !=0:
    print(0)
    sys.exit()
n = (X+Y)//3
d = (Y-X)
if n<d or (n-d)%2 != 0:
    print(0)
    sys.exit()
a = (n-d)//2
def make_fact(n):
    fact = [1]*(n+1)
    for i in range(1, n+1):
        fact[i] = fact[i-1]*i%mod
    return fact
fact = make_fact(n)  
def make_fact_inv(n):
    fact_inv = [1]*(n+1)
    fact_inv[n] = pow(fact[n], mod-2, mod)
    for i in range(n, 0, -1):
        fact_inv[i-1] = fact_inv[i]*i%mod
    return fact_inv
fact_inv = make_fact_inv(n)

def comb(n, k):
    return fact[n]*fact_inv[k]*fact_inv[n-k]%mod

print(comb(n, a))

Solution (Python):
class ModComb:
    def __init__(self, MAX, mod=10 ** 9 + 7):
        fac = [1, 1]
        finv = [1, 1]
        inv = [0, 1]
        for i in range(2, MAX):
            fac.append(fac[i - 1] * i % mod)
            inv.append(mod - inv[mod % i] * (mod // i) % mod)
            finv.append(finv[i - 1] * inv[i] % mod)
        self.fac, self.finv, self.mod = fac, finv, mod

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

mod = 10 ** 9 + 7
mc = ModComb(3 * 10 ** 6, mod)

X, Y = map(int, input().split())
if (2 * X - Y) % 3 != 0 or (X - 2 * Y) % 3 != 0:
    print(0)
    quit()

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

print(mc.nCk(a + b, a))

Solution (Python):
class Combinatorics:
    def __init__(self, N, mod):
        '''
        Preprocess for calculating binomial coefficients nCr (0 <= r <= n, 0 <= n <= N)
        over the finite field Z/(mod)Z.
        Input:
            N (int): maximum n
            mod (int): a prime number. The order of the field Z/(mod)Z over which nCr is calculated.
        '''
        self.mod = mod
        self.fact = {i: None for i in range(N+1)}         
        self.inverse = {i: None for i in range(1, N+1)}   
        self.fact_inverse = {i: None for i in range(N+1)} 
        
        
        self.fact[0] = self.fact[1] = 1
        self.fact_inverse[0] = self.fact_inverse[1] = 1
        self.inverse[1] = 1
        for i in range(2, N+1):
            self.fact[i] = i * self.fact[i-1] % self.mod
            q, r = divmod(self.mod, i)
            self.inverse[i] = (- (q % self.mod) * self.inverse[r]) % self.mod
            self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i-1] % self.mod
    
    def perm(self, n, r):
        '''
        Calculate nPr = n! / (n-r)! % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return (self.fact[n] * self.fact_inverse[n-r]) % self.mod
    
    def binom(self, n, r):
        '''
        Calculate nCr = n! /(r! (n-r)!) % mod
        '''
        if n < r or n < 0 or r < 0:
            return 0
        else:
            return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n-r] % self.mod) % self.mod
        
    def hom(self, n, r):
        '''
        Calculate nHr = {n+r-1}Cr % mod.
        Assign r objects to one of n classes.
        Arrangement of r circles and n-1 partitions:
            o o o | o o | | | o | | | o o | | o
        '''
        if n == 0 and r > 0:
            return 0
        if n >= 0 and r == 0:
            return 1
        return self.binom(n + r - 1, r)

MOD = 10**9 + 7
X, Y = map(int, input().split())
if (X + Y) % 3 != 0: print(0)
else:
    x, y = ((2*X - Y) // 3, (-X + 2*Y) // 3)
    com = Combinatorics(x + y, MOD)
    print(com.binom(x+y, x))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)

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

def comb(n, b):
    if b > n - b:
        b = n - b
    r = 1
    for k in range(n, n-b, -1):
        r = r * k % mod
    d = 1
    for k in range(1, b+1):
        d = d * k % mod
    return r * inv(d) % mod

def main():
    x,y = LI()
    x,y = max(x,y), min(x,y)
    if (x+y) % 3 > 0 or x > y*2:
        return 0

    c = (x+y) // 3
    xc = (x-y) + (c - (x-y)) // 2

    return comb(c,xc)


print(main())

Solution (Python):

MOD = 10 ** 9 + 7
def choose(n, r):
    num = 1
    den = 1
    for i in range(r):
        num *= n - i
        num %= MOD
    for i in range(r):
        den *= i + 1
        den %= MOD
    return num * pow(den, MOD - 2, MOD) % MOD
def solve(x, y):
    if (x + y) % 3 != 0:
        return 0
    k = (x + y) // 3
    if x < k or y < k:
        return 0
    return choose(k, min(y - k, x - k))
print(solve(*map(int, input().split())))

Solution (Python):
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]
MOD=10**9+7
MAX=10**6+2
for i in range(2, MAX):
  fac.append(fac[i - 1] * i % MOD)
  inv.append(MOD - inv[MOD % i] * (MOD // i) % MOD)
  finv.append(finv[i - 1] * inv[i] % MOD)
def COM(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
X,Y=[int(s) for s in input().split()]
if (X+Y)%3!=0:
  print(0)
else:
  A=(X+Y)//3
  X-=A
  Y-=A
  if X<0 or Y<0:
    print(0)
  else:
    print(COM(X+Y,X))

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()

def modfact(n):
    fact=[1]*(n+1)
    invfact=[1]*(n+1)
    for i in range(1,n+1):
        fact[i]=i*fact[i-1]%MOD
    invfact[n]=pow(fact[n],MOD-2,MOD)
    for i in range(n-1,-1,-1):
        invfact[i]=invfact[i+1]*(i+1)%MOD
    return fact,invfact

def resolve():
    x,y=map(int,input().split())
    if((x+y)%3):
        print(0)
        return

    z=(x+y)//3
    if(x-z<0 or y-z<0):
        print(0)
        return
    
    fact,invfact=modfact(z)
    print(fact[z]*invfact[x-z]%MOD*invfact[y-z]%MOD)
resolve()

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

mod = 10**9 + 7

def comb(n,k):
    k=min(k,n-k)
    a=1
    b=1
    for i in range(k):
        a*=n-i
        b*=i+1
        a%=mod
        b%=mod
    return (a * pow(b,mod-2,mod))%mod


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

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

from collections import defaultdict


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

class Combination(object):
	def __init__(self, N, con):
		self.fac = [0] * (N + 1)
		self.inv = [0] * (N + 1)
		self.finv = [0] * (N + 1)
		self.fac[0], self.fac[1] = 1, 1
		self.inv[1] = 1
		self.finv[0], self.finv[1] = 1, 1

		
		for i in range(2, N + 1):
			self.fac[i] = self.fac[i - 1] * i % con
			self.inv[i] = self.inv[con % i] * (con - (con // i)) % con
			self.finv[i] = self.finv[i - 1] * self.inv[i] % con

	def com(self, k, N):
		return (self.fac[N] * self.finv[k] * self.finv[N - k]) % con


def main():
	X, Y = getlist()
	N = (X + Y) // 3
	if (X + Y) % 3 != 0:
		print(0)
	elif X < N or Y < N:
		print(0)
	else:
		C = Combination(N, con)
		ans = C.com(X - N, N)
		print(ans)

	

if __name__ == '__main__':
	main()

Solution (Python):
x,y = map(int,input().split())
if (x+y)%3:
    print(0)
    exit()
c = (x+y)//3
x -= c; y -= c
if x<0 or y<0:
    print(0)
    exit()

n = x+y
mod = 10**9+7
fun = [1]*(n+1)
for i in range(1,n+1):
    fun[i] = fun[i-1]*i%mod
rev = [1]*(n+1)
rev[n] = pow(fun[n],mod-2,mod)
for i in range(n-1,0,-1):
    rev[i] = rev[i+1]*(i+1)%mod

def nCr(n,r):
    if r > n:
        return 0
    return fun[n]*rev[r]%mod*rev[n-r]%mod
    
print(nCr(x+y,x))

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

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

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

if (X+Y)%3:
    ans = 0
else:
    K = (X+Y)//3
    if X < K or Y < K:
        ans = 0
    else:
        ans = getComb(K, X-K, MOD)

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


class Combinations:
    def __init__(self, max_num, mod):
        self.mod = mod
        self.factorials = [1]
        for i in range(1, max_num + 1):
            self.factorials.append((self.factorials[-1] * i) % mod)
        self.invs = [pow(self.factorials[-1], mod - 2, mod)]
        for i in reversed(range(1, max_num + 1)):
            self.invs.append(self.invs[-1] * i % mod)
        self.invs = self.invs[::-1]

    def __call__(self, n, k):
        if n < 0 or k < 0 or k > n:
            return 0
        return self.factorials[n] * self.invs[k] * self.invs[n - k] % self.mod


MOD = 10**9 + 7

x, y = ril()
a = (2 * x - y) // 3
b = a - x + y
if 3 * a != 2 * x - y:
    print(0)
    exit()

combinations = Combinations(10**6, MOD)
print(combinations(a + b, a))

Solution (Python):
MOD = int(1e9+7)


def div(a, b):
    return a * pow(b, MOD-2, MOD) % MOD


def fact(x):
    prev = 1
    ret = 1
    for i in range(1, x+1):
        ret = (i * prev) % MOD
        prev = ret
    return ret


def main():
    X, Y = map(int, input().split())
    N = (X+Y)//3
    if (X+Y) % 3 != 0 or X < N or Y < N:
        print(0)
        return
    ans = div(div(fact(N), fact(X-N)), fact(N*2-X))
    print(ans)


if __name__ == "__main__":
    main()

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

import sys
x,y=map(int,input().split())
if (x+y)%3!=0:
  print(0)
  sys.exit()
z=(x+y)//3
mod=10**9+7

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

print(cmb(z,y-z,mod))

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


if (-X + 2*Y) % 3 or (2*X - Y) % 3 or (-X + 2*Y) < 0 or (2*X - Y) < 0:
    print(0)
else:
    a = (-X + 2*Y) // 3
    b = (2*X - Y) // 3
    P = 10**9 + 7
    fa = [1]
    for i in range(1, a + b + 100):
        fa.append(fa[-1] * i % P)    
    print(fa[a+b] * pow(fa[a]*fa[b], P-2, P) % P)

Solution (Python):

def pow(x,n):
    global p
    res=1
    prod=x
    while n>0:
        if n%2==1:
            res=(res*prod)%p
        prod=(prod*prod)%p
        n//=2
    return res


def inv(x):
    global p
    res=1
    prod=x
    e=p-2
    while e>0:
        if e%2==1:
            res=(res*prod)%p
        prod=(prod*prod)%p
        e//=2
    return res


def fact(x):
    if x<1:
        return 1
    global p
    res=1
    for i in range(2, x+1):
        res=(res*i)%p
    return res
    

def combi(n,r):
    if r<0 or r>n:
        return 0
    else:
        a=fact(n)
        b=inv(fact(r))
        c=inv(fact(n-r))
        return(((a*b)%p)*c)%p

p=10**9+7
X,Y=map(int,input().split())
if (X+Y)%3!=0 or 2*X<Y or 2*Y<X:
    ans=0
else:
    a=(2*X-Y)//3
    b=(-X+2*Y)//3
    ans=combi(a+b,a)
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

def combination_mod(n, k, mod):
    """ power_funcを用いて(nCk) mod p を求める """ 
    """ nCk = n!/((n-k)!k!)を使用 """

    from math import factorial
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    a = fact[n] 
    b = fact[k]
    c = fact[n - k]
    return (a * pow(b, mod - 2, mod) * pow(c, mod - 2, mod)) % mod
    



def solve():
    x, y = LI()
    x, y = (2 * y - x) / 3, (2 * x - y) / 3
    if x.is_integer() and y.is_integer():
        print(combination_mod(int(x + y), int(x), mod))
    else:
        print(0)
    return



if __name__ == '__main__':
    fact = [1] * (10 ** 6+1)
    for i in range(1, 10 ** 6):
        fact[i + 1] *= (fact[i] * (i+1)) % mod
    solve()

Solution (Python):
import sys
input = sys.stdin.readline

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

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

k=(X+Y)//3
X-=k
Y-=k

if X<0 or Y<0:
    print(0)
    sys.exit()

FACT=[1]
for i in range(1,2*10**6+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(2*10**6,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0

print(Combi(X+Y,X))

Solution (Python):

x, y = map(int,input().split())
mod = 10**9 + 7

l = 10**6+3

M = [1]  
m = 1
for i in range(1, l):
    m = (m * i) % mod
    M.append(m)

def inv(x, y):  
    yy = y - 2
    ans = 1
    while yy > 0:
        if yy % 2 == 1:
            ans = (ans * x) % y
        x = (x**2) % y
        yy //= 2
    return ans



MI = [0] * (l-1) +[inv(M[l-1], mod)]  
for i in range(l-2, -1, -1):
    MI[i] = MI[i+1] * (i+1) % mod

def C(x, y):  
    ans = M[x]
    ans = (ans * MI[y]) % mod
    return (ans * MI[x-y]) % mod
ans=0
for i in range(x//2+1):
    j = x-i*2
    if j >= 0 and i+2*j == y:
        
        ans = (ans + C(i+j, i))%mod
print(ans)

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

if (2 * x - y) % 3 != 0:
    print(0)
    exit()

if (2 * y - x) % 3 != 0:
    print(0)
    exit()

if 2 * x - y < 0:
    print(0)
    exit()

if 2 * y - x < 0:
    print(0)
    exit()

a = (2 * x - y) // 3
b = (2 * y - x) // 3

mod = 10**9 + 7
ans = 1
f1 = 1
f2 = 1

for i in range(1, a + b + 1):
    ans = ans * i % mod

for i in range(1, a + 1):
    f1 = f1 * i % mod

for i in range(1, b + 1):
    f2 = f2 * i % mod

ans = ans * pow(f1, mod-2, mod) % mod
ans = ans * pow(f2, mod-2, mod) % 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())

a=(2*x-y)/3
b=(2*y-x)/3

if a==int(a) and b==int(b) and a>=0 and b>=0:
    
    kosu=cmb(int(a+b), int(b))
    print(kosu%(10**9+7))
else:    
    print(0)

Solution (Python):
x,y=map(int,input().split())
mod=10**9+7
if (x+y)%3!=0:print(0);exit()
if x*2<y or y*2<x:print(0);exit()
t=(x+y)//3
f=[1]
for i in range(1,10**6+7):f.append(f[-1]*i%mod)
def comb(a,b,m):return f[a]*pow(f[b],m-2,m)*pow(f[a-b],m-2,m)%m
print(comb(t,x-t,mod))

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



X,Y = II()

if (2*X-Y)%3!=0 or (2*Y-X)%3!=0:
    print(0)
    exit()

x = (2*X-Y)//3
y = (2*Y-X)//3
if x<0 or y<0:
    print(0)
    exit()


def fact_all(n,M=mod):
    f = [1]*n
    ans = 1
    for i in range(1,n):
        ans = ans*i
        ans = ans%M
        f[i] = ans
    return f



def fact(a,M=mod):
    ans = 1
    for i in range(2,a+1):
        ans = ans*i
        ans = ans%M
    return ans



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=mod):
    x = extgcd(a,M)[0]
    return (M+x%M)%M



def nCk(n,k,M=mod,fact_list=[],inv_list=[]):
    if fact_list and inv_list:
        return (fact_list[n]*inv_list[k]*inv_list[n-k])%M
    else:
        x = fact(n,M)
        y1 = mod_inv(fact(k,M),M)
        y2 = mod_inv(fact(n-k,M),M)
        return (x*y1*y2)%M
    
print(nCk(x+y,x,mod))

Solution (Python):
x,y = map(int,input().split())
mod = 10**9 + 7
if (x+y)%3 != 0:
    print(0)
    exit()
if (2*y-x)%3 !=0 or (2*x-y)%3 != 0 or 2*y-x <0 or 2*x-y<0:
    print(0)
    exit()

a = (2*y-x)//3
b = (2*x-y)//3

def inverse(a,p):
    return pow(a,p-2,p)

def combination_modp(n, r, p): 
    r = min(n-r, r)
    result = 1
    for i in range(n, n-r, -1):
        result *= i
        result %=p
    for i in range(1, r+1):
        result *= inverse(i,p)
        result %= p
    return result

ans = combination_modp(a+b,a,mod)
print(ans)

Solution (Python):

import sys
import math
input = sys.stdin.readline
MOD = 10**9 + 7

def single_mod_nCr(n, r):
    if n < r or n < 0 or r < 0:
        return 0
    if r > n - r:
        r = n - r
    ret = 1
    for i in range(r):
        ret *= n - i
        ret *= pow(i+1, MOD-2, MOD)
        ret %= MOD
    return ret

x, y = [int(item) for item in input().split()]
if (x + y) % 3 != 0:
    print(0)
    exit()
step = (x + y) // 3
x -= step; y -= step
if x < 0 or y < 0:
    print(0)
    exit()

print(single_mod_nCr(x+y, x))

Solution (Python):
class Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

    def f(self, p, q):
        return self.fact[p + q] * self.factInv[p] * self.factInv[q] % MOD


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

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

time = (X + Y) // 3
ans = 0
comb = Combination(time + 10)

for y in range(time + 1):
    x = time - y

    if X == x * 2 + y and Y == y * 2 + x:
        ans += comb.f(x, y)
        ans %= MOD

print(ans % MOD)

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



nmax = 10**6 + 1  
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


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

if x < y:
    x, y = y, x
if x > 2 * y:
    print(0)
    exit()

n = (x+y) // 3
r = y - n
print(comb(n, r))

Solution (Python):
x,y=map(int,input().split())
MOD = 10**9+7
if (x+y)%3!=0:print(0);exit()
def inv_mod(a, p=MOD):
    def inv_mod_sub(a, p):
        if a == 1:
            return 1, 0
        else:
            d, r = p//a, p%a
            x, y = inv_mod_sub(r, a)
            return y-d*x, x
    if p < 0: p = -p
    a %= p
    return inv_mod_sub(a,p)[0] % p

def comb_mod(n, k):
    if k < 0 or k > n:
        return 0
    else:
        return f_mod[n]*f_mod_inverse[k]*f_mod_inverse[n-k] % MOD
f_mod=[1]*(10**6)
f_mod_inverse=[1]*(10**6)
for i in range(1,10**6):
  f_mod[i]=f_mod[i-1]*i%MOD
  f_mod_inverse[i]=f_mod_inverse[i-1]*inv_mod(i)%MOD
cnt=(x+y)//3
dif=abs(x-y)
ele=(cnt-dif)//2
print(comb_mod(cnt,ele))

Solution (Python):
x, y = [int(i) for i in input().split()]
if (x + y) % 3 != 0:
    print(0)
    exit()
z = (x + y) // 3
x -= z
y -= z

p = 10**9+7


def kai(x, p):
    a = 1
    for i in range(1, x + 1):
        a *= i
        a %= p
    return a


def comb(a, b, p):
    if a < 0 or b < 0:
        return 0
    elif a < b:
        return 0
    c = 1
    c *= kai(a, p)
    c *= pow(kai(b, p), p - 2, p)
    c *= pow(kai(a - b, p), p - 2, p)
    return c % p


print(comb(x+y, x, p))

Solution (Python):
def comb(n,m,p=10**9+7):
    if n < m : return 0
    if n < 0 or m < 0:return 0
    m = min(m, n-m)
    top = bot = 1
    for i in range(m):
        top = top*(n-i) % p
        bot = bot*(i+1) % p
    bot = pow(bot, p-2, p)
    return top*bot % p

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

j = 2*x-y
if j%3:
    print(0)
    exit()
j //= 3
i = x - 2*j
if i<0 or j<0:
    print(0)
    exit()

ans = comb(i+j,i)
print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    x,y = LI()
    n = x+y
    if n%3 != 0:
        print(0)
        return
    n //= 3
    ans = 1
    for i in range(max(x,y)-n):
        ans *= n-i
        ans %= mod
        ans *= pow(i+1,mod-2,mod)
        ans %= mod
    print(ans)
    return


if __name__ == "__main__":
    solve()