Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

const ll N_MAX = 1000005;

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll combination(ll C, ll D) {
  if (C < D || C < 1)
    return 0;
  return fac[C] * (finv[D] * finv[C - D] % mod) % mod;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();

  ll X, Y;
  cin >> X >> Y;
  ll A = X + Y;
  ll B = X - Y;

  if (A % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  ll T = A / 3;

  // dump(T)

  if ((B + T) % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }

  ll p = (B + T) / 2;

  cout << combination(T, p) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

const ll N_MAX = 1000005;

ll inv[N_MAX], fac[N_MAX], finv[N_MAX];
void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N_MAX; i++) {
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    fac[i] = fac[i - 1] * (ll)i % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll combination(ll C, ll D) {
  if (C < D || C < 1)
    return 0;
  return fac[C] * (finv[D] * finv[C - D] % mod) % mod;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  make();

  ll X, Y;
  cin >> X >> Y;
  ll A = X + Y;
  ll B = abs(X - Y);

  if (A % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }

  ll T = A / 3;

  // dump(T)

  if ((B + T) % 2 != 0) {
    cout << 0 << endl;
    return 0;
  }

  ll p = (B + T) / 2;

  cout << combination(T, p) << endl;
  return 0;
}