Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
// #define cccgift
// #define
// getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
template <typename T> inline void read(T &x) {
  static char ch;
  bool f = 1;
  for (x = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = 0;
  for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar())
    ;
  x = f ? x : -x;
}
template <typename T> void print(T x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char ap) {
  print(x);
  if (ap)
    putchar(ap);
}
template <typename T> inline void chkmax(T &x, const T &y) {
  x = x < y ? y : x;
}
template <typename T> inline void chkmin(T &x, const T &y) {
  x = x < y ? x : y;
}
const int mod = 1e9 + 7;
int n, m, fac[2000001], inv[2000001];
inline int C(int x, int y) {
  return (ll)fac[x] * inv[y] % mod * inv[x - y] % mod;
}
inline int kuai(int p, int q, int mod) {
  int tot = 1;
  for (; q; p = (ll)p * p % mod, q >>= 1)
    if (q & 1)
      tot = (ll)tot * p % mod;
  return tot;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  if ((2 * n - m) % 3 || (2 * m - n) % 3)
    return cout << 0 << '\n', 0;
  int now1 = (2 * n - m) / 3, now2 = (2 * m - n) / 3, now = now1 + now2;
  fac[0] = 1;
  for (res i = 1; i <= now; ++i)
    fac[i] = (ll)fac[i - 1] * i % mod;
  inv[now] = kuai(fac[now], mod - 2, mod);
  for (res i = now - 1; ~i; --i)
    inv[i] = (ll)inv[i + 1] * (i + 1) % mod;
  cout << C(now1 + now2, now1) << '\n';
  cout << flush;
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?), set tle
 * do something instead of nothing and stay organized
 */

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
// #define cccgift
// #define
// getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
// char buf[1<<21],*p1=buf,*p2=buf;
template <typename T> inline void read(T &x) {
  static char ch;
  bool f = 1;
  for (x = 0, ch = getchar(); !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = 0;
  for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar())
    ;
  x = f ? x : -x;
}
template <typename T> void print(T x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void print(T x, char ap) {
  print(x);
  if (ap)
    putchar(ap);
}
template <typename T> inline void chkmax(T &x, const T &y) {
  x = x < y ? y : x;
}
template <typename T> inline void chkmin(T &x, const T &y) {
  x = x < y ? x : y;
}
const int mod = 1e9 + 7;
int n, m, fac[2000001], inv[2000001];
inline int C(int x, int y) {
  return (ll)fac[x] * inv[y] % mod * inv[x - y] % mod;
}
inline int kuai(int p, int q, int mod) {
  int tot = 1;
  for (; q; p = (ll)p * p % mod, q >>= 1)
    if (q & 1)
      tot = (ll)tot * p % mod;
  return tot;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  if ((2 * n - m) % 3 || (2 * m - n) % 3 || 2 * m - n < 0 || 2 * n - m < 0)
    return cout << 0 << '\n', 0;
  int now1 = (2 * n - m) / 3, now2 = (2 * m - n) / 3, now = now1 + now2;
  fac[0] = 1;
  for (res i = 1; i <= now; ++i)
    fac[i] = (ll)fac[i - 1] * i % mod;
  inv[now] = kuai(fac[now], mod - 2, mod);
  for (res i = now - 1; ~i; --i)
    inv[i] = (ll)inv[i + 1] * (i + 1) % mod;
  cout << C(now1 + now2, now1) << '\n';
  cout << flush;
  return 0;
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?), set tle
 * do something instead of nothing and stay organized
 */