Problem p02862 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define String std::string
#define fir first
#define sec second
#define mp std::make_pair
#define Pair std::pair<int, int>
#define Map std::map<int, int>
#define Vector std::vector<int>

typedef long long ll;
typedef unsigned long long ull;

const int N = 700000 + 5;
const int MOD = 1e9 + 7;
const int inv2 = 500000004;
const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

inline ll read() {
  ll res = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    res = (res << 1) + (res << 3) + ch - '0';
  return res * f;
}

inline void exgcd(ll a, ll b, ll &g, ll &x, ll &y) {
  if (!b)
    g = a, x = 1, y = 0;
  else
    exgcd(b, a % b, g, y, x), y -= x * (a / b);
}

inline ll inv(ll a) {
  ll g, res, tmp;
  exgcd(a, MOD, g, res, tmp);
  return ((res % MOD) + MOD) % MOD;
}

ll f[N], fi[N];

inline void prepare() {
  f[0] = 1;
  int n = 700000;
  for (int i = 1; i <= n; i++)
    f[i] = f[i - 1] * i % MOD;
  fi[n] = inv(f[n]);
  for (int i = n - 1; i >= 1; i--)
    fi[i] = fi[i + 1] * (i + 1) % MOD;
}

inline ll C(int n, int m) {
  if (!m)
    return 1;
  if (n == m)
    return 1;
  if (m > n)
    return 0;
  return f[n] * fi[m] % MOD * fi[n - m] % MOD;
}

int main() {
  int x, y;
  int z;
  prepare();
  scanf("%d %d", &x, &y);
  z = x + y;
  if (z % 3) {
    puts("0");
  } else {
    printf("%lld\n", C(z / 3, y - z / 3));
  }
  return 0;
}

Fixed submission:
// #pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define String std::string
#define fir first
#define sec second
#define mp std::make_pair
#define Pair std::pair<int, int>
#define Map std::map<int, int>
#define Vector std::vector<int>

typedef long long ll;
typedef unsigned long long ull;

const int N = 700000 + 5;
const int MOD = 1e9 + 7;
const int inv2 = 500000004;
const int dx[] = {0, 1, -1, 0, 1, -1, 1, -1};
const int dy[] = {1, 0, 0, -1, 1, -1, -1, 1};

inline ll read() {
  ll res = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    res = (res << 1) + (res << 3) + ch - '0';
  return res * f;
}

inline void exgcd(ll a, ll b, ll &g, ll &x, ll &y) {
  if (!b)
    g = a, x = 1, y = 0;
  else
    exgcd(b, a % b, g, y, x), y -= x * (a / b);
}

inline ll inv(ll a) {
  ll g, res, tmp;
  exgcd(a, MOD, g, res, tmp);
  return ((res % MOD) + MOD) % MOD;
}

ll f[N], fi[N];

inline void prepare() {
  f[0] = 1;
  int n = 700000;
  for (int i = 1; i <= n; i++)
    f[i] = f[i - 1] * i % MOD;
  fi[n] = inv(f[n]);
  for (int i = n - 1; i >= 1; i--)
    fi[i] = fi[i + 1] * (i + 1) % MOD;
}

inline ll C(int n, int m) {
  if (!m)
    return 1;
  if (n == m)
    return 1;
  if (m > n)
    return 0;
  if (m < 0)
    return 0;
  return f[n] * fi[m] % MOD * fi[n - m] % MOD;
}

int main() {
  int x, y;
  int z;
  prepare();
  scanf("%d %d", &x, &y);
  z = x + y;
  if (z % 3) {
    puts("0");
  } else {
    printf("%lld\n", C(z / 3, y - z / 3));
  }
  return 0;
}