Problem p02965 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

constexpr long long int mod = 998244353LL;
struct Factorial {
  std::vector<long long int> factorial, inversed_factorial;
  Factorial(const int max)
      : factorial(max + 1, 1), inversed_factorial(max + 1, 1) {
    std::vector<long long int> inv(max + 1, 1);
    for (auto i = 2; i <= max; ++i) {
      factorial[i] = (factorial[i - 1] * i) % mod;
      inv[i] = (mod - mod / i) * inv[mod % i] % mod;
      inversed_factorial[i] = inv[i] * inversed_factorial[i - 1] % mod;
    }
  }
  long long int fact(const int n) const { return factorial[n]; }
  long long int comb(const int n, const int r) const {
    return factorial[n] * inversed_factorial[r] % mod *
           inversed_factorial[n - r] % mod;
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  Factorial factorial(2000000);
  auto all = factorial.comb(n - 1 + 3 * m, 3 * m);
  auto over = factorial.comb(n + m - 2, m - 1) * n % mod;
  long long int odd = 0;
  for (auto i = m + 1; i <= n && i <= 3 * m; ++i) {
    if ((3 * m - i) % 2 == 0) {
      odd += factorial.comb(n - 1 + (3 * m - i) / 2, (3 * m - i) / 2) *
             factorial.comb(n, i) % mod;
      odd %= mod;
    }
  }
  std::cout << (all - over - odd + 2 * mod) % mod << std::endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

constexpr long long int mod = 998244353LL;
struct Factorial {
  std::vector<long long int> factorial, inversed_factorial;
  Factorial(const int max)
      : factorial(max + 1, 1), inversed_factorial(max + 1, 1) {
    std::vector<long long int> inv(max + 1, 1);
    for (auto i = 2; i <= max; ++i) {
      factorial[i] = (factorial[i - 1] * i) % mod;
      inv[i] = (mod - mod / i) * inv[mod % i] % mod;
      inversed_factorial[i] = inv[i] * inversed_factorial[i - 1] % mod;
    }
  }
  long long int fact(const int n) const { return factorial[n]; }
  long long int comb(const int n, const int r) const {
    return factorial[n] * inversed_factorial[r] % mod *
           inversed_factorial[n - r] % mod;
  }
};

int main() {
  int n, m;
  std::cin >> n >> m;
  Factorial factorial(n + 3 * m);
  auto all = factorial.comb(n - 1 + 3 * m, 3 * m);
  auto over = factorial.comb(n + m - 2, m - 1) * n % mod;
  long long int odd = 0;
  for (auto i = m + 1; i <= n && i <= 3 * m; ++i) {
    if ((3 * m - i) % 2 == 0) {
      odd += factorial.comb(n - 1 + (3 * m - i) / 2, (3 * m - i) / 2) *
             factorial.comb(n, i) % mod;
      odd %= mod;
    }
  }
  std::cout << (all - over - odd + 2 * mod) % mod << std::endl;
}