Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[8] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int mod = 998244353;
const int inf = mod * mod;
const int d5 = 100100;

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % 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; }

  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) {
    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);
  }

  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

using modint = ModInt<mod>;
modint f[d5];

void makef() {
  f[0] = 1;
  for (int i = 1; i < d5; i++)
    f[i] = f[i - 1] * i;
}

modint nCr(int n, int r) {
  if (n < r || r < 0)
    return 0;
  return f[n] / (f[r] * f[n - r]);
}

int beki(int a, int b) {
  a %= mod;
  if (b == 0)
    return 1;
  int ans = beki(a, b / 2);
  ans = ans * ans % mod;
  if (b % 2 == 1)
    ans = ans * a % mod;
  return ans;
}

int combination(int a, int b) {
  if ((a == b) || (b == 0))
    return 1;
  if (a < b)
    return 0;
  int ret = 1;
  for (int i = 0; i < b; i++) {
    ret *= (a - i);
    ret %= mod;
    ret *= beki(i + 1, mod - 2);
    ret %= mod;
  }
  return ret;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  makef();
  modint res = 0;
  for (int i = 0; i <= k; i++) {
    modint a = modint(m - 1).pow(n - i - 1);
    a *= nCr(n - 1, i);
    a *= m;
    res += a;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[8] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int mod = 998244353;
const int inf = mod * mod;
const int d5 = 200100;

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % 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; }

  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) {
    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);
  }

  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};

using modint = ModInt<mod>;
modint f[d5];

void makef() {
  f[0] = 1;
  for (int i = 1; i < d5; i++)
    f[i] = f[i - 1] * i;
}

modint nCr(int n, int r) {
  if (n < r || r < 0)
    return 0;
  return f[n] / (f[r] * f[n - r]);
}

int beki(int a, int b) {
  a %= mod;
  if (b == 0)
    return 1;
  int ans = beki(a, b / 2);
  ans = ans * ans % mod;
  if (b % 2 == 1)
    ans = ans * a % mod;
  return ans;
}

int combination(int a, int b) {
  if ((a == b) || (b == 0))
    return 1;
  if (a < b)
    return 0;
  int ret = 1;
  for (int i = 0; i < b; i++) {
    ret *= (a - i);
    ret %= mod;
    ret *= beki(i + 1, mod - 2);
    ret %= mod;
  }
  return ret;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  makef();
  modint res = 0;
  for (int i = 0; i <= k; i++) {
    modint a = modint(m - 1).pow(n - i - 1);
    a *= nCr(n - 1, i);
    a *= m;
    res += a;
  }
  cout << res << endl;
}