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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 998244353;
typedef long long LL;

// ----------- VECTOR ------------
template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}
// ----------- VECTOR ------------
//

LL modpow(LL a, LL b) {
  LL r = 1;
  while (b) {
    if (b % 2 == 1)
      r = r * a % kMod;
    a = a * a % kMod;
    b /= 2;
  }
  return r;
}

LL invmod(LL a) { return modpow(a, kMod - 2); }

class Comb {
public:
  Comb(int n) {
    fact.resize(n + 1);
    finv.resize(n + 1);
    inv.resize(n + 1);

    fact[0] = fact[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < n; i++) {
      fact[i] = fact[i - 1] * i % kMod;
      inv[i] = kMod - inv[kMod % i] * (kMod / i) % kMod;
      finv[i] = finv[i - 1] * inv[i] % kMod;
    }
  }

  LL nCr(int n, int r) const {
    LL numerator = fact[n];
    LL denominator = finv[r] * finv[n - r] % kMod;
    return (numerator * denominator) % kMod;
  }

private:
  vector<LL> fact;
  vector<LL> inv;
  vector<LL> finv;
};

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

  if (N == 1) {
    cout << M << endl;
    return 0;
  }

  vector<LL> f(N);
  f[0] = M;
  for (LL i = 1; i < N; ++i) {
    f[i] = (M - 1) * f[i - 1] % kMod;
  }

  Comb C(N);

  LL sum = 0;
  for (int k = 0; k <= K; ++k) {
    LL v = f[N - k - 1] % kMod;
    LL c = C.nCr(N - 1, k) % kMod;
    v = (v * c) % kMod;
    sum = (sum + v) % kMod;
  }
  cout << sum << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 998244353;
typedef long long LL;

// ----------- VECTOR ------------
template <typename T> istream &operator>>(istream &is, vector<T> &vs) {
  for (T &v : vs)
    is >> v;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vs) {
  for (const T &v : vs)
    os << v << " ";
  return os;
}
// ----------- VECTOR ------------
//

LL modpow(LL a, LL b) {
  LL r = 1;
  while (b) {
    if (b % 2 == 1)
      r = r * a % kMod;
    a = a * a % kMod;
    b /= 2;
  }
  return r;
}

LL invmod(LL a) { return modpow(a, kMod - 2); }

class Comb {
public:
  Comb(int n) {
    fact.resize(n + 1);
    finv.resize(n + 1);
    inv.resize(n + 1);
    fact[0] = fact[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < n; i++) {
      fact[i] = fact[i - 1] * i % kMod;
      inv[i] = kMod - inv[kMod % i] * (kMod / i) % kMod;
      finv[i] = finv[i - 1] * inv[i] % kMod;
    }
  }

  LL nCr(int n, int r) const {
    LL numerator = fact[n];
    LL denominator = finv[r] * finv[n - r] % kMod;
    return (numerator * denominator) % kMod;
  }

private:
  vector<LL> fact;
  vector<LL> inv;
  vector<LL> finv;
};

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

  if (N == 1) {
    cout << M << endl;
    return 0;
  }

  vector<LL> f(N);
  f[0] = M;
  for (LL i = 1; i < N; ++i) {
    f[i] = (M - 1) * f[i - 1] % kMod;
  }

  Comb C(M);

  LL sum = 0;
  for (int k = 0; k <= K; ++k) {
    LL v = f[N - k - 1] % kMod;
    LL c = C.nCr(N - 1, k) % kMod;
    v = (v * c) % kMod;
    sum = (sum + v) % kMod;
  }
  cout << sum << endl;
}