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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int x, y; scanf("%d%d", &x, &y);

	const int MOD = 1e9 + 7;
	std::vector<i64> fact(1e6 + 10, 1);
	for(i64 i = 0; i < fact.size() - 1; i++) fact[i + 1] = fact[i] * (i + 1) % MOD;

	auto inv = [&](i64 x) {
		i64 n = MOD - 2, ret = 1;
		while(n) {
			if(n & 1) (ret *= x) %= MOD;
			(x *= x) %= MOD;
			n >>= 1;
		}
		return ret;			
	};
	auto comb = [&](int n, int r) {
		return fact[n] * inv(fact[r]) % MOD * inv(fact[n - r]) % MOD;
	};

	int ans = 0;
	for(int i = 0; i <= x; i++) {
		if((x - i) & 1) continue;
		int j = (x - i) >> 1;
		if(2 * i + j != y) continue;

		(ans += comb(i + j, i)) %= MOD;
	}
	printf("%d\n", ans);
	return 0;
}