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

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int pri = 998244353;

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }

  ModInt pow(int e) const {
    long long a = 1, p = x;
    while (e > 0) {
      if (e % 2 == 0) {
        p = (p * p) % mod;
        e /= 2;
      } else {
        a = (a * p) % mod;
        e--;
      }
    }
    return ModInt(a);
  }
};

using modint = ModInt<pri>;
vector<modint> frac(1000000, 0);

modint ncr(int n, int r) { return frac[n] / frac[r] / frac[n - r]; }

int mod_pow(int a, int N) {
  if (N == 0)
    return 1;
  if (N % 2 == 1)
    return (a * mod_pow(a, N - 1)) % pri;
  int tmp = mod_pow(a, N / 2);
  return (tmp * tmp) % pri;
}
signed main() {
  frac[0] = 1;
  for (int i = 1; i < 200010; i++) {
    frac[i] = i;
    frac[i] *= frac[i - 1];
  }
  int N, M, K;
  cin >> N >> M >> K;
  modint ans = 0;
  for (int i = 0; i < K + 1; i++) {
    modint tmp = M;
    tmp *= mod_pow(M - 1, N - i - 1);
    tmp *= ncr(N - 1, i);
    ans += tmp;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int pri = 998244353;

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }

  ModInt pow(int e) const {
    long long a = 1, p = x;
    while (e > 0) {
      if (e % 2 == 0) {
        p = (p * p) % mod;
        e /= 2;
      } else {
        a = (a * p) % mod;
        e--;
      }
    }
    return ModInt(a);
  }
};

using modint = ModInt<pri>;

signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<modint>> dp(N + 100, vector<modint>(K + 100, 0));
  dp[0][0] = M;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      modint m = M - 1;
      dp[i + 1][j + 1] += dp[i][j];
      dp[i + 1][j] += m * dp[i][j];
    }
  }
  modint ans = 0;
  for (int i = 0; i <= K; i++)
    ans += dp[N - 1][i];
  cout << ans << endl;
}