Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pii pair<int, int>
#define pb push_back
#define r1 rt << 1
#define r2 rt << 1 | 1
#define fi first
#define se second
#define ri register int
#define rep(i, a, b) for (ri i = (a); i <= (b); ++i)
#define rep2(i, a, b, c) for (ri i = (a); i <= (b); i += (c))
#define REP(i, a, b) for (ri i = (a); i >= (b); --i)
#define REP2(i, a, b, c) for (ri i = (a); i >= (b); i -= (c))
using namespace std;

inline int read() {
  ri x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar();
  return x * f;
}
inline ll readll() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar();
  return x * f;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
const int p = 1e9 + 7, N = 1e6 + 5;
int n, m, fac[N], inv[N];
inline int C(ri x, ri y) {
  if (x < y)
    return 0;
  return (ll)fac[x] * inv[y] % p * inv[x - y] % p;
}
inline int ksm(ri x, ri y) {
  ri z = 1;
  while (y) {
    if (y & 1)
      z = (ll)z * x % p;
    x = (ll)x * x % p;
    y >>= 1;
  }
  return z;
}
int main() {
  n = read();
  m = read();
  if ((n + m) % 3 != 0) {
    cout << 0;
    return 0;
  }
  ri xy = (n + m) / 3;
  ri x = m - xy;
  fac[0] = 1;
  rep(i, 1, xy) fac[i] = (ll)fac[i - 1] * i % p;
  inv[xy] = ksm(fac[xy], p - 2);
  REP(i, xy, 1) inv[i - 1] = (ll)inv[i] * i % p;
  printf("%d\n", C(xy, x));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pii pair<int, int>
#define pb push_back
#define r1 rt << 1
#define r2 rt << 1 | 1
#define fi first
#define se second
#define ri register int
#define rep(i, a, b) for (ri i = (a); i <= (b); ++i)
#define rep2(i, a, b, c) for (ri i = (a); i <= (b); i += (c))
#define REP(i, a, b) for (ri i = (a); i >= (b); --i)
#define REP2(i, a, b, c) for (ri i = (a); i >= (b); i -= (c))
using namespace std;

inline int read() {
  ri x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar();
  return x * f;
}
inline ll readll() {
  ll x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0'), c = getchar();
  return x * f;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
const int p = 1e9 + 7, N = 1e6 + 5;
int n, m, fac[N], inv[N];
inline int C(ri x, ri y) {
  if (x < y)
    return 0;
  return (ll)fac[x] * inv[y] % p * inv[x - y] % p;
}
inline int ksm(ri x, ri y) {
  ri z = 1;
  while (y) {
    if (y & 1)
      z = (ll)z * x % p;
    x = (ll)x * x % p;
    y >>= 1;
  }
  return z;
}
int main() {
  n = read();
  m = read();
  if ((n + m) % 3 != 0) {
    cout << 0;
    return 0;
  }
  ri xy = (n + m) / 3;
  ri x = m - xy, y = n - xy;
  if (x < 0 || y < 0) {
    cout << 0;
    return 0;
  }
  fac[0] = 1;
  rep(i, 1, xy) fac[i] = (ll)fac[i - 1] * i % p;
  inv[xy] = ksm(fac[xy], p - 2);
  REP(i, xy, 1) inv[i - 1] = (ll)inv[i] * i % p;
  printf("%d\n", C(xy, x));
  return 0;
}