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: Wrong Answer)

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;
        }
        //a >= bとなるように順番を操作する（順番は関係ないため）
        if (a < b) {
            long temp = b;
            b = a;
            a = temp;
        }

        //long ans = 1;
        //for (long i = 0; i < b; i++) {
        //    ans = (ans * (a + b - i)) % mod;
        //}

        //まずは分子のmodを計算する
        long ans = FactorialMod(a + b, mod);

        //for (long i = 2; i <= b; i++) {
        //    ans = ans * Modinv(i, mod) % mod;
        //}

        ans = ans * FactoricalModInv(a, 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;
    }

    // mod. m での a の逆元 a^{-1} を計算する
    static long Modinv(long a, long M) {
        long b = M;
        long u = 1;
        long v = 0;

        while (b > 0) {
            long t = a / b;
            a -= t * b;
            long temp = a;
            a = b;
            b = temp;
            u -= t * v;
            long temp2 = u;
            u = v;
            v = temp2;
        }
        u %= M;
        if (u < 0) u += M;
        return u;
    }

    static long ModInv(long a, long M) {
        if (a == 1) {
            return 1;
        }
        return (-(ModInv(M % a, M) * (M / a))) % M;
    }

    static long FactoricalModInv(long a, long M) {
        if (a == 1) {
            return 0;
        }
        return FactoricalModInv(a - 1, M) * Modinv(a, M) % M;
    }
}