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 in Lua (5.3.2) (judge verdict: Accepted)

local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
----
-- returns (b^n) % m
local function modpow(b, n, m)
    if n == 0 then
        return 1
    elseif n % 2 == 0 then
        -- be careful about overflow of b*b
        return modpow(b * b % m, n//2, m)
    else
        return b * modpow(b, n - 1, m) % m
    end
end
local function make_mod_nCr(max, m)
    -- Create n! table
    local mfac = {}
    mfac[0] = 1
    for i=1,max do
        mfac[i] = mfac[i-1] * i % m
    end
    -- Create (n!)^(-1) table
    -- (n!)^(-1) ≡ (n!)^(m - 2)  <mod m>
    local invmfac = {}
    invmfac[max] = modpow(mfac[max], m - 2, m)
    for i=max,1,-1 do
        -- ((a-1)!)^(-1) ≡ a * (a!)^(-1)  <mod m>
        invmfac[i-1] = i * invmfac[i] % m
    end
    -- returns nCr % m
    local function mod_nCr(n, r)
        -- nCr ≡ n! * (r!)^(-1) * ((n-r)!)^(-1)  <mod m>
        local a = mfac[n] * invmfac[r] % m
        a = a * invmfac[n-r] % m
        return a
    end
    return mod_nCr
end
----
local MOD = math.floor(10^9) + 7
local mod_nCr = make_mod_nCr(math.floor(10^6) + 1, MOD)
local X, Y = read.nn()
if (X/2) <= Y and Y <= 2*X then
    if (X+Y) % 3 == 0 then
        local num = (X+Y) // 3 + 1
        local basex = (X+Y) // 3
        local fromL = X - basex + 1
        print(mod_nCr(num-1, fromL-1))
    else
        print(0)
    end
else
    print(0)
end