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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class AtCoderD
{
    static void Main()
    {
        int mod = 1000000007;
        int x;
        int y;
        {
            var line = Console.ReadLine();
            var words = line.Split(' ');
            x = int.Parse(words[0]);
            y = int.Parse(words[1]);
        }

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

        int m = (2 * x - y) / 3;
        int n = x - 2 * m;

        if (m < 0 || n < 0)
        {
            Console.WriteLine(0);
            return;
        }

        long answer = Combination(n + m, m, mod);

        Console.WriteLine(answer);
    }

    // Comnibation（mod版）
    private static long Combination(int n, int k, int d)
    {
        long answer = 1;
        for (int i = n; i > n - k; i--)
        {
            answer = Mul(answer, i, d);
        }
        for (int i = 1; i <= k; i++)
        {
            answer = Div(answer, i, d);
        }
        return answer;
    }

    // 掛け算（mod版）
    private static long Mul(long a, long b, long c)
    {
        return ((a % c) * (b % c)) % c;
    }

    // 割り算（mod版）
    private static long Div(long a, long b, long c)
    {
        return Mul(a, Pow(b, c - 2, c), c);
    }

    // べき乗（mod版）
    private static long Pow(long a, long b, long c)
    {
        if (b == 0)
        {
            return 1;
        }
        else if (b == 1)
        {
            return a % c;
        }
        else if (b % 2 == 0)
        {
            long d = Pow(a, b / 2, c);
            return d * d % c;
        }
        else
        {
            return a * Pow(a, b - 1, c) % c;
        }
    }
}