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.IO;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using System.Runtime.CompilerServices;
using static MyUtil;

class MyUtil
{
    public static long[] ReadLongArray()
    {
	return ReadLine().Split().Select(x => long.Parse(x)).ToArray();
    }
    public static void Swap<T>(ref T a, ref T b)
    {
	var t = a;
	a = b;
	b = t;
    }
}

class Program
{
    static long ModInv(long a, long m)
    {
	long b = m, u = 1, v = 0;
	while (b > 0) {
	    long t = a / b;
	    a -= t * b; Swap(ref a, ref b);
	    u -= t * v; Swap(ref u, ref v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;	
    }
    
    public static void Main()
    {
	var tmp = ReadLongArray();
	long x = tmp[0], y = tmp[1];

	//    a + 2b = x   (1)
	//   2a +  b = y   (2)
	//=> a = (2y - x) / 3, b = (2x - y) / 3

	if (2*y - x >= 0 && (2*y - x) % 3 == 0 &&
	    2*x - y >= 0 && (2*x - y) % 3 == 0)
	{
	    long a = (2*y - x) / 3;
	    long b = (2*x - y) / 3;

	    long M = 1000000007;

	    long ans = 1;
	    for (long i = 1; i <= a+b; i++)
		ans = ans * i % M;

	    for (long i = 1; i <= a; i++)
	    {
		long iv = ModInv(i, M);
		ans = ans * iv % M;
	    }

	    for (long i = 1; i <= b; i++)
	    {
		long iv = ModInv(i, M);
		ans = ans * iv % M;
	    }

	    WriteLine(ans);
	}
	else
	{
	    WriteLine(0);
	}
    }
}