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
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    def main():
        X,Y=map(int,input().split())
        n=(-X+2*Y)/3
        m=(2*X-Y)/3

        
        def modpow(a, b, mod=10 ** 9 + 7):
            ret = 1
            while b > 0:
                if b % 2 == 1:
                    ret = ret * a % mod
                a = a * a % mod
                b //= 2
            return ret
        
        
        def modcomb(n, c, mod=10 ** 9 + 7):
            if c > n - c:
                c = n - c
            u, d = 1, 1
            for i in range(c):
                u = u * (n - i) % mod
                d = d * (i + 1) % mod
            return u * modpow(d, mod - 2) % mod
        
        
        """mod = 10 ** 9 + 7
        n, a, b = map(int, input().split())
        ans = (modpow(2, n) - 1) % mod
        ans = (ans-modcomb(n, a)) % mod
        ans = (ans-modcomb(n, b)) % mod
        print(ans)"""
        
        
        
        
        if n.is_integer() and m.is_integer() and n>=0 and m>=0:
            return modcomb(int(n+m),int(n))
        else: return 0
    print(main())
    
resolve()

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

SIZE=10**6+1; MOD=10**9+7 
 
SIZE += 1
inv = [0]*SIZE  
fac = [0]*SIZE  
finv = [0]*SIZE 
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD
 
def choose(n,r): 
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0
 
print(choose(X+Y,X))

Solution (Python):
MAX = 5100000

MOD = 1000000007


def read_input():
    x, y = map(int, input().split(" "))
    return x, y


class BinomialCoefficients(object):
    def __init__(self, MAX=MAX, MOD=MOD):
        self.MOD = MOD
        self.fac = [1 for _ in range(MAX)]
        self.finv = [1 for _ in range(MAX)]
        self.inv = [1 for _ in range(MAX)]
        for i in range(2, MAX):
            self.fac[i] = self.fac[i - 1] * i % MOD
            self.inv[i] = MOD - self.inv[MOD % i] * (MOD // i) % MOD
            self.finv[i] = self.finv[i - 1] * self.inv[i] % MOD

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


def ans(X, Y):
    a_3 = 2 * X - Y
    b_3 = 2 * Y - X
    if a_3 % 3 != 0 and b_3 % 3 != 0:
        return 0
    if a_3 < 0 or b_3 < 0:
        return 0
    a = a_3 // 3
    b = b_3 // 3

    c = BinomialCoefficients()
    return c.calc(a + b, a)


if __name__ == "__main__":
    X, Y = read_input()
    print(ans(X, Y))

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

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




up = (2 * x - y) // 3
left = (2 * y - x) // 3

if up < 0 or left < 0:
    print(0)
    exit()


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
res = 1
for i in range(1, left + up + 1):
    res = res * i % mod
for i in range(1, up + 1):
    res = res * mod_inv(i, mod) % mod
for i in range(1, left + 1):
    res = res * mod_inv(i, mod) % mod
print(res % mod)

Solution (Python):
def com(n,r,mod):
    k=min(r,n-r)
    C=1
    for i in range(1,k+1):
        C=(C*(n+1-i)*pow(i,mod-2,mod))%mod
    return C

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

m = 2 * x - y
n = 2 * y - x
if m >= 0 and n >= 0 and m % 3 == 0 and n % 3 == 0:
    print(com((n + m) // 3, n // 3, mod))
else:print(0)

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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):
        if n < r:
            return 0
        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

    def comb_with_repetition(self, n, r):
        
        return self.__call__(n + r - 1, r)


def main():
    X, Y = map(int, input().split())
    if (X + Y) % 3 != 0:
        print(0)
        return
    else:
        if Y > 2 * X:
            print(0)
            return
        if 2 * Y < X:
            print(0)
            return

        comb = Combination(n_max=10 ** 6 + 1)
        n = (X + Y) // 3
        r = X - n
        ans = comb(n, r)

        print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math
x,y = map(int,input().split())
mod = pow(10,9)+7
if (2*x-y)% 3 != 0 or (2*y-x)% 3 != 0 or ((2 * y - x) / 3 < 0) or ((2 * x - y) / 3 < 0):
	print(0)
else:
	n = int((2 * y - x) / 3)
	m = int((2 * x - y) / 3)
	P = [1]*(n + m + 1)
	for i in range(1, n+m+1):
		P[i] = P[i-1]*i
		P[i] %= mod
	print((P[n+m]*pow(P[n],mod-2,mod)*pow(P[m],mod-2,mod)) % mod)

Solution (Python):
x, y= map(int, input().split())
mod=10**9+7
import math
a=(2*y-x)//3
b=(2*x-y)//3

if (2*x-y)%3!=0 or (2*y-x)%3!=0 or a<0 or b<0:
	print(0)

else:
	L=[1]*(a+b+1)
	for i in range(1,a+b+1):
		L[i]=L[i-1]*i
		L[i]=L[i]%mod
	p=pow(L[a], mod-2, mod)
	q=pow(L[b], mod-2, mod)
	ans=(L[a+b]*p*q)%mod
	print(ans)

Solution (Python):

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

a, s = divmod(2*X - Y, 3)
b, t = divmod(-X + 2*Y, 3)

mod = 10 ** 9 + 7
N = 10 ** 6
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]

def cmb(n, r, mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fac[n] * ( finv[r] * finv[n-r] % mod ) % mod
 
for i in range(2, N + 1):
  fac.append(( fac[-1] * i ) % mod)
  inv.append(mod - ( inv[mod % i] * (mod // i)) % mod)
  finv.append( finv[-1] * inv[-1]  % mod)

if s != 0 or t !=0:
  print(0)
else:
  print(cmb(a+b, a, mod))

Solution (Python):
x,y = map(int,input().split())
if (x+y)%3 != 0:
    print(0)
    quit()
elif x < (x+y)//3 or y < (x+y)//3:
    print(0)
    quit()
else:
    n = (x+y)//3
    r = x-n

mod = 10**9+7
def cmb(n,r,mod):
    r = min(r, n-r)
    return (fact[n] * factinv[r] * factinv[n-r]) % mod
def makefact(n,mod):
    fact = [1,1]
    factinv = [1,1]
    inv = [0,1]
    for i in range(2,n+1):
        fact.append((fact[-1]*i) % mod)
        inv.append(-inv[mod%i]*(mod//i)%mod)
        factinv.append((factinv[-1]*inv[-1])%mod)
    return fact,factinv
fact,factinv = makefact(n,mod)

print(cmb(n,r,mod))

Solution (Python):
import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline
    x,y=map(int, readline().rstrip().split())
    z=2*x-y
    if z%3 != 0 or z < 0:
        print(0)
        return
    b=z//3
    a=x-2*b
    if a < 0:
        print(0)
        return
    mod=10**9+7
    n=a+b
    r=min(a,b)

    ans=1

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

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
import math

x, y = map(int, input().split())
mod = 10 ** 9 + 7
a = (2 * y - x) / 3
b = (2 * x - y) / 3

if (not a.is_integer()) or (not b.is_integer()) or \
    (a < 0) or (b < 0):
    print(0)
else:
    a, b = int(a), int(b)
    I = [1] * (a + b + 1)
    for n in range(1, a+b+1):
        I[n] = I[n-1] * n
        I[n] %= mod
    i, j, k = I[a+b], I[a], I[b]
    j = pow(j, mod-2, mod)
    k = pow(k, mod-2, mod)

    ans = i * j * k
    ans %= mod
    print(ans)

Solution (Python):
X, Y = map(int, input().split())
mod = 10**9+7
b = (2*Y-X)//3
a = Y - 2*b
max_num = a + b + 5
facts = [1 for i in range(max_num)]
invs = [1 for i in range(max_num)]
 
def inv(x):
    return pow(x,mod-2,mod)
 
for i in range(1,max_num):
    facts[i] = facts[i-1]*i%mod
    invs[i] = inv(facts[i])
 
def nCk(n,k):
    try:
        return facts[n]*invs[k] * invs[n-k] % mod
    except IndexError:
        print(n, k, len(facts), len(invs))
if (X+Y)%3 == 0 and a>=0 and b >=0:
    print(nCk(a+b, a))
else:
    print(0)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


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):
        if n < r:
            return 0
        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


def main():
    X, Y = map(int, input().split())
    if (X + Y) % 3 != 0:
        print(0)
        return
    a = (X + Y) // 3
    two = X - a
    if two < 0:
        print(0)
        return
    if two == 0:
        print(1)
        return
    cmb = Combination(a + 1)
    print(cmb(a, two))


if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


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

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


def mod_com(n, r):
    MOD = 1000000007
    r = min(r, n - r)
    numer = denom = 1
    for i in range(1, r + 1):
        numer = numer * (n - r + i) % MOD
        denom = denom * i % MOD
    return numer * pow(denom, MOD - 2, MOD) % MOD


print(mod_com(n, x))

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


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

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


def mod_com(n, r):
    MOD = 1000000007
    r = min(r, n - r)
    numer = denom = 1
    for i in range(1, r + 1):
        numer = numer * (n - r + i) % MOD
        denom = denom * i % MOD
    return numer * pow(denom, MOD - 2, MOD) % MOD


print(mod_com(n, x))

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


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

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


def mod_com(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    if k > n - k:
        k = n - k
    if k == 0:
        return 1
    ans = 1
    for i in range(n - k + 1, n + 1):
        ans = ans * i % MOD

    inv = [0] * (k + 1)
    inv[1] = 1
    for i in range(2, k + 1):
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        ans = ans * inv[i] % MOD

    return ans


print(mod_com(n, x))

Solution (Python):
def com(x,y,mod):
    bunb = 1
    buns = 1
    for i in range(y):
        buns = buns * (x-i) % mod
        bunb = bunb * (i+1) % mod
 
    bunb_inv = pow(bunb,mod-2,mod)
    d = (buns * bunb_inv) % mod
    return (buns*pow(bunb,mod-2,mod))%mod

x,y = map(int,input().split())
setcou = 0
katacou = 0
while True:
    if x * 2 == y or y * 2 == x:
        katacou = min(x,y)
        break
    elif x < 3 or y < 3:
        print(0)
        exit()
    elif x >= 3 and y >= 3:
        setcou += 1
        x -= 3
        y -= 3

print(com(setcou*2+katacou,setcou,10**9+7))

Solution (Python):
def fact(n, k, mod):
    res = 1
    for i in range(k):
        res = res * (n - i) % mod
    return res

def c(x, y, mod):
    y = min(x, x - y)
    return (fact(x, y, mod) * pow(fact(y, y, mod), mod - 2 , mod)) % mod

P = 10**9 + 7
X, Y = map(int, input().split())
if (X + Y) % 3 > 0:
    print(0)
    exit()
n = (X + Y) // 3
x = X - n
y = Y - n
if 0 > x or 0 > y:
    print(0)
    exit()
print(c(x + y, x, P))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from math import factorial 
def main():
    x, y = map(int, readline().split())
    if ((2*y-x)%3) or ((2*x-y)%3):
        print(0)
        return
    a = (2*y-x)//3
    b = (2*x-y)//3
    if a < 0:
        print(0)
        return
    if b < 0:
        print(0)
        return
    P = int(1e9)+7
    def c(n, m):
        ch = 1
        for i in range(m):
            ch *= (n-i)
            ch %= P
        b = factorial(m) 
        b = pow(b, P-2, P)
        return ch*b%P
    print(c(a+b, a)) 
if __name__ == '__main__':
    main()

Solution (Python):

def power(x, y):
    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y//2)**2 % mod
    else            : return power(x, y//2)**2 * x % mod

def div(a, b):
    return a*power(b, mod-2)%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:
    w1=(2*y-x)//3
    h1=(2*x-y)//3

    mod=10**9+7

    ic=1
    ih=1
    for i in range(h1):
        ic=ic*(w1+1+i)%mod
        ih=ih*(i+1)%mod

    ic=div(ic,ih)

    print(ic)

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

N = 10**6+1
def pow_mod(x, n):
    if n == 0:
        return 1
    elif n % 2 == 0:
        return pow_mod(x * x % MOD, n // 2) % MOD
    else:
        return x * pow_mod(x * x % MOD, n // 2) % MOD

fact = [1]
for i in range(1,N+1):
    fact.append( (fact[i-1] * i) % MOD )

inv = [1] * (N+1)
inv[N] = pow_mod(fact[N], MOD-2)
for i in range(N, 1, -1):
    inv[i-1] = inv[i] * (i) % MOD

def cmb_mod(n,r):
    return fact[n] * inv[r] * inv[n-r] % MOD


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

Solution (Python):
"""
組み合わせ, nCrのmod

(i+1, j+2)で動く回数をm回、(i+2, j+1)で動く回数をn回とすると
m + 2n = x
2m + n = y
の方程式の解として、m,nが0以上の整数で求められる必要がある

m,nが0以上の整数で求められなければそのような行き方はない
求められれば、全部でm+n回の動きの中でどのm回で(i+1, j+2)の動きをするか（ないし、どのn回で(i+2, j+1)の動きをするか）が行き方の数なので、m+nCmを求めれば良い
"""
x,y = map(int, input().split())

mod = 10**9+7

def choose(n,r):
    p,q = 1,1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(i+1)%mod
    return p * pow(q,mod-2,mod) % mod 

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



if n >= 0 and m >= 0 and not n%1 and not m%1:
    ans = choose(int(m+n), int(m))
else:
    ans = 0
print(ans)

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

mod = 10**9+7

def choose(n,r):
    p,q = 1,1
    for i in range(r):
        p = p*(n-i)%mod
        q = q*(i+1)%mod
    return p * pow(q,mod-2,mod) % mod 

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

if n >= 0 and m >= 0 and int(n)*10 == n*10 and int(m)*10 == m*10:
    ans = choose(int(m+n), int(m))
else:
    ans = 0
print(ans)

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

r = (X+Y)//3

from functools import reduce
mod = 10**9+7

if (X+Y)%3==0 and X<= 2*Y and Y<= 2*X :
    r = (X+Y)//3
    l = min
    if r != X and r != Y:
        num = reduce(lambda x,y:x*y%mod,[k+1 for k in range(r)])
        p = reduce(lambda x,y:x*y%mod,[k+1 for k in range(X-r)])
        q = reduce(lambda x,y:x*y%mod,[k+1 for k in range(2*r-X)])
        ans = num*pow(p,mod-2,mod)*pow(q,mod-2,mod)%mod
        print(ans%mod)   
    else:
        print(1)
else:
    print(0)

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


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

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

MOD = 1000000007
COM_MAX = n

fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2, COM_MAX + 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


def mod_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


print(mod_com(n, x))

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


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

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

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

MOD = 1000000007
COM_MAX = n

fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2, COM_MAX + 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


def mod_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


print(mod_com(n, x))

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

def pow_k(xx, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K = (K*xx)%di
        xx = (xx*xx)%di
        n //= 2

    return (K * xx)%di

if (2*y-x)%3>0 and (2*x-y)%3>0:
    print(0)
else:
    a = (2*y-x)//3
    b = (2*x-y)//3
    if a<0 or b<0:
        print(0)
    else:
        lis = [i for i in range(a+b+1)]
        lis[0]=1
        for i in range(1,a+b+1):
            lis[i] = (lis[i-1]*lis[i])%di
        lisr = [pow_k(i,di-2) for i in range(max(a,b)+1)]
        lisr[0]=1
        
        for i in range(1,max(a,b)+1):
            lisr[i] = (lisr[i-1]*lisr[i])%di
        tmp = (lis[a+b]*lisr[a])%di
        print((tmp*lisr[b])%di)

Solution (Python):

class ModOperator:
    def __init__(self, MOD: int = 10 ** 9 + 7):
        self.MOD = MOD

    
    def mod_pow(self, x: int, n: int):
        bi = str(format(n, "b"))
        res = 1
        a = x
        for i in range(len(bi)):
            if n >> i & 1:
                res = (res * a) % self.MOD
            a = (a * a) % self.MOD
        return res
    
    
    def mod_fact(self, x: int):
        res = 1
        for i in range(1, x + 1):
            res = res * i % self.MOD
        return res % self.MOD

    
    def mod_partial_fact(self, x: int, y: int):
        res = 1
        for i in range(x - y + 1, x + 1):
            res = res * i % self.MOD
        return res % self.MOD

    
    def mod_inv(self, x: int):
        return self.mod_pow(x, self.MOD - 2)
    
    
    def mod_comb(self, n: int, k: int):
        numerator = self.mod_partial_fact(n, k)
        denomintator = self.mod_fact(k)
        return numerator * self.mod_inv(denomintator) % self.MOD


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

    p = (X + Y) // 3
    q = (X + Y) % 3
    if q == 0 and p <= X <= 2 * p and p <= Y <= 2 * p:
        op = ModOperator()
        res = op.mod_comb(p, X - p)
    else:
        res = 0
    print(res)

if __name__ == '__main__':
    main()

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


'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

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

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

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

def comb(n, r):
    
    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)
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

print(comb(n+m,n))

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


'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

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

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

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

def comb(n, r):
    
    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)
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

print(comb(n+m,n))

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

def ncr(n, r):
    r = min(r, n-r)
    x, y = 1, 1
    for i in range(r):
        x = (x * (n-i)) % mod
        y = (y * (i+1)) % mod
    
    return x * pow(y, mod-2, mod) % mod


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


if 2*Y - X < 0 or 2*X - Y < 0 or (2*Y - X) % 3 != 0 or (2*X - Y) % 3 != 0:
    print("0")
else:
    M = (2*Y - X) // 3
    N = (2*X - Y) // 3
    print(ncr(M+N,N))

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

class Combination:
    def __init__(self, n):
        self.facts = [1 for i in range(n+1)]
        self.invs = [1 for i in range(n+1)]
        
        for i in range(1, n+1):
            self.facts[i] = self.facts[i-1] * i % mod
            self.invs[i] = pow(self.facts[i], mod-2, mod)

    def ncr(self, n, r):
        return self.facts[n] * self.invs[r] * self.invs[n-r] % mod

    def nhr(self, n, r): 
        return self.ncr(n+r-1, n-1)

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

comb = Combination(max(X, Y))

if 2*Y - X < 0 or 2*X - Y < 0 or (2*Y - X) % 3 != 0 or (2*X - Y) % 3 != 0:
    print("0")
else:
    M = (2*Y - X) // 3
    N = (2*X - Y) // 3
    print(comb.ncr(M+N,M))

Solution (Python):












def nCr(n, r, mod):
    if n < 0 or r < 0:
        return 0
    r = min(r, n - r)
    x = y = 1
    for i in range(r):
        x = (x * (n - i)) % mod
        y = (y * (i + 1)) % mod

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


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

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

a = (2 * X - Y) // 3
b = (2 * Y - X) // 3
if a < 0 or b < 0:
    print(0)
else:
    print(nCr(a + b, a, 10**9 + 7))

Solution (Python):
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 6  
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

X,Y = map(int,input().split())
if (X+Y)%3 == 0:
  a = (X+Y)//3
  X -= a
  Y -= a
  if X >= 0 and Y >= 0:
    n = X+Y
    r = X
    print(cmb(n, r, p))
    exit()

print(0)

Solution (Python):
P = 10**9 + 7
X, Y = map(int, input().split())
if (X + Y) % 3 > 0:
    print(0)
    exit()
n = (X + Y) // 3
x = X - n
y = Y - n
if 0 > x or 0 > y:
    print(0)
    exit()
fact = [0] * (x + y + 1)
inv = [0] * (x + y + 1)
fact_inv = [0] * (x + y + 1)
fact[0], fact[1] = 1, 1
inv[0], inv[1] = 0, 1
fact_inv[0], fact_inv[1] = 1, 1
for i in range(2, x + y + 1):
    fact[i] = (fact[i - 1] * i) % P
print((fact[x + y] * pow(fact[x], P - 2, P) * pow(fact[y], P - 2, P)) % P)

Solution (Python):
import math

P = 10**9 + 7
X, Y = map(int, input().split())
if (X + Y) % 3 > 0:
    print(0)
    exit()
n = (X + Y) // 3
x = X - n
y = Y - n
if 0 > x or 0 > y:
    print(0)
    exit()
fact = [0] * (x + y + 1)
inv = [0] * (x + y + 1)
fact_inv = [0] * (x + y + 1)
fact[0], fact[1] = 1, 1
inv[0], inv[1] = 0, 1
fact_inv[0], fact_inv[1] = 1, 1
for i in range(2, x + y + 1):
    fact[i] = (fact[i - 1] * i) % P
    inv[i] = (-inv[P % i] * (P // i)) % P
    fact_inv[i] = (fact_inv[i - 1] * inv[i]) % P
print((fact[x + y] * fact_inv[x] * fact_inv[y]) % P)

Solution (Python):
X, Y = map(int, input().split())
n, mod = divmod(X + Y, 3)


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 mod != 0 or not (X <= 2 * Y and Y <= 2 * X):
    print(0)
else:
    r = Y - n
    print(combination(n, r))

Solution (Python):
X, Y = map(int, input().split())
n, mod = divmod(X + Y, 3)


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 mod != 0 or not (X <= 2 * Y and Y <= 2 * X):
    print(0)
else:
    r = Y - n
    print(combination(n, r))

Solution (Python):
x,y = map(int,input().split())
s = x+y
if s%3 !=0:
    print(0)
    exit()
kai = s // 3
p = x - kai
q = y - kai

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(kai,p,mod)

print(a)

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

if (X + Y) % 3 != 0 or X > 2*Y or Y > 2*X:
    print(0)
else:
    n2x = (2*X - Y) // 3
    n1x = (2*Y - X) // 3
    n = n2x + n1x

    
    def combs(n,n2x,mod):
        facs = [1] * (n+1)
        
        nfac = 1
        for i in range(1, n+1):
            nfac = nfac * i % mod
            facs[i] = nfac
            
        return nfac * (pow(facs[n2x], mod-2, mod) * pow(facs[n-n2x], mod-2, mod)) % mod

    print(combs(n, n2x, mod))

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

if (X + Y) % 3 != 0 or X > 2*Y or Y > 2*X:
    print(0)
else:
    n2x = (2*X - Y) // 3
    n1x = (2*Y - X) // 3
    n = n2x + n1x

    
    def combs(n,n2x):
        invs = [1] * (n+1)
        nfac = 1
        for i in range(1, n+1):
            nfac = nfac * i % mod
            invs[i] = pow(nfac, mod-2, mod)
        return nfac * invs[n2x] * invs[n-n2x] % mod

    print(combs(n, n2x))

Solution (Python):
MOD = 1000000007

X,Y = map(int,input().split())
x,y = (2*X-Y)//3,(2*Y-X)//3
if (2*X-Y)%3 != 0 or (2*Y-X)%3 != 0 or x < 0 or y < 0 or(x==0 and y==0):
    print(0)
    exit()
ans =1
for i in range(x+y,0,-1):
    ans = ((ans%MOD)*i)%MOD
for i in range(x,0,-1):
    ans = ((ans%MOD)*pow(i,MOD-2,MOD))%MOD
for i in range(y,0,-1):
    ans = ((ans%MOD)*pow(i,MOD-2,MOD))%MOD

print(ans)

Solution (Python):


import math

mod = 10**9+7

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















def comb(n, r):
    X = Y = 1
    if r < 0 or n < r:
        return 0
    if r == 0:
        return 1
    if n-r < r:
        r = n-r
    for i in range(1, r+1):
        Y = Y*i % mod
        X = X*(n-i+1) % mod
    Y = pow(Y, mod-2, mod)
    return X*Y


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

num = min(x, y)-(x+y)//3
if num < 0:
    print(0)
    exit()


print(comb((x+y)//3, num) % mod)

Solution (Python):
def modinv(a,m):
    b, u, v = m, 1, 0
    while b:
        t = a//b
        a -= t*b
        a,b = b,a
        u -= t * v
        u,v = v,u
    u %= m
    return u


N_MAX = 10**6
MOD = 10**9 + 7
fac, finv, inv = [0]*N_MAX, [0]*N_MAX, [0]*N_MAX
def com_init():
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, N_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 < 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 = map(int, input().split()) 
    com_init()

    a3 = -x+2*y
    b3 = 2*x-y

    if a3%3 != 0 or b3%3 != 0:
        print(0) 
        return

    a = a3//3
    b = b3//3

    print(com(a+b, min(a,b)))

if __name__ == "__main__":
    main()

Solution (Python):
def modinv(a,m):
    b, u, v = m, 1, 0
    while b:
        t = a//b
        a -= t*b
        a,b = b,a
        u -= t * v
        u,v = v,u
    u %= m
    return u


N_MAX = 10**6
MOD = 10**9 + 7
fac, finv, inv = [0]*N_MAX, [0]*N_MAX, [0]*N_MAX
def com_init():
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, N_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 < 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 = map(int, input().split()) 
    com_init()

    a3 = -x+2*y
    b3 = 2*x-y

    if a3%3 != 0 or b3%3 != 0:
        print(0) 
        return

    a = a3//3
    b = b3//3

    print(com(a+b, min(a,b)))

if __name__ == "__main__":
    main()