Problem p02965 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define foreach(it, v)                                                         \
  for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second

typedef unsigned int uint;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef set<int> si;

const int MAX = 1010000;
const int MOD = 998244353;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int N, M;

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

  // 前処理
  COMinit();

  cin >> N >> M;

  ll ans = 0;
  for (int n = M % 2; n <= M; n += 2) {
    int D = (M * 3 - n) / 2;
    ll a1 = COM(N, n) * COM(N + D - 1, N - 1) % MOD;
    ll b1 = N * (COM(N - 1, n - 1) * COM(N + D - M - 1, N - 1) % MOD) % MOD;
    ll b2 = N * (COM(N - 1, n) * COM(N + D - M - 2, N - 1) % MOD) % MOD;
    ans = (ans + a1 - b1 - b2 + MOD * 2) % MOD;
  }
  cout << ans << "\n";
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define foreach(it, v)                                                         \
  for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second

typedef unsigned int uint;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef set<int> si;

const int MAX = 3010000;
const int MOD = 998244353;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int N, M;

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

  // 前処理
  COMinit();

  cin >> N >> M;

  ll ans = 0;
  for (int n = M % 2; n <= M; n += 2) {
    int D = (M * 3 - n) / 2;
    ll a1 = COM(N, n) * COM(N + D - 1, N - 1) % MOD;
    ll b1 = N * (COM(N - 1, n - 1) * COM(N + D - M - 1, N - 1) % MOD) % MOD;
    ll b2 = N * (COM(N - 1, n) * COM(N + D - M - 2, N - 1) % MOD) % MOD;
    ans = (ans + a1 - b1 - b2 + MOD * 2) % MOD;
  }
  cout << ans << "\n";
  return 0;
}