Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)

/*
 * Modint struct
 * library author : @snuke
 * */
const int mod = 998244353;
struct mint {
  long long x; // typedef long long ll;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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(); }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct Combination {
  const int MAX = 1000050;
  vector<mint> fact;
  vector<mint> finv;
  Combination() : fact(MAX), finv(MAX) {
    for (int i = 0; i < MAX; i++) {
      if (i == 0)
        fact[i] = (mint)1;
      else
        fact[i] = fact[i - 1] * (mint)i;
    }
    finv[MAX - 1] = fact[MAX - 1].inv();
    for (int i = MAX - 2; i >= 0; i--) {
      finv[i] = finv[i + 1] * (mint)(i + 1);
    }
  }
  mint comb(int n, int r) {
    if (n < 0 || r < 0 || n < r)
      return 0;
    else
      return fact[n] * finv[r] * finv[n - r];
  }
} C;

ll n, m;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  mint ans = 0;

  for (int k = 0; k <= m; k++) {
    if ((3 * m - k) % 2 != 0)
      continue;
    ans +=
        C.comb(n, k) * (C.comb((3 * m - k) / 2 + n - 1, n - 1) -
                        (mint)k * C.comb((m - k) / 2 + n - 1, n - 1) -
                        (mint)(n - k) * C.comb((m - 2 - k) / 2 + n - 1, n - 1));
  }

  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)

/*
 * Modint struct
 * library author : @snuke
 * */
const int mod = 998244353;
struct mint {
  long long x; // typedef long long ll;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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(); }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
struct Combination {
  const int MAX = 2000050;
  vector<mint> fact;
  vector<mint> finv;
  Combination() : fact(MAX), finv(MAX) {
    for (int i = 0; i < MAX; i++) {
      if (i == 0)
        fact[i] = (mint)1;
      else
        fact[i] = fact[i - 1] * (mint)i;
    }
    finv[MAX - 1] = fact[MAX - 1].inv();
    for (int i = MAX - 2; i >= 0; i--) {
      finv[i] = finv[i + 1] * (mint)(i + 1);
    }
  }
  mint comb(int n, int r) {
    if (n < 0 || r < 0 || n < r)
      return 0;
    else
      return fact[n] * finv[r] * finv[n - r];
  }
} C;

ll n, m;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m;
  mint ans = 0;

  for (int k = 0; k <= m; k++) {
    if ((3 * m - k) % 2 != 0)
      continue;
    ans +=
        C.comb(n, k) * (C.comb((3 * m - k) / 2 + n - 1, n - 1) -
                        (mint)k * C.comb((m - k) / 2 + n - 1, n - 1) -
                        (mint)(n - k) * C.comb((m - 2 - k) / 2 + n - 1, n - 1));
  }

  cout << ans << '\n';
}