Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};

mint N[2000001];

int main() {
  int X, Y;
  cin >> X >> Y;

  if ((-X + 2 * Y) % 3 != 0 || (2 * X - Y) % 3 != 0) {
    cout << "0" << endl;
    return 0;
  }

  int x = (-X + 2 * Y) / 3;
  int y = (2 * X - Y) / 3;

  N[0] = 1;
  for (int i = 1; i < 2000001; i++) {
    N[i] = N[i - 1] * i;
  }

  mint ans = N[x + y] / (N[x] * N[y]);

  cout << ans.x << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};

mint N[2000001];

int main() {
  int X, Y;
  cin >> X >> Y;

  if ((-X + 2 * Y) % 3 != 0 || (2 * X - Y) % 3 != 0) {
    cout << "0" << endl;
    return 0;
  }

  int x = (-X + 2 * Y) / 3;
  int y = (2 * X - Y) / 3;

  if (x < 0 || y < 0) {
    cout << "0" << endl;
    return 0;
  }

  N[0] = 1;
  for (int i = 1; i < 2000001; i++) {
    N[i] = N[i - 1] * i;
  }

  mint ans = N[x + y] / (N[x] * N[y]);

  cout << ans.x << endl;

  return 0;
}