Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/numeric>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define popcount(n) __builtin_popcount(n)
#define popcountll(n) __builtin_popcountll(n)
#define ll long long

using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
// template<typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;

const double PI = acos(-1.0);

const int N = 1e6 + 5, MOD = 1e9 + 7;
long long fact[N], fact_inv[N];

long long mul(long long a, long long b) { return a * b % MOD; }

long long power(long long a, long long b) {
  if (!b)
    return 1;
  long long r = power(a, b / 2);
  r = mul(r, r);
  if (b & 1)
    return mul(r, a);
  return r;
}

long long mod_inv(long long x) { return power(x, MOD - 2); }

long long nCr(long long n, long long r) {
  if (!n)
    return !r;
  if (r > n)
    return 0;
  return mul(fact[n], mul(fact_inv[n - r], fact_inv[r]));
}

void pre() {
  fact[0] = fact_inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
    fact_inv[i] = mod_inv(fact[i]);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
//    freopen("friday.out", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  pre();
  int x, y;
  cin >> x >> y;
  long long ans = 0;
  for (int i = 0; i <= x; ++i) {
    int a = x - i;
    int b = y - 2 * i;
    if (a % 2 == 0 && a / 2 == b) {
      ans += nCr(i + b, b);
      ans %= MOD;
    }
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/numeric>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_update
#define oo 0x3f3f3f3f
#define OO 0x3f3f3f3f3f3f3f3f
#define popcount(n) __builtin_popcount(n)
#define popcountll(n) __builtin_popcountll(n)
#define ll long long

using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
// template<typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
// tree_order_statistics_node_update>;

const double PI = acos(-1.0);

const int N = 1e6 + 5, MOD = 1e9 + 7;
long long fact[N], fact_inv[N];

long long mul(long long a, long long b) { return a * b % MOD; }

long long power(long long a, long long b) {
  if (!b)
    return 1;
  long long r = power(a, b / 2);
  r = mul(r, r);
  if (b & 1)
    return mul(r, a);
  return r;
}

long long mod_inv(long long x) { return power(x, MOD - 2); }

long long nCr(long long n, long long r) {
  if (!n)
    return !r;
  if (r > n)
    return 0;
  return mul(fact[n], mul(fact_inv[n - r], fact_inv[r]));
}

void pre() {
  fact[0] = fact_inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
    fact_inv[i] = mod_inv(fact[i]);
  }
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "rt", stdin);
//    freopen("friday.out", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  pre();
  int x, y;
  cin >> x >> y;
  long long ans = 0;
  for (int i = 0; i <= x; ++i) {
    int a = x - i;
    int b = y - 2 * i;
    if (a % 2 == 0 && a / 2 == b) {
      ans += nCr(i + b, b);
      ans %= MOD;
    }
  }

  cout << ans << '\n';

  return 0;
}