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 numpy as np
import math

def cmb(n, r, mod):
    n_1 = 1
    n_2 = 1

    r = min(r, n-r)
    for i in range(1, r+1):
        n_1 = n_1 * i % mod
        n_2 = n_2 * (n - i + 1) % mod

    return (n_2 * pow(n_1, mod - 2, mod))%mod

mod = 10**9+7

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

x = (2*Y - X) / 3
y = (2*X - Y) / 3


if x.is_integer() == False or y.is_integer() == False or x < 0 or y < 0:
    print(0)
else:
    print(cmb(int(x+y), int(x), mod))

Solution (Python):
X,Y=map(int,input().split())
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
if (X+Y)%3!=0:
  print(0)
else:
  n=2*Y-X
  m=2*X-Y
  mod=10**9+7
  if n<0 or m<0:
    print(0)
  else:
    n=int(n/3)
    m=int(m/3)
    print(cmb(n+m,m)%mod)

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
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 (X+Y)%3!=0:
  print(0)
else:
  n=2*Y-X
  m=2*X-Y
  if n<0 or m<0:
    print(0)
  else:
    n=int(n/3)
    m=int(m/3)
    print(cmb(n+m,m,mod))

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

if (x+y)%3 != 0:
  print(0)
  exit()
if 2*x < y:
  print(0)
  exit()
if 2*y < x:
  print(0)
  exit()
  
mod = 10**9+7
n = (2*x-y)//3
m = (2*y-x)//3

def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod) 
  
def comb(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

print(comb(n+m,n,mod))

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

if (x+y)%3 != 0:
  print(0)
  exit()
if 2*x < y:
  print(0)
  exit()
if 2*y < x:
  print(0)
  exit()
  
mod = 10**9+7
n = (2*x-y)//3
m = (2*y-x)//3

def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [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)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

fact, fact_inv = make_array_for_comb(n+m,mod)

print(comb(n+m,n,mod))

Solution (Python):
x,y = map(int,input().split())
s = (2 * y -x) / 3
t = (2 * x -y) / 3
if s.is_integer() and t.is_integer() and s >= 0 and t >= 0:
    def comb(n, r, mod=10 ** 9 + 7):
        a = 1
        b = 1
        r = min(r, n - r)
        for i in range(r):
            a = a * (n - i) % mod
            b = b * (i + 1) % mod
        return a * pow(b, mod - 2, mod) % mod
    s = int(s)
    t = int(t)
    print(comb(s + t, t, mod=10 ** 9 + 7))
else:
    print(0)

Solution (Python):
x,y = map(int,input().split())
s = (2 * y -x) / 3
t = (2 * x -y) / 3
if s.is_integer() and t.is_integer() and s >= 0 and t >= 0:
    def comb(n, r, mod=10 ** 9 + 7):
        a = 1
        b = 1
        r = min(r, n - r)
        for i in range(r):
            a = a * (n - i) % mod
            b = b * (i + 1) % mod
        return a * pow(b, mod - 2, mod) % mod
    s = int(s)
    t = int(t)
    print(comb(s + t, t, mod=10 ** 9 + 7))
else:
    print(0)

Solution (Python):
def ext_euler(a, b):
    
    assert a > b
    if b == 0:
        return 1, 0, a
    y, x, v = ext_euler(b, a % b)
    y -= (a // b) * x
    return x, y, v


def mod_inv(a, mod):
    if a > mod:
        x, _, _ = ext_euler(a, mod)
    else:
        _, x, _ = ext_euler(mod, a)
    return x % mod


def mod_comb(n, k, mod):
    if k > n // 2:
        return mod_comb(n, n - k, mod)
    c = 1
    for i in range(k):
        c *= (n - i)
        c *= mod_inv(k - i, mod)
        c %= mod
    return c


def main():
    MOD = 10**9 + 7
    X, Y = list(map(int, input().split(' ')))
    if (2 * X - Y) % 3 != 0 or (- X + 2 * Y) % 3 != 0:
        print(0)
        exit(0)
    a = (2 * X - Y) // 3
    b = (- X + 2 * Y) // 3
    if a < 0 or b < 0:
        print(0)
        exit(0)
    print(mod_comb(a + b, b, MOD))


if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *
































def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	X,Y = inputMap()

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

	turn = (X+Y) // 3

	X -= turn
	Y -= turn

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

	print(combination(X+Y,X))





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


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

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)


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



def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

if __name__ == "__main__":
	main()

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *
































def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	X,Y = inputMap()

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

	turn = (X+Y) // 3

	X -= turn
	Y -= turn

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

	print(combination(X+Y,Y))





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


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

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)


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



def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

if __name__ == "__main__":
	main()

Solution (Python):

'''
A.ダメなケースを除外できるか、B.組合せの数を問題に応じて求められるか
階乗や逆元はfor文で求める。
ダメなケースを除外した上で、最終X,Y に行くには、二つの選択肢
(i+1,j+2),(i+2,j+1)の組合せの数を求める。
組合せの数を求める時は、階乗と逆元を使う。
逆元は一通り、全て求めておいて、配列invsに格納して、後で使う。
'''
def prepare(n, MOD):
    f = 1
    for m in range(1, n + 1):
        f = f * m % MOD
    fn = f

    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv = inv * m % MOD
        invs[m - 1] = inv
        
    return fn, invs

def solve(x, y):
    d, m = divmod(x + y, 3)
    if m != 0 or x < d or y < d:
    
        return 0
    
    c = abs(d - x)
 
    MOD = 10 ** 9 + 7
    f, invs = prepare(d, MOD)
    return f * invs[c] * invs[d - c] % MOD
  
  
  
x, y = map(int, input().split())
print(solve(x, y))

Solution (Python):
def cmb(n, k, mod, fac, ifac):
    """
    nCkを計算する
    """
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod


def make_tables(mod, n):
    """
    階乗テーブル、逆元の階乗テーブルを作成する
    """
    fac = [1, 1] 
    ifac = [1, 1] 
    inverse = [0, 1] 
    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac
mod = 10**9+7
x,y = map(int,input().split())
if (x+y) % 3 != 0:
    res = 0
else:
    m = (2 * x - y) // 3
    n = (2 * y - x) // 3
    if m < 0 or n < 0:
        res = 0
    else:
        total = n+m
        k = n
        fac,ifac = make_tables(mod, total)
        res = cmb(total, k, mod, fac, ifac)
print(res)

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

def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m

def combination(n,r,q):
    if r==0:
        return 1

    return ((n*combination(n-1,r-1,q)%q)*modinv(r,q))%q

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

def main():
    i=_input()
    x,y=i[0],i[1]
    if 2*y>=x and 2*x>=y and (2*y-x)%3==0 and (2*x-y)%3==0:
        n=(2*y-x)//3
        m=(2*x-y)//3
        print(int(combination(n+m,min(m,n),1000000007)))
    else:
        print(0)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
 
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0
 
def modinv(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m
 
def combination(n,r,q):
    if r==0:
        return 1
 
    return ((n*combination(n-1,r-1,q)%q)*modinv(r,q))%q
 
def _input():
    return list(map(int, input().split()))
 
def main():
    i=_input()
    x,y=i[0],i[1]
    if 2*y>=x and 2*x>=y and (2*y-x)%3==0 and (2*x-y)%3==0:
        n=(2*y-x)//3
        m=(2*x-y)//3
        print(int(combination(n+m,min(m,n),1000000007)))
    else:
        print(0)
 
if __name__ == "__main__":
    main()

Solution (Python):
x,y=map(int,input().split())
INF=1000000007
if (x+y)%3!=0:
    print(0)
    exit(0)
if x>2*y or y>x*2:
    print(0)
    exit(0)
times_sum=(x+y)//3
x-=times_sum;y-=times_sum
def factorial_cor(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
        fact %= INF
    return fact
a=factorial_cor(x+y)
b=factorial_cor(y)
c=factorial_cor(x)
def modinv(a):
    b=INF;u=1;v=0
    while b:
        t=a//b
        a-=t*b
        a,b=b,a
        u-=t*v
        u,v=v,u
    u%=INF
    if u<0:
        u+=INF
    return u
b=modinv(b)
c=modinv(c)

print(a*b*c%INF)

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

def powmod(a,p):
  if p==0:
    return 1
  else:
    pow2=powmod(a,p//2)
    if p%2==0:
      return (pow2**2)%MOD
    else:
      return (a*pow2**2)%MOD
def invmod(a):
  return powmod(a,MOD-2)
def comb_mod(n,r):
  r=min(r,n-r)
  nPr=1
  fact_r=1
  for i in range(r):
    nPr*=n-i
    nPr%=MOD
    fact_r*=r-i
    fact_r%=MOD  
  return (nPr*invmod(fact_r))%MOD

if (X+Y)%3!=0:
  print(0)
else:
  u=(2*X-Y)//3
  v=(-X+2*Y)//3
  if u>=0 and v>=0:    
    
    print(comb_mod(u+v,v))
  else:
    print(0)

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

def powmod(a,p):
  if p==0:
    return 1
  elif p==1:
    return a
  else:
    pow2=powmod(a,p//2)
    if p%2==0:
      return (pow2**2)%MOD
    else:
      return (a*pow2**2)%MOD
def invmod(a):
  return powmod(a,MOD-2)
def comb_mod(n,r):
  nPr=1
  fact_r=1
  for i in range(r):
    nPr*=n-i
    nPr%=MOD
    fact_r*=r-i
    fact_r%=MOD  
  return (nPr*invmod(fact_r))%MOD

if (X+Y)%3!=0:
  print(0)
else:
  u=(2*X-Y)//3
  v=(-X+2*Y)//3
  if u>=0 and v>=0:    
    
    print(comb_mod(u+v,v))
  else:
    print(0)

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

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



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

if x==0 and y==0:
    print(0)
    exit()


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


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

if n==0 and m==1:
    print(1)
    exit()
if n==1 and m==0:
    print(1)
    exit()
    





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)



print(cmb(n+m, m, 10**9+7))

Solution (Python):

import numpy as np
from collections import deque
import collections

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


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

ans = 1
for i in range(1, a+b+1):
    ans = (ans * i) % mod
a_func = 1
for i in range(1, a+1):
    a_func = (a_func * i) % mod
inv_a_func = pow(a_func, mod-2, mod)

b_func = 1
for i in range(1, b+1):
    b_func = (b_func * i) % mod
inv_b_func = pow(b_func, mod-2, mod)

ans = (ans * inv_a_func) % mod
ans = (ans * inv_b_func) % mod
print(ans)

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


def calc_comb(n, k, mod):
    '''
        n が大きい場合に使う
    '''
    a = 1
    b = 1
    for i in range(1,k+1):
        a *= i
        a %= mod
        b *= n
        n -= 1
        b %= mod
    return b * pow(a, mod-2, mod) % mod

if (X+Y) % 3 != 0:
    print(0)
else:
    total_move = (X+Y) // 3
    
    right_move =  min(X,Y) - total_move
    if right_move >= 0:
        print(calc_comb(total_move, right_move, MOD))
    else:
        print(0)

Solution (Python):

F = 10**6+5
mod = 10**9+7
fact = [1]*F
inv = [1]*F
for i in range(2,F):
    fact[i]=(fact[i-1]*i)%mod
inv[F-1]=pow(fact[F-1],mod-2,mod)
for i in range(F-2,1,-1):
    inv[i] = (inv[i+1]*(i+1))%mod

x,y=map(int,input().split())
t=(x+y)//3
if (x+y)%3!=0 or x<t or y<t:
    print(0)
else:
    print(fact[t]*inv[x-t]*inv[2*t-x]%mod)

Solution (Python):
class Combination:
    def __init__(self, n, mod):
        self.mod = mod

        
        l = [1]
        num = 1
        for i in range(1, n+1):
            num *= i
            num %= mod
            l.append(num)
        self.fac = l.copy()

        
        num = pow(num, mod-2, mod)
        l = [1 for i in range(n+1)]
        l[n] = num
        for i in range(n-1, 0, -1):
            num *= i + 1
            num %= mod
            l[i] = num
        self.rec = l.copy()

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

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

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

n = (x + y) // 3
r = x - n
c = Combination(n, mod)
print(c.comb(n, r))

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

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

Solution (Python):
import sys
import math







INF = sys.maxsize
MOD = 10 ** 9 + 7





class Combination:
    def __init__(self, mod):
        self.mod = mod
        self.fact = [1] * (2 * 10 ** 6 + 1)
        for i in range(1, 2 * 10 ** 6 + 1):
            self.fact[i] = i * self.fact[i - 1] % self.mod

    def nCr(self, n, k):
        a = self.fact[n] % self.mod
        b = (self.fact[k] * self.fact[n - k]) % self.mod
        c = pow(b, self.mod - 2, self.mod)
        return a * c % self.mod


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

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

yoko = (2 * x - y) // 3
tate = (2 * y - x) // 3

if yoko < 0 or tate < 0:
    print(0)
    exit(0)

comb = Combination(MOD)
print(comb.nCr(yoko + tate, yoko))

Solution (Python):
'''
Aは移動関数と同数：xとy方向に合計で3進んでいるから
Bは移動回数の内、(i+1,j+2)を選んだ回数：
1回でx方向には1進み、y方向には2進む
進み方は2通りしかないので、全体の内、1通りを選んだ回数を求めればOK
'''
X,Y=map(int,input().split())
if (X+Y)%3!=0:
  print(0)
  exit()
A=int((X+Y)/3)
B=int((2*Y-X)/3)
if A<0 or B<0:
  print(0)
  exit()
MOD=10**9+7
def comb(n,k):
  tmp=1
  for i in range(n-k+1,n+1):
    tmp*=i
    tmp%=MOD
  for i in range(1,k+1):
    tmp*=pow(i,MOD-2,MOD)
    tmp%=MOD
  return tmp
ans = comb(A,B)
print(ans%MOD)

Solution (Python):

def prepare(n, MOD):
    f = 1
    for m in range(1, n + 1):
        f = f * m % MOD
        
    fn = f
    
 
    
    inv = pow(f, MOD - 2, MOD)
    
    invs = [1] * (n + 1)
    
    invs[n] = inv
    for m in range(n, 1, -1):
        inv = inv * m % MOD
        invs[m - 1] = inv
 
    return fn, invs
 
def solve(x, y):
    d, m = divmod(x + y, 3)
    if m != 0 or x < d or y < d:
    
        return 0
    
    c = abs(d - x)
 
    MOD = 10 ** 9 + 7
    f, invs = prepare(d, MOD)
    return f * invs[c] * invs[d - c] % MOD
  
  
  
x, y = map(int, input().split())
print(solve(x, y))

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

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

if (2*X < Y):
    print (0)
    exit()

W = X - ((X+Y)//3)
H = Y - ((X+Y)//3)

mx = 10**6
fact = [1] * (mx+1) 
def inv(n): 
    return pow(n, MOD-2, MOD)
for i in range(mx):
    fact[i+1] = fact[i] * (i+1) % MOD 
ans = (fact[W+H] * inv(fact[W]) * inv(fact[H])) % MOD 
print (ans)

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

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

if (2*X < Y):
    print (0)
    exit()

W = X - ((X+Y)//3)
H = Y - ((X+Y)//3)

mx = 10**6
fact = [1] * (mx+1) 
def inv(n): 
    return pow(n, MOD-2, MOD)
for i in range(mx):
    fact[i+1] = fact[i] * (i+1) % MOD 
ans = (fact[W+H] * inv(fact[W]) * inv(fact[H])) % MOD 
print (ans)

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

if (X+Y) % 3 == 0:
    Z = int((X+Y)/3)
    A = X - Z
    B = Y - Z
    fact = [1] * (Z+1)
    fact_inv = [1] * (Z+1)

    if A >= 0 and A <= Z and A + B == Z:
        for i in range(1,Z+1):
            fact[i] = (fact[i-1] * i)%MOD
        for i in range(1,max(A,B)+1):
            fact_inv[i] = pow(fact[i], MOD-2,MOD)

        ans = (((fact[Z] * fact_inv[Z-A]) % MOD ) * fact_inv[A]) % MOD
        print(ans)
    else:
        print(0)


else:
    print(0)

Solution (Python):
import sys, math
from pprint import pprint
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

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

X, Y = rl()

if (X+Y) % 3 != 0:
	print(0)
	exit()
if not (1/2 <= Y/X <= 2):
	print(0)
	exit()
n = (X+Y)//3
k = X-n
print(combmod(n,k))

Solution (Python):
import numpy as np

MAX = 10**6+1
MOD = 10**9+7

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

def COMinit():
    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 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


if __name__ == '__main__':
    x, y = map(int, input().split())
    if (x + y) % 3 != 0:
        res = 0
    else:
        n, m = map(int, np.linalg.solve([[1, 2], [2, 1]], [x, y]))
        if n < 0 and m < 0:
            res = 0
        else:
            COMinit()
            res = COM(n + m, n)
    print(res)

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

MAX = 10**6+1
MOD = 10**9+7
fac = [1]
for i in range(1, MAX):
    fac.append(fac[i-1] * i % MOD)

if (x % 3 == 0 and y % 3 == 0) or (min(x % 3, y % 3) == 1 and max(x % 3, y % 3) == 2):
    xx, yy = max(x, y), min(x, y)
    a = b = 0
    while not xx == yy:
        xx -= 2
        yy -= 1
        a += 1
        if xx < 0 or yy < 0:
            res = 0
            break
    else:
        a += xx // 3
        b += yy // 3
        res = fac[a+b] * pow(fac[a]*fac[b], MOD-2, MOD) % MOD
    print(res)
else:
    print(0)

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

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

def comb(n, r, mod = 10**9 + 7):
    return ( framod(n,mod) * pow(framod(n-r,mod),mod-2,mod ) * pow(framod(r,mod),mod-2,mod))
X,Y=map(int,input().split())
if (X + Y) % 3 ==0:
  s,t= ( 2*X - Y ) // 3 , (2*Y - X) // 3
  if s*t<0:
    print(0)
  else:
    print(comb(s+t,s) % mod)
else:
  print(0)

Solution (Python):
import math
import sys

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


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

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

if X / 2 > Y or Y > X * 2:
    print(0)
    sys.exit()

x = X - ((X + Y) // 3)
y = Y - ((X + Y) // 3)

mod = 10 ** 9 + 7

numerater = 1
for i in range(1, x+y+1):
    numerater =  (numerater * i) % mod

denominator_x = 1
for i in range(1, x+1):
    denominator_x = (mod_inv(i, mod) * denominator_x) % mod

denominator_y = 1
for i in range(1, y+1):
    denominator_y = (mod_inv(i, mod) * denominator_y) % mod

res = (numerater * denominator_x * denominator_y) % mod

print(res)

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

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)
base=(X+Y)//3
print(cmb(base, (X-base),p))

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

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)
base=(X+Y)//3
print(cmb(base, (X-base),p))

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

a = 2*x - y
b = -x + 2*y
if a < 0 or b < 0:
    print(0)
    exit()

a //= 3
b //= 3
MOD = 10**9+7
MAX_N = 10**6
fact = [1]
fact_inv = [0]*(MAX_N+4)
for i in range(MAX_N+3):
    fact.append(fact[-1]*(i+1)%MOD)

fact_inv[-1] = pow(fact[-1],MOD-2,MOD)
for i in range(MAX_N+2,-1,-1):
    fact_inv[i] = fact_inv[i+1]*(i+1)%MOD

def mod_comb_k(n,k,MOD):
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] %MOD


print(mod_comb_k(a+b, a, MOD))

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

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

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

cnt = 0
while(y != 2*x):
    y += 1
    x -= 1
    cnt += 1

def mod_combination(n, k):
    mod = 1000000007
    v = 1
    for i in range(1, k+1):
        v *= n
        v %= mod
        v *= pow(i, mod-2, mod)
        v %= mod
        n -= 1
    return v % mod

print(mod_combination(x, cnt))

Solution (Python):
from functools import reduce
X,Y = (int(x) for x in input().split())
def route(x,y):
    d = min(x,y) - ((x+y)//3)
    if d == 0:
        return 1
    else:
        return (route(x-2,y-1) + route(x-1,y-2))%1000000007
def modmul(a,b):
    return a * b % 1000000007
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0
def modinv(a, m):
    g, x, y = xgcd(a, m)
    return x % m
def comb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(modmul, range(n, n - r, -1))
    under = reduce(modmul, range(1,r + 1))
    return over * modinv(under, 1000000007)
if (X+Y) % 3 != 0 or min(X,Y) < (X+Y) // 3:
    print('0')
else:
    d = min(X,Y) - ((X+Y)//3)
    if d == 0:
        print('1')
    else:
        t = (X+Y) // 3
        print((comb(t-1,d)+comb(t-1,d-1))%1000000007)

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

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

def make_tables(mod,n):
    fac = [1,1]
    ifac = [1,1]
    inverse = [0,1]

    for i in range(2,n+1):
        fac.append((fac[-1]*i)%mod)
        inverse.append((-inverse[mod%i]* (mod//i)) % mod)
        ifac.append((ifac[-1]* inverse[-1])%mod)
    return fac, ifac

if x > y:
    x, y = y, x
d = x+y
if d % 3 != 0:
    print(0)
else:
    s = (x+y)//3
    t = x - s

    if y > 2 * x:
        print(0)
    else:
        mod = 10**9+7

        fac, ifac = make_tables(mod,s)
        ans = comb(s,t,mod,fac,ifac)
        print(ans)

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

MOD = 10**9 + 7
def prepare(n, MOD):

    
    f = 1
    factorials = [1]  
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    
    inv = pow(f, MOD - 2, MOD)
    
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs
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)
        exit()


    fac, invs = prepare((n+m), MOD)


    print(fac[n+m]*invs[n]*invs[m]%MOD)

Solution (Python):
mod = 10**9 + 7
x, y = map(int, input().split())
move_count = 0
if (y + x) % 3 == 0:
    move_count = ((y + x) // 3)

move_a = (y - move_count) % mod
move_b = (x - move_count) % mod


def nck(n, k):
    numer, denom = 1, 1
    for i in range(k):
        numer = numer * (n - i) % mod
        denom = denom * (k - i) % mod
    return numer * pow(denom, mod-2, mod) % mod


ans = nck(move_count, min(move_a, move_b))
print(ans % 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
n = (2 * y - x) // 3 if (2 * y - x) % 3 == 0 else -1
m = (2 * x - y) // 3 if (2 * x - y) % 3 == 0 else -1

if n >= 0 and m >= 0:
    print(com(n + m, n, mod))
else:
    print(0)

Solution (Python):
def comb(n, k, p):
    if n<0 or k<0 or n<k:
        return 0
    if n==0 or k==0:
        return 1
    a, b, c = 1, 1, 1
    for i in range(1, n+1):
        a = (a*i)%p
    for i in range(1, k+1):
        b = (b*i)%p
    for i in range(1, n-k+1):
        c = (c*i)%p
    return a*pow(b, p-2, p)*pow(c, p-2, p)%p
X,Y = map(int,input().split())
if (X + Y) % 3 != 0:
    print(0)
else:
    n = int((X+Y)/3)
    p = max(X,Y)
    q = min(X,Y)
    if p > 2 * q:
        print(0)
    else:
        a = 2*n - p
        b = comb(n,a,10**9 + 7)
        print(b)

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

MOD = 10**9 + 7


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


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

if ((x + y) % 3 != 0):
    print("0")
else:
    a = (2*y - x) // 3
    b = (x - a) // 2

    if a < 0 or b < 0:
        print(0)
    else:
        print(combination(a+b, a))