Problem p02965 (C++)

Buggy submission (status: Runtime Error):
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) {
  if (x > y)
    x = y;
}
template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) {
  if (x < y)
    x = y;
}
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 3000228;
const int Mod = 998244353;

int mul(int a, int b) { return (1LL * a * b) % Mod; }

int sum(int a, int b) { return (a + b) % Mod; }

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = mul(res, a);
    }
    b >>= 1;
    a = mul(a, a);
  }
  return res;
}

int n, m;
int fact[MAXN];
int rfact[MAXN];

int getc(int i, int j) { return mul(fact[i], mul(rfact[j], rfact[i - j])); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // read(FILENAME);
  cin >> n >> m;
  fact[0] = 1;
  for (int i = 1; i <= 3000000; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  rfact[3000000] = powm(fact[3000000], Mod - 2);
  for (int i = 2999999; i >= 0; i--) {
    rfact[i] = mul(rfact[i + 1], i + 1);
  }
  // n + 3 * m, n - 1
  int ans = getc(n + 3 * m - 1, n - 1);
  // cout << ans << endl;
  for (int value = 2 * m + 1; value <= 3 * m; value++) {
    int f = getc(n + 3 * m - value - 2, n - 2);
    f = mul(f, n);
    ans = sum(ans, Mod - f);
  }
  for (int cntodd = m + 1; cntodd <= n; cntodd++) {
    int f = getc(n, cntodd);
    int rest = 3 * m - cntodd;
    if (rest % 2 != 0) {
      continue;
    }
    int have = rest / 2;
    f = mul(f, getc(have + n - 1, n - 1));
    ans = sum(ans, Mod - f);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
/*
ЗАПУСКАЕМ
░ГУСЯ░▄▀▀▀▄░РАБОТЯГУ░░
▄███▀░◐░░░▌░░░░░░░
░░░░▌░░░░░▐░░░░░░░
░░░░▐░░░░░▐░░░░░░░
░░░░▌░░░░░▐▄▄░░░░░
░░░░▌░░░░▄▀▒▒▀▀▀▀▄
░░░▐░░░░▐▒▒▒▒▒▒▒▒▀▀▄
░░░▐░░░░▐▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░▀▄░░░░▀▄▒▒▒▒▒▒▒▒▒▒▀▄
░░░░░░▀▄▄▄▄▄█▄▄▄▄▄▄▄▄▄▄▄▀▄
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░░░▌▌░▌▌░░░░░
░░░░░░░░░▄▄▌▌▄▌▌░░░░░
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) {
  if (x > y)
    x = y;
}
template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) {
  if (x < y)
    x = y;
}
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left224
#define right right224
#define next next224
#define rank rank224
#define prev prev224
#define y1 y1224
#define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin)
#define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout)
#define files(FILENAME) read(FILENAME), write(FILENAME)
#define pb push_back
#define mp make_pair
const string FILENAME = "input";
const int MAXN = 3000228;
const int Mod = 998244353;

int mul(int a, int b) { return (1LL * a * b) % Mod; }

int sum(int a, int b) { return (a + b) % Mod; }

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = mul(res, a);
    }
    b >>= 1;
    a = mul(a, a);
  }
  return res;
}

int n, m;
int fact[MAXN];
int rfact[MAXN];

int getc(int i, int j) { return mul(fact[i], mul(rfact[j], rfact[i - j])); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // read(FILENAME);
  cin >> n >> m;
  fact[0] = 1;
  for (int i = 1; i <= 3000000; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  rfact[3000000] = powm(fact[3000000], Mod - 2);
  for (int i = 2999999; i >= 0; i--) {
    rfact[i] = mul(rfact[i + 1], i + 1);
  }
  // n + 3 * m, n - 1
  int ans = getc(n + 3 * m - 1, n - 1);
  // cout << ans << endl;
  for (int value = 2 * m + 1; value <= 3 * m; value++) {
    int f = getc(n + 3 * m - value - 2, n - 2);
    f = mul(f, n);
    ans = sum(ans, Mod - f);
  }
  for (int cntodd = m + 1; cntodd <= n; cntodd++) {
    int f = getc(n, cntodd);
    int rest = 3 * m - cntodd;
    if (rest % 2 != 0) {
      continue;
    }
    if (cntodd > 3 * m) {
      continue;
    }
    int have = rest / 2;
    f = mul(f, getc(have + n - 1, n - 1));
    ans = sum(ans, Mod - f);
  }
  cout << ans << endl;

  return 0;
}