Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;

ll mod = 998244353;

ll mod_pow(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll res = mod_pow(a, b / 2) % mod;
  res *= res;
  res %= mod;
  if (b % 2)
    res *= a;
  return res % mod;
}

struct perm {
private:
  int sz;
  vector<ll> p, invp;

public:
  perm(int n) {
    sz = n + 1;
    p.resize(sz), invp.resize(sz);
    p[0] = 1;
    for (int i = 1; i <= sz - 1; i++) {
      p[i] = p[i - 1] * i % mod;
    }
    invp[sz - 1] = mod_pow(p[sz - 1], mod - 2);
    for (int i = sz - 2; i >= 0; i--) {
      invp[i] = invp[i + 1] * (i + 1) % mod;
    }
  }
  ll comb(ll x, ll y) {
    if (x < y || y < 0)
      return 0;
    return (p[x] * invp[x - y] % mod) * invp[y] % mod;
  }
};
perm p(1 << 20);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  ll ans = 0;
  for (int i = 0; i <= min(n, m); i++) {
    if ((3 * m - i) % 2)
      continue;
    ll k = (3 * m - i) / 2;
    (ans += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
  }
  ll res = 0;
  ll res2 = 0;
  for (int i = 0; i <= (m, n); i++) {
    if ((m - i) % 2)
      continue;
    ll k = (m - i) / 2;
    (res += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
  }
  for (int i = 0; i <= (m, n - 1); i++) {
    if ((m - i) % 2)
      continue;
    ll k = (m - i) / 2;
    (res2 += p.comb(n - 1, i) * p.comb(n - 2 + k, k) % mod) %= mod;
  }
  res -= res2;
  res %= mod;
  ans -= res * n;
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long int ll;

ll mod = 998244353;

ll mod_pow(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll res = mod_pow(a, b / 2) % mod;
  res *= res;
  res %= mod;
  if (b % 2)
    res *= a;
  return res % mod;
}

struct perm {
private:
  int sz;
  vector<ll> p, invp;

public:
  perm(int n) {
    sz = n + 1;
    p.resize(sz), invp.resize(sz);
    p[0] = 1;
    for (int i = 1; i <= sz - 1; i++) {
      p[i] = p[i - 1] * i % mod;
    }
    invp[sz - 1] = mod_pow(p[sz - 1], mod - 2);
    for (int i = sz - 2; i >= 0; i--) {
      invp[i] = invp[i + 1] * (i + 1) % mod;
    }
  }
  ll comb(ll x, ll y) {
    if (x < y || y < 0)
      return 0;
    return (p[x] * invp[x - y] % mod) * invp[y] % mod;
  }
};
perm p(5000000);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, m;
  cin >> n >> m;
  ll ans = 0;
  for (int i = 0; i <= min(n, m); i++) {
    if ((3 * m - i) % 2)
      continue;
    ll k = (3 * m - i) / 2;
    (ans += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
  }
  ll res = 0;
  ll res2 = 0;
  for (int i = 0; i <= (m, n); i++) {
    if ((m - i) % 2)
      continue;
    ll k = (m - i) / 2;
    (res += p.comb(n, i) * p.comb(n - 1 + k, k) % mod) %= mod;
  }
  for (int i = 0; i <= (m, n - 1); i++) {
    if ((m - i) % 2)
      continue;
    ll k = (m - i) / 2;
    (res2 += p.comb(n - 1, i) * p.comb(n - 2 + k, k) % mod) %= mod;
  }
  res -= res2;
  res %= mod;
  ans -= res * n;
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}