Problem p02965 (C++)

Buggy submission (status: Runtime Error):
// Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me

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

using LL = long long;

namespace _buff {

const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  if (ib == ie) {
    ib = ibuf;
    ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  }
  return ib == ie ? -1 : *ib++;
}

} // namespace _buff

LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}

const size_t N = 1.5e6 + 5;
const int MOD = 998244353;

int qpow(int base, int e) {
  int ret = 1;
  for (; e; e >>= 1) {
    if (e & 1) {
      ret = (LL)ret * base % MOD;
    }
    base = (LL)base * base % MOD;
  }
  return ret;
}

void ladd(int &ans, LL val) { ans = (ans + val) % MOD; }

int fac[N], inv[N];

void prep() {
  fac[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = (LL)fac[i - 1] * i % MOD;
  }
  inv[N - 1] = qpow(fac[N - 1], MOD - 2);
  for (int i = N - 1; i; --i) {
    inv[i - 1] = (LL)inv[i] * i % MOD;
  }
}

LL comb(int n, int m) {
  if (n < m)
    return 0;
  return (LL)fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}

LL calc(int n, int m) {
  // n split into the sum of m non-negative integers
  return comb(n + m - 1, m - 1);
}

int main() {
  prep();
  int n = read(), m = read();
  int ans = 0;
  for (int i = 0; i <= min(n, m); ++i) {
    if ((3 * m - i) & 1)
      continue;
    ladd(ans, comb(n, i) * calc((3 * m - i) / 2, n));
  }
  for (int i = 2 * m + 1; i <= 3 * m; ++i) {
    ladd(ans, -calc(3 * m - i, n - 1) * n);
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << '\n';
  return 0;
}

Fixed submission:
// Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me

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

using LL = long long;

namespace _buff {

const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  if (ib == ie) {
    ib = ibuf;
    ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  }
  return ib == ie ? -1 : *ib++;
}

} // namespace _buff

LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}

const size_t N = 6e6 + 5;
const int MOD = 998244353;

int qpow(int base, int e) {
  int ret = 1;
  for (; e; e >>= 1) {
    if (e & 1) {
      ret = (LL)ret * base % MOD;
    }
    base = (LL)base * base % MOD;
  }
  return ret;
}

void ladd(int &ans, LL val) { ans = (ans + val) % MOD; }

int fac[N], inv[N];

void prep() {
  fac[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = (LL)fac[i - 1] * i % MOD;
  }
  inv[N - 1] = qpow(fac[N - 1], MOD - 2);
  for (int i = N - 1; i; --i) {
    inv[i - 1] = (LL)inv[i] * i % MOD;
  }
}

LL comb(int n, int m) {
  if (n < m)
    return 0;
  return (LL)fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}

LL calc(int n, int m) {
  // n split into the sum of m non-negative integers
  return comb(n + m - 1, m - 1);
}

int main() {
  prep();
  int n = read(), m = read();
  int ans = 0;
  for (int i = 0; i <= min(n, m); ++i) {
    if ((3 * m - i) & 1)
      continue;
    ladd(ans, comb(n, i) * calc((3 * m - i) / 2, n));
  }
  for (int i = 2 * m + 1; i <= 3 * m; ++i) {
    ladd(ans, -calc(3 * m - i, n - 1) * n);
  }
  if (ans < 0)
    ans += MOD;
  cout << ans << '\n';
  return 0;
}