Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

const int combination_max = 400000 + 1000;
const int combination_modulo = 998244353;
vector<int64_t> factorials(combination_max);
vector<int64_t> factorials_inv(combination_max);
vector<int64_t> inverse(combination_max);
int combination_mod(int n, int k);
int prepare_combination_mod();

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  int64_t total = 0;
  prepare_combination_mod();

  vector<int64_t> mminus1pow(n);
  mminus1pow[0] = 1;
  for (int i = 1; i < n; ++i) {
    mminus1pow[i] = mminus1pow[i - 1] * (m - 1) % combination_modulo;
  }

  for (int i = 0; i <= k; ++i) {
    int64_t add = m * mminus1pow[n - 1 - i] % combination_modulo;
    add *= combination_mod(n - 1, i);
    add %= combination_modulo;
    // fprintf(stderr,"%d %lld\\n",i,add);
    total += add;
    total %= combination_modulo;
  }
  cout << total << endl;
}

int prepare_combination_mod() {
  factorials[0] = 1;
  factorials[1] = 1;
  factorials_inv[0] = 1;
  factorials_inv[1] = 1;
  inverse[1] = 1;
  for (int i = 2; i < combination_max; ++i) {
    factorials[i] = factorials[i - 1] * i % combination_modulo;
    inverse[i] = combination_modulo - inverse[combination_modulo % i] *
                                          (combination_modulo / i) %
                                          combination_modulo;
    factorials_inv[i] = factorials_inv[i - 1] * inverse[i] % combination_modulo;
  }
  return 0;
}

int combination_mod(int n, int k) {
  return factorials[n] *
         (factorials_inv[k] * factorials_inv[n - k] % combination_modulo) %
         combination_modulo;
}


Error: 0

Fixed submission:

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

vector<int> primes = {2, 3, 5, 7};
int primes_extend(int calc_from, int calc_to);
int factoring(int n, map<int, int> &factors);

const int combination_max = 400000 + 1000;
const int combination_modulo = 998244353;
vector<int64_t> factorials(combination_max);
vector<int64_t> factorials_inv(combination_max);
vector<int64_t> inverse(combination_max);
int combination_mod(int n, int k);
int prepare_combination_mod();

int main() {
  int n, m, k;
  cin >> n >> m >> k;

  int64_t total = 0;
  prepare_combination_mod();

  vector<int64_t> mminus1pow(n);
  mminus1pow[0] = 1;
  for (int i = 1; i < n; ++i) {
    mminus1pow[i] = mminus1pow[i - 1] * (m - 1) % combination_modulo;
  }

  for (int i = 0; i <= k; ++i) {
    int64_t add = m * mminus1pow[n - 1 - i] % combination_modulo;
    add *= combination_mod(n - 1, i);
    add %= combination_modulo;
    // fprintf(stderr,"%d %lld\\n",i,add);
    total += add;
    total %= combination_modulo;
  }
  cout << total << endl;
}

int primes_extend(int calc_from, int calc_to) {

  for (int i = calc_from; i <= calc_to; ++i) {
    int j;
    int loop_max = sqrt(i);
    for (j = 0; (primes[j] <= loop_max) && (j < primes.size()); ++j) {
      if ((i % primes[j]) == 0) {
        // not prime...
        break;
      }
    }
    if ((j >= primes.size()) || (primes[j] > loop_max)) {
      primes.push_back(i);
    }
  }
  // fprintf(stderr,"count: %d\\n",primes.size());
}

int factoring(int n, map<int, int> &factors) {
  int maxfactor = sqrt(n);
  for (int i = 0; (n > 1) && (primes[i] <= maxfactor); ++i) {
    while ((n % primes[i]) == 0) {
      n /= primes[i];
      factors[primes[i]]++;
    }
  }
  if (n != 1) {
    factors[n]++;
  }

  return 0;
}

int prepare_combination_mod() {
  factorials[0] = 1;
  factorials[1] = 1;
  factorials_inv[0] = 1;
  factorials_inv[1] = 1;
  inverse[1] = 1;
  for (int i = 2; i < combination_max; ++i) {
    factorials[i] = factorials[i - 1] * i % combination_modulo;
    inverse[i] = combination_modulo - inverse[combination_modulo % i] *
                                          (combination_modulo / i) %
                                          combination_modulo;
    factorials_inv[i] = factorials_inv[i - 1] * inverse[i] % combination_modulo;
  }
}

int combination_mod(int n, int k) {
  return factorials[n] *
         (factorials_inv[k] * factorials_inv[n - k] % combination_modulo) %
         combination_modulo;
}