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):
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
x,y=map(int,input().split())
if (2*x-y)>=0 and (2*y-x)>=0 and (x+y)%3==0:
  print(mod_comb_k((x+y)//3,(2*y-x)//3,mod))
else:print(0)

Solution (Python):


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
INF = float("inf")

class Modinv:
    def __init__(self, n, mod):
        self.mod = mod
        self.fact = [1]*n
        self.finv = [1]*n
        self.inv  = [1]*n
        for i in range(2,n):
            self.fact[i] = (self.fact[i-1]*i) % self.mod
            self.inv[i]  = self.mod - self.inv[self.mod%i] * (self.mod//i)%self.mod
            self.finv[i] = self.finv[i-1] * self.inv[i] % self.mod

    def nPr(self, n, r):
        if n<r:
            return 0
        else:
            return self.fact[n] * self.finv[n-r] % self.mod

    def nCr(self, n, r):
        if n<r:
            return 0
        else:
            return self.fact[n] * (self.finv[r] * self.finv[n-r] % self.mod) % self.mod

    def nHr(self, n, r):
        return self.nCr(n-1+r, n-1)

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

    if (X+Y)%3 != 0:
        print(0)
    else:
        
        n = (X+Y)//3
        
        X -= n
        Y -= n
        
        if X < 0 or Y < 0:
            print(0)
        else:
            modinv = Modinv(10**6+10, 10**9+7)
            print(modinv.nCr(X+Y,X))


if __name__ == "__main__":
    main()

Solution (Python):
from math import factorial as F
def comInit(mx=510000, mod=pow(10,9)+7):
    fac = [0]*mx
    finv = [0]*mx
    inv = [0]*mx
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2,mx):
        fac[i] = fac[i-1]*i%mod
        inv[i] = mod - inv[mod%i]*(mod//i)%mod
        finv[i] = finv[i-1]*inv[i]%mod
    return fac, finv

def com(fac, finv, 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 = map(int, input().split())
mod = pow(10,9)+7
if (x+y)%3!=0:
    print(0)
else:
    w = (2*y-x)//3
    h = (2*x-y)//3
    if w<0 or h<0:
        print(0)
    else:
        fac, finv = comInit(w+h+1)
        print(com(fac, finv, w+h,w))

Solution (Python):
import sys
input = sys.stdin.readline
from collections import defaultdict, deque

(x, y), res, MOD = map(int, input().split()), 0, int(1e9) + 7
if (x + y) % 3 == 0:
    a, b = (y * 2 - x) // 3, (x * 2 - y) // 3
    if all([True if x >= 0 else False for x in (a, b)]):
        l, r = 1, 1
        for i in range(a + 1, a + b + 1): l = (l * i) % MOD
        for i in range(1, b + 1): r = (r * i) % MOD
        res = l * pow(r, MOD - 2, MOD)
print(res % MOD)

Solution (Python):
mod = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]


def init(n):
    for i in range(2, n + 1):
        fac.append(fac[-1] * i % mod)
        inv.append(-inv[mod % i] * (mod // i) % mod)
        finv.append(finv[-1] * inv[-1] % mod)


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


X, Y = map(int, input().split())
b = max([X, Y])
s = min([X, Y])
if s - (b-s) < 0 or not (s-(b-s)) % 3 == 0:
    print(0)
    exit()

s_t = (s-(b-s))//3
b_t = (s-(b-s))//3 + (b-s)
a = s_t + b_t
b = b_t
p = 10**9 + 7
init(a)
print(com(a, b, p))

Solution (Python):
MOD = 10**9 + 7
m = 666666 + 500
fac = [0] * m
finv = [0] * m
inv = [0] * m


def COMBinitialize(m):
    fac[0] = 1
    finv[0] = 1
    if m > 1:
        fac[1] = 1
        finv[1] = 1
        inv[1] = 1
        for i in range(2, m):
            fac[i] = fac[i-1] * i % MOD
            inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
            finv[i] = finv[i - 1] * inv[i] % MOD


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


COMBinitialize(m)


def main():
    X, Y = (int(i) for i in input().split())
    if (X+Y) % 3 != 0:
        return print(0)
    ans = COMB((X+Y)//3, X - (X+Y)//3)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
class Com:
    def __init__(self, MAX = 1000000, MOD = 1000000007):
        self.MAX = MAX
        self.MOD = MOD
        self._fac = [0]*MAX
        self._finv = [0]*MAX
        self._inv = [0]*MAX
        self._fac[0], self._fac[1] = 1, 1
        self._finv[0], self._finv[1] = 1, 1
        self._inv[1] = 1
        for i in range(2,self.MAX):
            self._fac[i] = self._fac[i - 1] * i % self.MOD
            self._inv[i] = self.MOD - self._inv[self.MOD%i] * (self.MOD // i) % self.MOD
            self._finv[i] = self._finv[i - 1] * self._inv[i] % self.MOD
    
    def com(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
a,b = list(map(int,input().split()))
if 2*a-b >= 0 and (2*a-b)%3 == 0 and a-2*((2*a-b)//3) >= 0:
    x = (2*a-b)//3
    y = a-2*x
    com = Com()
    print(com.com(x+y, x))
    exit()
a,b = b,a
if 2*a-b >= 0 and (2*a-b)%3 == 0 and a-2*((2*a-b)//3) >= 0:
    x = (2*a-b)//3
    y = a-2*x
    com = Com()
    print(com.com(x+y, x))
    exit()
print(0)

Solution (Python):
x, y = map(int, input().split())
tmp = 2*y - x
if tmp%3 != 0:
    print(0)
    exit()
up = tmp // 3
total = y - up*2 + up

n = total
k = up
mod = 10**9 + 7




if k == 0:
    print(1)
    exit()
try:
    modinv_table = [-1] * (k+1)
    modinv_table[1] = 1
    for i in range(2, k+1):
        modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod
except:
    k = total - up
    modinv_table = [-1] * (k + 1)
    modinv_table[1] = 1
    for i in range(2, k + 1):
        modinv_table[i] = (-modinv_table[mod % i] * (mod // i)) % mod

def binomial_coefficients(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

print(binomial_coefficients(n, k))

Solution (Python):
def bigcmb(N, R, MOD):  
    if (R < 0) or (N < R):
        return 0
    R = min(R, N - R)
    fact, inv = 1, 1
    for i in range(1, R + 1):
        fact = (fact * (N - i + 1)) % MOD
        inv = (inv * i) % MOD
    return fact * pow(inv, MOD - 2, MOD) % MOD


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

if (x + y) % 3 != 0:
    print(0)
else:
    cnt = (x + y) // 3
    print(bigcmb(cnt, x - cnt, mod))

Solution (Python):
def bigcmb(N, R, MOD):  
    if (R < 0) or (N < R):
        return 0
    R = min(R, N - R)
    fact, inv = 1, 1
    for i in range(1, R + 1):
        fact = (fact * (N - i + 1)) % MOD
        inv = (inv * i) % MOD
    return fact * pow(inv, MOD - 2, MOD) % MOD


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

if (x + y) % 3 != 0 or (x > 2 * y) or (2 * x < y):
    print(0)
else:
    cnt = (x + y) // 3
    print(bigcmb(cnt, x - cnt, mod))

Solution (Python):
def bigcmb(N, R, MOD):  
    if (R < 0) or (N < R):
        return 0
    R = min(R, N - R)
    fact, inv = 1, 1
    for i in range(1, R + 1):
        fact = (fact * (N - i + 1)) % MOD
        inv = (inv * i) % MOD
    return fact * pow(inv, MOD - 2, MOD) % MOD


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

if (x + y) % 3 != 0 or x > 2 * y or 2 * x < y:
    print(0)
else:
    print(bigcmb((x + y) // 3, x - (x + y) // 3, mod))

Solution (Python):
def nCr(n,r):
  dividend = 1
  divisor = 1
  MOD = 10**9+7
  d1 = n
  for i in range(1,r+1):
    dividend *= d1
    divisor *= i
    d1 -= 1
    dividend %= MOD
    divisor %= MOD

  return (dividend * pow(divisor, MOD-2, MOD)) % MOD

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

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

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

if n<0 or m<0:
  print(0)
else:
  print(nCr(n+m, n))

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)



class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True



def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P


def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]




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


x, y = map(int, input().split())
ans = 0
for i in range(x + 1):
    cnt1 = i
    if (x - cnt1) % 2 != 0 or y - 2 * cnt1 != (x - cnt1) // 2:
        continue
    cnt2 = (x - cnt1) // 2
    ans += mod_comb_k(cnt1 + cnt2, cnt1, MOD)
print(ans % MOD)

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


S = -X + 2 * Y
T = 2 * X - Y

if S < 0 or T < 0:
    print(0)
    exit()
if S % 3 != 0 or T % 3 != 0:
    print(0)
    exit()

S //= 3
T //= 3


def cmb(n, r, p):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = 1
    for i in range(n, n - r, -1):
        over = over * i % p
    under = 1
    for i in range(1, r + 1):
        under = under * i % p
    inv = pow(under, p - 2, p)
    return over * inv % p



ans = cmb(S + T, S, MOD) % MOD
print(ans % MOD)

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

if n==0 or m==0:
  print(1)
  exit()
def ka(x):
  a = 1
  while x!=1:
    a *= x
    x -= 1
    a%= mod
  return a

ans = (ka(n+m)*pow(ka(m),mod-2,mod)%mod)*(pow(ka(n),mod-2,mod))%mod
print(ans)

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

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


fac = [0] * (MAX + 1)
fac[0] = 1
fac[1] = 1
for i in range(2, MAX + 1):
    fac[i] = fac[i - 1] * i % MOD
    

finv = [0] * (MAX + 1)
finv[MAX] = pow(fac[MAX], MOD - 2, MOD)
for i in reversed(range(1, MAX + 1)):
    finv[i - 1] = finv[i] * i % MOD

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

mod = 10 ** 9 + 7
v1 = 2 * Y - X
v2 = 2 * X - Y
if (v1 >= 0 and v1 % 3 == 0 and v2 >= 0 and v2 % 3 == 0):
    a = v1 // 3
    b = v2 // 3
    print(comb(a + b, a))
else:
    print(0)

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

def mod_inv(n, m):
    return pow(n, m-2, m)

def nCr_mod_m(n, r, m):
    if r < 0 or n < r:
        return 0
    
    fact = fact_prev = 1 
    for i in range(2, n+1):
        fact = fact_prev * i % m
        fact_prev = fact
    
    fact_inv = [0]*(max(r, n-r)+1)
    fact_inv[0] = fact_inv[1] = 1  
    for i in range(2, max(r, n-r)+1):
        fact_inv[i] = fact_inv[i-1] * mod_inv(i, m) % m
    return (fact * fact_inv[r] % m) * fact_inv[n-r] % m

def main():
    X, Y = map(int, input().split())
    if ((X+Y)%3 != 0) or (min(X,Y)*2 < max(X,Y)):
        print(0)
        sys.exit()

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

    print(nCr_mod_m(x+y, x, MOD))
main()

Solution (Python):
import sys
from collections import deque
import itertools


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


def main():
    x,y = map(int,input().split())
    if (x+y)%3 !=0:
        print(0)
        exit()
    if x*2 <y or y *2 <x:
        print(0)
        exit()

    n = int((x+y)/3)
    


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

    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
    for i in range(2, N + 1):
        g1.append((g1[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod // i)) % mod)
        g2.append((g2[-1] * inverse[-1]) % mod)

    a = cmb(n, x-n, mod)

    print(a)
if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import deque
import itertools


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


def main():
    x,y = map(int,input().split())
    if (x+y)%3 !=0:
        print(0)
        exit()
    if x*2 <y or y *2 <x:
        print(0)
        exit()

    n = int((x+y)/3)
    


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

    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
    for i in range(2, N + 1):
        g1.append((g1[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod // i)) % mod)
        g2.append((g2[-1] * inverse[-1]) % mod)

    a = cmb(n, x-n, mod)

    print(a)
if __name__ == "__main__":
    main()

Solution (Python):
X, Y = map(int, input().split())
P = 10 ** 9 + 7
N = (X+Y) // 3
R = (2 * X - Y) // 3
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))


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


if (X+Y) % 3 != 0:
    print(0)
else:
    print(cmb_mod(N, R, P))

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


def log(*args):
    print(*args, file=sys.stderr)


def main():
    x, y = map(int, input().rstrip().split())
    
    
    
    
    
    
    
    
    
    
    if (2 * x - y) % 3 != 0 or (2 * y - x) % 3 != 0:
        print(0)
        return
    a = (2 * x - y) // 3
    b = (2 * y - x) // 3
    if a < 0 or b < 0:
        print(0)
        return
    
    
    mod = 10**9 + 7
    ans = (factorial_mod(a + b, mod) * pow(factorial_mod(min(a, b), mod), mod - 2, mod)
           * pow(factorial_mod(a + b - min(a, b), mod), mod - 2, mod)) % mod
    print(ans)


def factorial_mod(a, mod):
    ans = 1
    for v in range(1, a + 1):
        ans *= v
        ans %= mod
    return ans


if __name__ == '__main__':
    main()

Solution (Python):


def main():
    x, y = map(int, input().split())
    
    
    
    
    
    

    m = (2*x-y) // 3
    n = (2*y-x) // 3
    def cmb(n, r, mod):
        if (r<0 or r>n):
            return 0
        r = min(r, n-r)
        return fac[n] * finv[r] * finv[n-r] % mod

    mod = 10**9+7 
    N = m+n
    fac = [1, 1] 
    finv = [1, 1] 
    finv_cal = [0, 1] 

    for i in range(2, N + 1):
        fac.append((fac[-1] * i) % mod)
        finv_cal.append((-finv_cal[mod % i] * (mod//i)) % mod)
        finv.append((finv[-1] * finv_cal[-1]) % mod)
    
    
    if (x+y) % 3 != 0:
        print(0)
    else:
        
        if m < 0 or n < 0 or (m+n) == 0:
            print(0)
        else:
            ans = cmb(m+n, n, mod)           
            print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

X,Y = map(int,input().split())
import numpy as np
from functools import reduce
import math 
a = [[1, 2], [2, 1]]

mod = pow(10,9)+7

b = np.linalg.inv(a)


Z = np.array([[X],[Y]])

n,m=np.dot(b,Z)
p,q = *n,*m


def comb(n,k,p):
    if k==0:
        return 1
    A = reduce(lambda x,y:x*y%p ,[n-k+1+i for i in range(k)])
    B = reduce(lambda x,y:x*y%p ,[i+1 for i in range(k)])
    return A*pow(B,p-2,p)%p


if p<0 or q<0:
    print(0)
elif not abs(p-round(p))<pow(10,-3) or not abs(q-round(q))<pow(10,-3):
    print(0)
elif p==0 and q==0:
    print(0)
elif p==0:
    print(1)
elif q==0:
    print(1)
else:
    n = int(round(p))
    m = int(round(q))
    ans = comb(n+m,m,mod)

    print(ans%mod)

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

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

def comb(n,r):
    fact = 1
    invfact = 1
    for i in range(r):
        fact *= n-i
        fact %= mod
        invfact *= i+1
        invfact %=mod
    return fact*pow(invfact,mod-2,mod)%mod

print(comb(x+y,y))

Solution (Python):
x,y = map(int, input().split())
a = 0
b = 0
if x>y:
  while x>y:
    x-=2
    y-=1
    a+=1
if x<y:
  while x<y:
    x-=1
    y-=2
    b+=1
if x == y and x%3==0:
    a+=x//3
    b+=y//3
else:
  print(0)
  exit(0)
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 )

print(cmb(a+b,b,mod))

Solution (Python):

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

OK = False
X, Y = map(int, input().split())
for i in range(0, max(X, Y)+1):
    if (Y - (X - i*2)*2) == i and X-2*i >= 0:
        x = i
        y = X - i*2
        print(mod_comb_k(x + y, min(x, y), mod))
        OK = True
        break
if not OK:
    print(0)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
		

lim = 10**6   
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod


fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

X, Y = MAP()

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

if a%1 == b%1 == 0 and 0 <= a and 0 <= b:
	print(C(int(a+b), int(a)))
else:
	print(0)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *
 
		

lim = 10**6   
fact = [1] * (lim+1)
for n in range(1, lim+1):
	fact[n] = n * fact[n-1] % mod


fact_inv = [1]*(lim+1)
fact_inv[lim] = pow(fact[lim], mod-2, mod)
for n in range(lim, 0, -1):
	fact_inv[n-1] = n*fact_inv[n]%mod

def C(n, r):
	return (fact[n]*fact_inv[r]%mod)*fact_inv[n-r]%mod

X, Y = MAP()

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

if a%1 == b%1 == 0 and 0 <= a and 0 <= b:
	print(C(int(a+b), int(a)))
else:
	print(0)

Solution (Python):
M=10**9+7
x,y=map(int,input().split())
ans=0
if (x+y)%3==0:
  a=(2*y-x)//3
  b=(2*x-y)//3
  if a>=0 and b>=0:
    f1,f2=1,1
    for i in range(a+1,a+b+1):
      f1*=i
      f1%=M
    for i in range(1,b+1):
      f2*=i
      f2%=M
    ans=f1*pow(f2,M-2,M)

print(ans%M)

Solution (Python):
import math
mod=10**9+7
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod
def div(a, b):
    return mul(a, pow(b, mod-2,mod))
x,y=map(int,input().split())
x,y=min(x,y),max(x,y)
s=y-x
if (x-s)<0 or 3*((x-s)//3)+2*s!=y:
  print(0)
else:
  a=(x-s)//3
  b=(y-2*s)//3+s
  A=1
  B=1
  C=1
  for i in range(1,a+b+1):
    A*=i
    A%=mod
  for i in range(1,a+1):
    B*=i
    B%=mod
  for i in range(1,b+1):
    C*=i
    C%=mod
  print(div(A,mul(B,C)))

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def mod_com_once(n, r, MOD):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    r = min(r, n - r)
    numer = denom = 1
    for i in range(n - r + 1, n + 1):
        numer = numer * i % MOD
    for i in range(1, r + 1):
        denom = denom * i % MOD
    return numer * pow(denom, MOD - 2, MOD) % MOD


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

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

    p, q = (-X + 2 * Y) // 3, (2 * X - Y) // 3
    if p < 0 or q < 0:
        print(0)
        return

    ans = mod_com_once(p + q, p, MOD)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import math
X, Y = map(int, input().split())
      
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 = 10**9+7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]
for i in range(2, (X+Y)//3+1):
  fac.append(fac[-1] * i % mod)
  inv.append(-inv[mod % i] * (mod // i) % mod)
  finv.append(finv[-1] * inv[-1] % mod)
  
ans = 0
x = 0
y = 0
if (X + Y) % 3 == 0:
  x = (2*Y-X) // 3
  y = (2*X-Y) // 3
  ans = cmb(x+y, x, mod)

print(ans)

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

m, r = divmod(2*Y-X, 3)
if r != 0:
    print(0)
    exit()

n = Y - 2*m

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

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

print(combination(n+m, n))

Solution (Python):
x,y = map(int,input().split())
p = 10**9+7
from math import factorial


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



ans = 1
for i in range(0,x//2+2):
    j = x - 2*i
    if 2*j + i == y:
        a = i
        b = j
        break
else:
    print(0)
    exit()

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

a_inv = 1
for i in range(1,a+1):
    a_inv *= i%p
    a_inv %= p
a_inv = pow(a_inv,p-2,p)

b_inv = 1
for i in range(1,b+1):
    b_inv *= i%p
    b_inv %= p
b_inv = pow(b_inv,p-2,p)

ans *= (a_inv*b_inv)%p

print(ans%p)

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

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

mmm = 1000000000 + 7
sss = x + y
if sss % 3 != 0:
    print(0)
    exit()
hoge = sss // 3






p =  y - hoge
q = x - hoge

if p < 0 or q < 0:  
    print(0)
    exit()


fac = []
inv = []
inv_fac = []
def init(n):
    fac.append(1)
    fac.append(1)
    inv.append(0)
    inv.append(1)
    inv_fac.append(1)
    inv_fac.append(1)
    for i in range(2, n):
        fac.append(fac[-1] * i % mmm)
        inv.append(mmm - inv[mmm%i] * (mmm // i) % mmm)
        inv_fac.append(inv_fac[-1] * inv[-1] % mmm)

def choice(a, b):
    if a < b:
        return 0
    return fac[a] * (inv_fac[b] * inv_fac[a-b] % mmm) % mmm

init(hoge * 2 + 1)
print(choice(hoge, p) % mmm)

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

    facn = 1
    facr = 1
    facnr = 1
    for i in range(1, n + 1):
        facn = (facn * i) % mod
        if i == r:
            facr = facn
        if i == n - r:
            facnr = facn
    
    b = 1
    invfacr = 1
    invfacnr = 1
    while b <= mod:
        if b & (mod - 2) != 0:
            invfacr = (invfacr * facr) % mod
            invfacnr = (invfacnr * facnr) % mod
        facr = (facr * facr) % mod
        facnr = (facnr * facnr) % mod
        b *= 2

    print((facn * invfacr * invfacnr) % mod)

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





ans = 0
for a in range(X//2 + 1):
    b = X - a * 2
    y = 2 * b + a
    if b >= 0 and Y == y:
        ans = cmb(a+b, a) % (10**9 + 7)

print(ans)