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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
class Program {
    static void Main(string[] args) {
        string[] input = Console.ReadLine().Split();
        long x = long.Parse(input[0]);
        long y = long.Parse(input[1]);
        long mod = 1000000007;

        if ((x + y) % 3 != 0) {
            Console.WriteLine("0");
            return;
        }

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

        //片方でも負の数があれば、答えはゼロとなる
        if (a < 0 || b < 0) {
            Console.WriteLine("0");
            return;
        }
        ////片方でも0があれば、答えは1となる
        //if (a == 0 || b == 0) {
        //    Console.WriteLine("1");
        //    return;
        //}

        //逆元の配列を作る
        long[] inv = new long[a];
        
        //まずは分子のmodを計算する
        long ans = FactorialMod(a + b, mod);

        //分母の逆元modをかける
        ans = ans * FactoricalModInv(a, mod) % mod * FactoricalModInv(b, mod) % mod;

        Console.WriteLine(ans);
    }

    //mod付き階乗の計算
    static long FactorialMod(long n, long m) {
        if (n <= 1) {
            return 1;
        }
        return n * FactorialMod(n - 1, m) % m;
    }

    //aの逆元を求める
    static long ModInv(long a, long M) {
        if (a <= 1) {
            return 1;
        }
        long m = (-(ModInv(M % a, M) * (M / a))) % M;
        if (m < 0) {
            m += M;
        }
        return m;
    }

    //分母の階乗をしながら逆元を求めていく
    static long FactoricalModInv(long a, long M) {
        if (a <= 1) {
            return 1;
        }
        return FactoricalModInv(a - 1, M) * ModInv(a, M) % M;
    }
}