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 <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define ABS(a) (((a) >= 0) ? (a) : -(a))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define POW_10_5 100000
#define POW_10_9 1000000000
#define MOD 1000000007
#define INF (numeric_limits<int>::max()/2)
#define int long long

typedef pair<int, int> Pair;
typedef unordered_map<int, int> Hash;

// ---
int X, Y;

// Mod begin
class Mod {
public:
  // 全部 mod p で考える。
  // Combを使うにはpが素数である必要がある。
  int p;
  vector<int> fact;
  Mod(int p) : p(p), fact{1} {}

  int Fact(int n) {
    if (n % p > fact.size() - 1) {
      int i = fact.size();
      fact.resize(n % p + 1);
      for (; i <= n % p; ++i) {
        fact[i] = (fact[i-1] * i) % p;
      }
    }
    return fact[n];
  }

  // n! = a * p^e の a mod p をreturnしeを変更で返す
  int ModFact(int n, int& e) {
    e = 0;
    if (n == 0) { return 1; }

    int res = ModFact(n / p, e);
    e += n / p;

    if ((n / p) % 2 == 0) {
      return (res * Fact(n % p)) % p;
    } else {
      return (res * (p - Fact(n % p))) % p;
    }
  };

  int Extgcd(int a, int b, int& x, int& y) {
    int d = a;
    if (b != 0) {
      d = Extgcd(b, a % b, y, x);
      y -= (a / b) * x;
    } else {
      x = 1; y = 0;
    }
    return d;
  };

  // 一般的すぎるので繰り返し自乗法とかにしてもいいかも
  int Inv(int n) {
    int x, y;
    Extgcd(n, p, x, y);
    return ((p + x) % p) % p;
  };

  int Comb(int n, int k) {
    if (n < 0 || k < 0 || n < k) { return 0; }
    int e1, e2, e3;
    int a1 = ModFact(n, e1), a2 = ModFact(k, e2), a3 = ModFact(n - k, e3);
    if (e1 > e2 + e3) {
      return 0;
    } else {
      return (a1 * Inv((a2 * a3) % p)) % p;
    }
  };
};
// Mod end

int Ans() {
  if (Y > X) {
    int tmp = X;
    X = Y;
    Y = tmp;
  }
  // Xの方が大きいとしてよい

  int d = X - Y;
  // d回だけ(2,1)移動の回数が多い、残りは等しい回数

  // n: (3,3)移動の回数
  if ((Y-d)%3 != 0) { return 0; }
  int n = (Y-d)/3;

  // (2*n+d) C n mod MOD が答え
  Mod m(MOD);
  return m.Comb(2*n+d, n);
}

signed main() {
  cin >> X >> Y;

  auto ans = Ans();

  cout << ans << endl;
  return 0;
}