Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

int64 power_mod(int64 a, int n) {
  int64 ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
  }
  return ret;
}

const int N = 1e6 + 10;
int64 F[N], G[N];

int main() {
  F[0] = G[0] = 1;
  for (int i = 1; i < N; ++i) {
    F[i] = F[i - 1] * i % MOD;
    G[i] = power_mod(F[i], MOD - 2);
  }
  int x, y;
  scanf("%d%d", &x, &y);
  if ((x + y) % 3) {
    puts("0");
    return 0;
  }
  int n = (x + y) / 3;
  int u = x - n, v = y - n;
  int64 ret = F[n] * G[u] % MOD * G[v] % MOD;
  printf("%lld\n", ret);
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

int64 power_mod(int64 a, int n) {
  int64 ret = 1;
  for (; n; n >>= 1) {
    if (n & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
  }
  return ret;
}

const int N = 1e6 + 10;
int64 F[N], G[N];

int main() {
  F[0] = G[0] = 1;
  for (int i = 1; i < N; ++i) {
    F[i] = F[i - 1] * i % MOD;
    G[i] = power_mod(F[i], MOD - 2);
  }
  int x, y;
  scanf("%d%d", &x, &y);
  if ((x + y) % 3) {
    puts("0");
    return 0;
  }
  int n = (x + y) / 3;
  int u = x - n, v = y - n;
  if (u < 0 || v < 0) {
    // trace(u, v);
    puts("0");
    return 0;
  }
  int64 ret = F[n] * G[u] % MOD * G[v] % MOD;
  printf("%lld\n", ret);
  return 0;
}