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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include "bits/stdc++.h"
using namespace std;

#define show(x) cerr << #x << " = " << x << endl ;
#define input_from_file freopen("input.txt","r",stdin)
#define output_to_file freopen("output.txt" , "w",stdout)
#define output_to_error freopen("error.txt" , "w" ,stderr)
#define fastio ios_base::sync_with_stdio(0)
#define endl '\n'
#define int long long
const int mod = 1e9+7 , N = 1e6+1;
int fact[2*N];
int find(int x , int y)
{
    int ans = 1;
    while(y)
    {
        if(y&1)ans = (ans * x)%mod;
        y >>= 1;
        x = (x * x)%mod;
    }
    return ans;
}

signed main()
{
    #ifdef LOCAL
        input_from_file;
        output_to_file;
        output_to_error;
    #endif
    fastio;cin.tie(0);cout.tie(0);
    int x , y , ans = 0; cin >> x >> y ;
    fact[0] = 1;
    for(int i = 1 ; i < 2*N ; ++i)
        fact[i] = (i*fact[i-1])%mod;
    for(int a = 0 ; a <= x ; ++a)
    {
        if((x-a)&1) continue;
        else
        {
            int b = (x - a)/2;
            if(a + 2 * b == x && 2*a + b == y)
            {
                int num = fact[a+b];
                int den = (fact[a] * fact[b])%mod;
                ans = ( ans + num*find(den , mod-2))%mod;
            }
        }
    }
    cout << ans ;

    return 0;
}