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

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

def main():
    x, y = map(int, input().split(' '))
    if (2*x-y) % 3 == 0 and (2*y-x) % 3 == 0:
        n = (2*x-y) // 3
        m = (2*y-x) // 3
        for i in range( 2, N + 1 ):
            g1.append( ( g1[-1] * i ) % mod )
            inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
            g2.append( (g2[-1] * inverse[-1]) % mod )
        print(cmb(n+m, n, mod))
    else:
        print(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


if __name__ == '__main__':
    main()

Solution (Python):
def d_knight():
    X, Y = [int(i) for i in input().split()]

    if (abs(X) + abs(Y)) % 3 != 0 or Y > 2 * X or 2 * Y < X:
        return 0

    n = (abs(X) + abs(Y)) // 3
    k = -1
    for x in range(n, 2 * n + 1):
        if Y == 3 * n - x:
            k = x - n
            break

    class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ['mod', 'factorial', 'inverse']

        def __init__(self, max_val_arg: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_val_arg + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_val_arg, 0, -1):
                inv_append((inv[-1] * i) % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod
    return Combination().combination(n, k)

print(d_knight())

Solution (Python):
import math

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())
if ((x + y) % 3 != 0):
	print("0")
elif (x > 2*y or y > 2*x):
	print("0")
else:
	n_x = (2 * x - y) // 3
	n_y = (2 * y - x) // 3
	n_xy = (x + y) // 3
	print(cmb(n_xy,n_x)%1000000007)

Solution (Python):
import math

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

def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

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

def mod_fact(frm, to):
	ans = 1
	for i in range(frm, to + 1):
		ans = (ans * i) % mod
	return ans



if (x + y) % 3 == 0 and x <= y * 2 and y <= x * 2:
	n = (y * 2 - x) // 3
	m = (x + y) // 3
	
	
	

	print((mod_fact(m - n + 1, m) * (mod_inv(mod_fact(1, n), mod)) % mod))
else:
	print(0)

Solution (Python):
import sys

mod = 1000000007

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

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 mul(a, power(b, mod-2))

def nCr(n,r):

    if r * 2 > n:
        r = n-r

    now = 1
    for i in range(r):
        now *= n - i
        now *= gdic[r-i]
        now %= mod
    return now


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

a = 0
b = 0

while X > 0 and Y > 0:

    if X > Y:
        a += 1
        X -= 2
        Y -= 1

    else:
        b += 1
        X -= 1
        Y -= 2

"""
factorial = [1]
for n in range(1, a+b):
    factorial.append(factorial[n-1]*n%mod)

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

inverseFactorial = [0] * (a+b)
inverseFactorial[a+b-1] = power(factorial[a+b-1], mod-2)
for n in range(a+b-2, -1, -1):
    inverseFactorial[n] = inverseFactorial[n+1] * (n+1) % mod

def combi(n, m):
    return factorial[n] * inverseFactorial[m] * inverseFactorial[n-m] % mod
"""

sys.setrecursionlimit(500000)

gdic = [0]
gyaku = 1
for i in range(min(a,b)):

    i += 2
    gdic.append(gyaku)
    gyaku = mod -gdic[mod % i] * (mod // i) % mod



if X != 0 or Y != 0:
    print (0)

else:
    print (nCr(a+b,a) % mod)

Solution (Python):
x, y = map(int, input().split())                               
                                                               
if (x+y)%3!=0:                                                 
    print(0)                                                   
    exit()                                                     
                                                               
all = (x+y)//3                                                 
                                                               
if 2*all-x==y-all:                                             
    migi = 2*all-x                                             
else:                                                          
    print(0)                                                   
    exit()                                                     
                                                               
                                                               
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 )                  
                                                               
if 0 <= migi <= all:                                           
    migi = min(migi, all-migi)                                 
    print(cmb(all, migi, mod))                                 
else:                                                          
    print(0)

Solution (Python):
MOD = 10 ** 9 + 7
N = 10 ** 6
fact = [1]*(N+1)
rfact = [1]*(N+1)
r = 1
for i in range(1, N+1):
  fact[i] = r = r * i % MOD
rfact[N] = r = pow(fact[N], MOD-2, MOD)
for i in range(N, 0, -1):
  rfact[i-1] = r = r * i % MOD


def perm(n, k):
  return fact[n] * rfact[n-k] % MOD


def comb(n, k):
  return (fact[n] * rfact[k] * rfact[n-k]) % MOD

x,y = map(int, input().split())
ans = 0
if not (x + y) % 3:
  cnt = (x+y) // 3
  a = abs(x-y)
  if cnt >= a:
    r = (cnt - a) // 2
    ans = comb(cnt,r)
print(ans)

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 :
    moves = (x + y) // 3
    tate = (moves + abs(x - y)) // 2

    print(cmb(moves, tate, mod))
else :
    print(0)

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


def solve(x, y):
    if (x + y) % 3 != 0:
        return 0
    z = (x + y) // 3
    if x < z or y < z:
        return 0
    
    r = min(x - z, y - z)
    res = 1
    for i in range(r):
        res *= z - i
        res *= pow(i + 1, LARGE - 2, LARGE)
        res %= LARGE
    return res


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


def test():
    assert solve(3, 3) == 2
    assert solve(2, 2) == 0
    assert solve(999999, 999999) == 151840682


if __name__ == "__main__":
    
    main()

Solution (Python):
import sys

input = sys.stdin.readline


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

        cnt = 0
        for n in range(X + 1):
            if (X - n) % 2 != 0:
                continue
            else:
                m = (X - n) // 2
                if 2 * n + m == Y:
                    cnt += self.cmb(n+m,n)

        print(cnt % (10 ** 9 + 7))

    def cmb(self,  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 __name__ == '__main__':
    AtCoder().main()

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

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

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

X = max(x,y)
Y = min(x,y)

all_move = (X+Y)//3
diff = (X-Y)
y_move = (all_move - diff)//2


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

n = int(all_move)
r = int(y_move)
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

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


print(cmb(n,r,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 = result * int(numerator[k]) % 1000000007
    return result


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

if (X+Y)%3 != 0 or X < Y / 2 or Y < X / 2:
    print(0)
    exit()

num = (X + Y) // 3
ans = cmb(num, X-num)
print(ans)

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


X,Y = map(int,input().split())
a = 2*Y-X
b = 2*X-Y
if (a//3)*3!=a or (b//3)*3!=b or a<0 or b<0:
    ans = 0
else:
    A,B = a//3,b//3
    mod = 10**9+7
    N = A+B
    g1 = [1, 1] 
    g2 = [1, 1] 
    inverse = [0, 1] 

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

    ans = cmb(A+B,A,mod)
print(ans)

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

import numpy as np

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

MOD = 10**9 + 7

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U,MOD):
    x = np.arange(U,dtype=np.int64); x[0] = 1
    fact = cumprod(x,MOD)
    x = np.arange(U,0,-1,dtype=np.int64); x[0] = pow(int(fact[-1]),MOD-2,MOD)
    fact_inv = cumprod(x,MOD)[::-1]
    return fact,fact_inv

U = 10**6
fact, fact_inv = make_fact(U,MOD)

if (X+Y) % 3 != 0:
    print(0)
    exit()
A = (2*X - Y) // 3
B = (2*Y - X) // 3
if A < 0 or B < 0:
    print(0)
    exit()
answer = fact[A+B] * fact_inv[A] % MOD * fact_inv[B] % MOD
print(answer)

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

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

def power(n, r, mod):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod

def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod  
  


n = (x+y)//3

a = -1
for i in range(n+1):
  if i+2*(n-i) == x and  2*i+(n-i) == y:
    a = i
    
if a == -1:
  print(0)
else:
  ans = comb(n,a,MOD)
  print(ans%MOD)

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

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

Solution (Python):
import itertools
x,y=map(int,input().split())
if x>y:
    x,y=y,x
a=y-x
x-=a
y-=a*2

if x!=y or x%3!=0 or y%3!=0 or x<0 or y<0:
    print(0)
else:
    b=x//3
    ans=0
    
    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(a+2*b, b, mod=10**9+7))

Solution (Python):
x, y = map(int, input().split())
b3 = 2*x - y
a3 = 2*y - x
if a3 % 3 != 0 or b3 % 3 != 0:
    print(0)
else:
    a = a3 // 3
    b = b3 // 3

    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 )

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

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

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

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

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

n = (2 * x - y) / 3
m = (2 * y - x) / 3
mod = 10 ** 9 + 7

if int(n) == n and int(m) == m:
    n = int(n)
    m = int(m)
    ans = cmb(n + m, n, mod)
    ans %= mod
    print(ans)

else:
    print(0)

Solution (Python):


MOD = 10**9 + 7

def modPow(a, b, mod):
    x = 1
    
    while b > 0:
        if b & 1:
            x = (x*a)%mod

        a = (a**2)%mod
        b //= 2

    return x

def modFact(n, mod):
    x = 1

    for i in range(2, n+1):
        x = x*i%mod

    return x


def getModInv(a, b):
    return 1 if (a == 1) else int((1-b*getModInv(b%a, a))/a%b)


def f(X, Y):
    if X*2<Y or Y*2<X or (X+Y)%3 != 0:
        return 0

    N = (X+Y)//3
    K = X - N

    return(modFact(N, MOD)*(getModInv(
        modFact(K, MOD)*modFact(N-K, MOD)%MOD, MOD))%MOD)



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


if __name__ == '__main__':
    main()

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

def comb(n, k):
    ret = 1
    for i in range(1, k + 1):
        ret = ret * (n - i + 1) % MOD        
        ret = ret * pow(i, MOD - 2, MOD) % MOD
        
    return ret

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

if n >= 0 and m >= 0 and n == int(n) and m == int(m):
    print(comb(int(n + m), int(n)))
else:
    print(0)

Solution (Python):

def main():
    mod=1000000007
    x,y=map(int,input().split())
    k=min(x,y//2)
    l=min(x//2,y)
    n=k+l
    
    Fact=[1]
    for i in range(1,n+1):
        Fact.append(Fact[i-1]*i%mod)
    Finv=[0]*(n+1) 
    Finv[-1]=pow(Fact[-1],mod-2,mod)
    for i in range(n-1,-1,-1):
        Finv[i]=Finv[i+1]*(i+1)%mod
    def comb(n,r):
        if n<r:
            return 0
        return Fact[n]*Finv[r]*Finv[n-r]%mod
    
    ans=0
    for a in range(k+1):
        if (x-a)%2!=0:
            continue
        if (x-a)//2==y-2*a:
            b=(x-a)//2
            ans+=comb(a+b,a)
            ans%=mod
    print(ans)
    
if __name__=='__main__':
    main()

Solution (Python):
import math
p=10**9+7
def kaijo(n):
    kotae=1
    for i in range(1,n+1):
        kotae*=i
        
    kotae%=p
    return(kotae)

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

fanc=[1]*(10**6)
finv=[1]*(10**6)
inv=[1]*(10**6)

for i in range(2,666670):
    fanc[i]=fanc[i-1]*i%p
    inv[i]=p-inv[p%i]*(p//i)%p
    finv[i]=finv[i-1]*inv[i]%p

kanou=True

a=2*y-x
b=2*x-y

if(a<0 or b<0):
    kanou=False
if(a%3!=0 or b%3!=0):
    kanou=False

if(not kanou):
    print(0)
else:
    a//=3
    b//=3
    x-=a+b
    y-=a+b
    so=x+y
    
    
    
    
    
    
    
    ans=fanc[so]*finv[x]*finv[y]%p
    print(ans)

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

if (X + Y) % 3 == 0 : 
    n = (X + Y) // 3
    a = 2 * n - X
    
    if 0 <= a <= n and a * 2 + (n - a) == Y :
        fac = [1]
        inv = [1]
        for i in range(1, n + 1) :
            fac.append(fac[-1] * i % mod)
            inv.append(pow(fac[-1], mod - 2, mod))
        print(fac[n] * inv[n-a] * inv[a] % mod)
    
    else :
        print(0)
        
else :
    print(0)

Solution (Python):

import sys

MOD = 1000000007  

def mpow(x: int, k: int, MOD=1000000007) -> int:
    """
    x^kのmodを返します
    """
    res = 1
    y = x
    while (k):
        if(k % 2 == 1):
            res = (res * y) % MOD
        y = (y ** 2) % MOD
        k = k // 2
    return res

def mfact(MAX=200000, MOD=1000000007) -> 'List[int]':
    """
    MAXまでの階乗のmodの配列を返します
    """
    f = [1]
    for i in range(1, MAX):
        f.append(f[-1] * i % MOD)
    return f

def inv(x: int, MOD=1000000007) -> 'List[int]':
    """
    フェルマーの小定理に基づく逆元を返します
    """
    return mpow(x, MOD - 2, MOD)

def mcomb(n: int, k: int, f: 'List[int]', MOD=1000000007) -> int:
    """
    nCkのmodを返します
    """
    a = f[n]
    b = f[n-k]
    c = f[k]
    bc = (b * c) % MOD
    return (a * inv(bc)) % MOD

def solve(X: int, Y: int):
    if (2*Y-X) % 3 != 0 or (2*X-Y) % 3 != 0:
        print(0)
        return
    if 2*Y - X < 0 or 2 * X - Y < 0:
        print(0)
        return
    x = (2*Y-X) // 3
    y = (2*X-Y) // 3
    f = mfact(2000000)
    print(mcomb(x+y, x, f))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  
    Y = int(next(tokens))  
    solve(X, Y)

if __name__ == '__main__':
    main()

Solution (Python):
a,b=map(int,input().split())
aa=a
bb=b
x=0
y=0
while aa>0 and bb>0:
	if aa>bb:
		aa-=1
		x+=1
	else:
		bb-=1
		y+=1
	aa-=1
	bb-=1
if aa!=0 or bb!=0:
	print(0)
	exit()
mod=10**9+7
fac=[1]
for i in range(max(a,b)):
	fac.append(fac[i]*(i+1)%mod)
x+=y
r=pow(fac[y]*fac[x-y]%mod,mod-2,mod)
print(fac[x]*r%mod)

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

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)


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

if ((2 * X - Y) >= 0) and ((2 * X - Y) % 3 == 0):
    t = (2 * X - Y) // 3
else:
    print(0)
    exit()

if ((2 * Y - X) >= 0) and ((2 * Y - X) % 3 == 0):
    s = (2 * Y - X) // 3
else:
    print(0)
    exit()

print(cmb(s + t, s, mod))

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

def pow_mod(a, x, mod):
    if x == 0:
        return 1 % mod
    if x == 1:
        return a % mod
    half = pow_mod(a, x // 2, mod)
    half = half * half % mod
    if x % 2 == 1:
        half = half * a % mod
    return half

def rev(i, mod):
    return pow_mod(i, mod - 2, mod)

def calc(n, m, mod):
    if n == m or m == 0:
        return 1
    if n - m < m:
        m = n - m
    ans = 1
    for i in range(1, m + 1):
        f1 = ans * (n-i+1) * rev(i, mod) % mod
        ans = f1
    return ans


print(calc(row, x, mod))

Solution (Python):
def prepare(n, MOD):
    f = 1
    for m in range(1, n + 1):
        f *= m
        f %= MOD
    fn = 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 fn, invs


def solve(x, y):
    d, m = divmod(x + y, 3)
    if m != 0 or x / y > 2 or y / x > 2:
        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, 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())
if (2*x - y)%3 == 0 and (2*y - x)%3 == 0 and (2*x - y) >= 0 and (2*y - x) >=0:
    a = (2*x - y)//3
    b = (2*y - x)//3
    c = cmb(a+b,a)
    print(c%(10**9 + 7))
else:
    print(0)

Solution (Python):
import sys,collections as cl,bisect as bs,math,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
mod = 10**9+7 
N = 10**6
def cmb(n, r):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
def p(n,r):
    if ( r<0 or r>n ):
        return 0
    return g1[n] * g2[n-r] % mod

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 )

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

x,y = m()


li = []
count = 0

for i in range(0,x+1):
    nx = x - i
    ny = y - 2*i

    if nx >= 0 and ny >= 0 and nx == 2*ny:
        count += cmb(i+ny,i)
        count %= mod
print(count)

Solution (Python):
MOD=1000000007

def combination(n,m):
    if m==0:
        return 1
    else:
        return combination(n-1,m-1)*n//m

def pow_mod(a, b, p):
    if b == 0:
        return 1
    elif b%2 == 0:
        d = pow_mod(a, b//2, p)
        return (d**2)%p
    else:
        return (a*pow_mod(a,b-1,p))%p    
    
def factorial_mod(n,p):
    tmp = 1
    for i in range(1,n+1):
        tmp *= i
        tmp %= p
    return tmp


      
x,y=map(int,input().split())
if (x+y)%3!=0:
  print(0)
elif x*2<y:
  print(0)
elif y*2<x:
  print(0)
else:
  a=(x+y)//3
  x-=a
  y-=a
  
  if x>y:
    a=x
    x=y
    y=a
  W=x+1
  H=y+1
  ans = factorial_mod(W-1 + H-1, MOD)
  ans *= pow_mod(factorial_mod(H-1,MOD), MOD-2, MOD)
  ans %= MOD
  ans *= pow_mod(factorial_mod(W-1,MOD), MOD-2, MOD)
  ans %= MOD

 
  print(ans)

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

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

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

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv
if (x + y) % 3 != 0:
  print(0)
else:
  n = (x + y) // 3
  if n > x or n > y:
    print(0)
  else:
    nx = x - n
    comb = Combination(n)
    print(comb(n, nx))

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]


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

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

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

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

    sum_ = (x + y) // 3
    tmp_x = 2 * sum_
    if sum_ > x or sum_ > y:
        print(0)
        return

    if tmp_x > x:
        c_y = tmp_x - x
        c_x = sum_ - c_y
    else:
        c_y = -(tmp_x - x)
        c_x = sum_ - c_y

    print(cmb(sum_, c_x, MOD))


if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np
from math import factorial
from itertools import permutations

def cmb_fnct(MOD):
    
    fact_mod = [1,1]
    inv_mod = [0,1]
    inv_fact_mod = [1,1]
    i = len(fact_mod)
    i_ = len(inv_fact_mod)

    def cmb(n,r):
        nonlocal i, i_
        r = min(r, n-r)
        if r<0: 
            return 0
        if r==0:
            return 1
        if r==1: 
            return n%MOD

        while n >= i:
            fact_mod[i:i] = [(fact_mod[i-1]*i)%MOD]
            i += 1

        while n-r >= i_:
            inv_mod[i_:i_] = [(-inv_mod[MOD%i_]*(MOD//i_))%MOD]
            inv_fact_mod[i_:i_] = [(inv_fact_mod[i_-1]*inv_mod[i_])%MOD]
            i_ += 1

        return fact_mod[n] * inv_fact_mod[r] * inv_fact_mod[n-r] % MOD
    return cmb

def solve():
    X, Y = map(int, input().split())
    a = -(X-2*Y)/3
    b = -(-2*X+Y)/3
    if a == int(a) and b == int(b):
        a = int(a)
        b = int(b)
    else:
        return '0'
    cmb = cmb_fnct(10**9+7)

    return str(cmb(a+b,a))

if __name__ == '__main__':
    print(solve())

Solution (Python):
import math

def ncr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i 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 *= numerator[k]
    return result

def combi(i,j,mod):
    out = ncr(i+j,j)
    return out % mod

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

if (2*x-y)%3 == 0 and (2*y-x)%3 == 0 and (2*x-y) >= 0 and (2*y-x) >= 0:
    i = (2*x-y)//3
    j = (2*y-x)//3
    print(combi(i,j,10**9+7))
else:
    print(0)

Solution (Python):
a, b = map(int, input().split())

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

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

def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod

import math
if (2 * a - b) % 3 != 0 or (2 * b - a) % 3 != 0:
    print(0)
else:
    d = (2 * b - a) // 3
    c = (2 * a - b) // 3
    if c < 0 or d < 0:
        print(0)
    else:
        print(comb(c+d, c, 10 ** 9 + 7))

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

k = (X+Y)//3
if k > X or k > Y:
    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 + 1
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(k,abs(k-X),mod))