Problem p02965 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  ni(a[i])
#define nal(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
const double pi = acos(-1);
const int MOD = 998244353;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
ll fac[MAXN], ifac[MAXN];

ll pw(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b & 1)
      r = (r * a) % MOD, b--;
    else
      a = (a * a) % MOD, b /= 2;
  }
  return r;
}

ll ncr(int n, int k) { return (fac[n] * ifac[k] % MOD) * ifac[n - k] % MOD; }

int main() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 1; i < MAXN; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
    ifac[i] = pw(fac[i], MOD - 2);
  }
  int n, m;
  scanf("%d %d", &n, &m);
  if (n == 2)
    return !pri(m + 1);
  ll ans = ncr(3 * m + n - 1, n - 1);
  for (int i = 2 * m + 1; i <= 3 * m; i++)
    ans = (ans - (ll)n * ncr(3 * m - i + n - 2, n - 2) % MOD + MOD) % MOD;
  for (int i = 0; i < m; i++)
    if (n >= 3 * m - 2 * i)
      ans = (ans - ncr(i + n - 1, n - 1) * ncr(n, 3 * m - i * 2) % MOD + MOD) %
            MOD;
  prl(ans);
  return 0;
}


Fixed submission:
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a, v) memset((a), (v), sizeof(a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  ni(a[i])
#define nal(a, n)                                                              \
  for (int i = 0; i < (n); i++)                                                \
  nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int, int, hash<int>> ht;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;
const double pi = acos(-1);
const int MOD = 998244353;
const int INF = 1e9 + 7;
const int MAXN = 3e6 + 5;
const double eps = 1e-9;
ll fac[MAXN], ifac[MAXN];

ll pw(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b & 1)
      r = (r * a) % MOD, b--;
    else
      a = (a * a) % MOD, b /= 2;
  }
  return r;
}

ll ncr(int n, int k) { return (fac[n] * ifac[k] % MOD) * ifac[n - k] % MOD; }

int main() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 1; i < MAXN; i++) {
    fac[i] = (fac[i - 1] * i) % MOD;
    ifac[i] = pw(fac[i], MOD - 2);
  }
  int n, m;
  scanf("%d %d", &n, &m);
  if (n == 2)
    return !pri(m + 1);
  ll ans = ncr(3 * m + n - 1, n - 1);
  for (int i = 2 * m + 1; i <= 3 * m; i++)
    ans = (ans - (ll)n * ncr(3 * m - i + n - 2, n - 2) % MOD + MOD) % MOD;
  for (int i = 0; i < m; i++)
    if (n >= 3 * m - 2 * i)
      ans = (ans - ncr(i + n - 1, n - 1) * ncr(n, 3 * m - i * 2) % MOD + MOD) %
            MOD;
  prl(ans);
  return 0;
}