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

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.endl(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 998244353 // prime;
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
}
// int size = 200001;  // e6 + 1
vector<ll> fac;  // n!(mod M)
vector<ll> ifac; // k!^{M-2} (mod M)
vector<ll> inv;  // k!^{M-2} (mod M)
////////////////////////////////////////////////
void fac_mod(int size) {
  // fac[i] = i! (mod M)
  fac[0] = 1;
  fac[1] = 1;
  REP(i, fac.size() - 1) { fac[i + 1] = fac[i] * (i + 1) % MOD; }
}
////////////////////////////////////////////////
void ifac_mod(int size) {
  // ifac[i] = 1/i! (mod M)
  ifac[0] = 1;
  ifac[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < ifac.size() - 1; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    ifac[i] = ifac[i - 1] * inv[i] % MOD;
  }
}
////////////////////////////////////////////////
ll nCk_mod(ll n, ll k) {
  // nCk (mod M)
  if (n == 0 && k == 0)
    return 1;
  if (n < k || k < 0)
    return 0;
  return fac[n] * (ifac[n - k] * ifac[k] % MOD) % MOD;
}
////////////////////////////////////////////////
ll pow_mod(ll x, int n) {
  // x^n (mod M)
  ll pow = 1;
  while (n != 0) {
    if (n & 1)
      pow = pow * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return pow;
}
////////////////////////////////////////////////
int main() {
  init();
  ll n, m, k;
  cin >> n >> m >> k;
  fac = vector<ll>((int)(n + 1), 0);
  ifac = vector<ll>((int)(n + 1), 0);
  inv = vector<ll>((int)(n + 1), 0);
  fac_mod((int)(n + 1));
  ifac_mod((int)(n + 1));
  vector<ll> mpow(n, 0);
  vector<ll> ncj(n, 0);
  REP(i, n) {
    mpow[i] = pow_mod((ll)(m - 1), i);
    ncj[i] = nCk_mod((n - 1), i);
  }
  ll ans = 0;
  REP(i, k + 1) {
    // cout << mpow[n - 1 - i] << " " << ncj[i] << " "
    //  << ((m * mpow[n - 1 - i]) % MOD * ncj[i]) % MOD << endl;
    ans += ((m * mpow[n - 1 - i]) % MOD * ncj[i]) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.endl(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 998244353 // prime;
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
}
// int size = 200001;  // e6 + 1
vector<ll> fac;  // n!(mod M)
vector<ll> ifac; // k!^{M-2} (mod M)
vector<ll> inv;  // k!^{M-2} (mod M)
////////////////////////////////////////////////
void fac_mod(int size) {
  // fac[i] = i! (mod M)
  fac[0] = 1;
  fac[1] = 1;
  REP(i, size) { fac[i + 1] = fac[i] * (i + 1) % MOD; }
}
////////////////////////////////////////////////
void ifac_mod(int size) {
  // ifac[i] = 1/i! (mod M)
  ifac[0] = 1;
  ifac[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < size; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    ifac[i] = ifac[i - 1] * inv[i] % MOD;
  }
}
////////////////////////////////////////////////
ll nCk_mod(ll n, ll k) {
  // nCk (mod M)
  if (n == 0 && k == 0)
    return 1;
  if (n < k || k < 0)
    return 0;
  return fac[n] * (ifac[n - k] * ifac[k] % MOD) % MOD;
}
////////////////////////////////////////////////
ll pow_mod(ll x, int n) {
  // x^n (mod M)
  ll pow = 1;
  while (n != 0) {
    if (n & 1)
      pow = pow * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return pow;
}
////////////////////////////////////////////////
int main() {
  init();
  ll n, m, k;
  cin >> n >> m >> k;
  fac = vector<ll>((int)(n + 1), 0);
  ifac = vector<ll>((int)(n + 1), 0);
  inv = vector<ll>((int)(n + 1), 0);
  fac_mod((int)(n + 1));
  ifac_mod((int)(n + 1));
  vector<ll> mpow(n, 0);
  vector<ll> ncj(n, 0);
  REP(i, n) {
    mpow[i] = pow_mod((ll)(m - 1), i);
    ncj[i] = nCk_mod((ll)(n - 1), (ll)i);
  }
  ll ans = 0;
  REP(i, k + 1) {
    // cout << mpow[n - 1 - i] << " " << ncj[i] << " "
    //  << ((m * mpow[n - 1 - i]) % MOD * ncj[i]) % MOD << endl;
    ans += ((m * mpow[n - 1 - i]) % MOD * ncj[i]) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}