Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
// #define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
const int MAX = 510000;
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 (ll 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(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  // 前処理
  COMinit();
  ll n, m;
  cin >> n >> m;
  ll ans = COM(3 * m + n - 1, n - 1);
  fr(i, m) {
    ans -= COM(n - 2 + i, n - 2) * n;
    ans %= MOD;
    if (ans < 0) {
      ans += MOD;
    }
  }
  for (ll i = m + 1; i <= min(3 * m, n); i++) {
    if ((3 * m - i) % 2 == 0) {
      ans -= COM(n, i) * COM((3 * m - i) / 2 + n - 1, n - 1);
      ans %= MOD;
      if (ans < 0) {
        ans += MOD;
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
// #define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define ALL(a) (a).begin(), (a).end()
const int MAX = 2510000;
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 (ll 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(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  // 前処理
  COMinit();
  ll n, m;
  cin >> n >> m;
  ll ans = COM(3 * m + n - 1, n - 1);
  fr(i, m) {
    ans -= COM(n - 2 + i, n - 2) * n;
    ans %= MOD;
    if (ans < 0) {
      ans += MOD;
    }
  }
  for (ll i = m + 1; i <= min(3 * m, n); i++) {
    if ((3 * m - i) % 2 == 0) {
      ans -= COM(n, i) * COM((3 * m - i) / 2 + n - 1, n - 1);
      ans %= MOD;
      if (ans < 0) {
        ans += MOD;
      }
    }
  }
  cout << ans << endl;
}