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: Runtime Error)

using System;
using System.Numerics;

class Program
    {
		private static void Main(string[] args)
        {
            string[] line = Console.ReadLine().Split(' ');
            int X = int.Parse(line[0]);
            int Y = int.Parse(line[1]);

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

            int N = (X + Y) / 3;
            int R = X - N;
            BigInteger m = BigInteger.Parse("1000000007");
            Console.WriteLine(Combination(N, R) % m);
        }

        public static BigInteger Combination(int n, int r)
        {
            if (n < 0 || r < 0 || r > n) throw new ArgumentException("不正な引数です．");

            if (n - r < r) r = n - r;
            if (r == 0) return 1;
            if (r == 1) return n;

            int[] numerator = new int[r];
            int[] denominator = new int[r];

            for (int k = 0; k < r; k++)
            {
                numerator[k] = n - r + k + 1;
                denominator[k] = k + 1;
            }

            for (int p = 2; p <= r; p++)
            {
                int pivot = denominator[p - 1];
                if (pivot > 1)
                {
                    int offset = (n - r) % p;
                    for (int k = p - 1; k < r; k += p)
                    {
                        numerator[k - offset] /= pivot;
                        denominator[k] /= pivot;
                    }
                }
            }

            BigInteger result = BigInteger.One;
            for (int k = 0; k < r; k++)
            {
                if (numerator[k] > 1) result *= numerator[k];
            }

            return result;
        }
    }