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

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using boost::multiprecision::cpp_int;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> lpll;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;

const ll LINF = LLONG_MAX;
// const ll MOD = 1e9+7;
const ll MOD = 998244353;

const ld PI = (acos(-1));
const ld EPS = 0.0000000001;

ll modPow(ll x,
          ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

struct Combination {
  vector<ll> fac, finv, inv;
  Combination(int MAX) {
    fac.resize(MAX);
    finv.resize(MAX);
    inv.resize(MAX);
  }
  void COMinit(int MAX) {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
      fac[i] = fac[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      finv[i] = finv[i - 1] * inv[i] % MOD;
    }
  }

  // 二項係数計算
  ll COM(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
  }
};

// cout << fixed << setprecision(10) << middle << endl;

int main() {
  // input, ready
  int N, M, K;
  cin >> N >> M >> K;

  // resolve
  if (N == 1) {
    cout << M << endl;
    return 0;
  }
  Combination cb(N);
  cb.COMinit(N);
  ll ans = 0;
  for (int i = 0; i <= K; i++) {
    ll ans_tmp = M;
    ans_tmp *= modPow(M - 1, N - 1 - i);
    ans_tmp %= MOD;
    ans_tmp *= cb.COM(N - 1, i);
    ans += ans_tmp;
    ans %= MOD;
  }

  // output
  cout << ans % MOD << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using boost::multiprecision::cpp_int;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ll, pair<ll, ll>> lpll;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;

const ll LINF = LLONG_MAX;
// const ll MOD = 1e9+7;
const ll MOD = 998244353;

const ld PI = (acos(-1));
const ld EPS = 0.0000000001;

ll modPow(ll a, ll n) {
  if (n == 1)
    return a % MOD;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1)) % MOD;
  ll t = modPow(a, n / 2);
  return (t * t) % MOD;
}

struct Combination {
  vector<ll> fac, finv, inv;
  Combination(int MAX) {
    fac.resize(MAX);
    finv.resize(MAX);
    inv.resize(MAX);
  }
  void COMinit(int MAX) {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
      fac[i] = fac[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      finv[i] = finv[i - 1] * inv[i] % MOD;
    }
  }

  // 二項係数計算
  ll COM(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
  }
};

// cout << fixed << setprecision(10) << middle << endl;

int main() {
  // input, ready
  int N, M, K;
  cin >> N >> M >> K;

  // resolve
  Combination cb(N + 1);
  cb.COMinit(N + 1);
  ll ans = 0;
  for (int i = 0; i <= K; i++) {
    ll ans_tmp = M;
    ans_tmp *= modPow(M - 1, N - 1 - i);
    ans_tmp %= MOD;
    ans_tmp *= cb.COM(N - 1, i);
    ans_tmp %= MOD;
    ans += ans_tmp;
    ans %= MOD;
  }

  // output
  cout << ans % MOD << endl;

  return 0;
}