Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
#define show(x) cout << #x << " is " << x << "\n";
typedef vector<ll> vi;
#define fi first
#define se second
const ll mod = 998244353;
const ll N = 1e5 + 1;
const ll inf = 1e18;

ll modfac[N];
ll modfacinv[N];
ll invsi[N];

ll power(ll x, ll y, ll M = inf) {
  ll ans = 1;
  x %= M;
  while (y) {
    if (y & 1)
      ans = (x * ans) % M;

    x = (x * x) % M;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2, mod); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c > mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

void simake() {
  invsi[0] = 1;
  invsi[1] = 1;

  modfac[0] = 1;
  modfac[1] = 1;

  modfacinv[0] = 1;
  modfacinv[1] = 1;

  for (ll i = 2; i < N; i++) {
    invsi[i] = mul(invsi[mod % i], (mod - (mod / i)));
    modfac[i] = mul(i, modfac[i - 1]);
    modfacinv[i] = mul(invsi[i], modfacinv[i - 1]);
  }
}

ll nCrm(ll n, ll r) {
  if (n == r || r == 0)
    return 1;
  if (n < r)
    return 0;
  return mul(mul(modfac[n], modfacinv[r]), modfacinv[n - r]);
}
// const ll N = 1e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

int main() {
  simake();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k, m;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll x = mul(power(m - 1, n - i - 1, mod), nCrm(n - 1, i));
    // cout<<x<<" ";
    ans = add(ans, x);
  }
  cout << (ans * m) % mod;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
#define show(x) cout << #x << " is " << x << "\n";
typedef vector<ll> vi;
#define fi first
#define se second
const ll mod = 998244353;
const ll N = 2e5 + 1;
const ll inf = 1e18;

ll modfac[N];
ll modfacinv[N];
ll invsi[N];

ll power(ll x, ll y, ll M = inf) {
  ll ans = 1;
  x %= M;
  while (y) {
    if (y & 1)
      ans = (x * ans) % M;

    x = (x * x) % M;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2, mod); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c > mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

void simake() {
  invsi[0] = 1;
  invsi[1] = 1;

  modfac[0] = 1;
  modfac[1] = 1;

  modfacinv[0] = 1;
  modfacinv[1] = 1;

  for (ll i = 2; i < N; i++) {
    invsi[i] = mul(invsi[mod % i], (mod - (mod / i)));
    modfac[i] = mul(i, modfac[i - 1]);
    modfacinv[i] = mul(invsi[i], modfacinv[i - 1]);
  }
}

ll nCrm(ll n, ll r) {
  if (n == r || r == 0)
    return 1;
  if (n < r)
    return 0;
  return mul(mul(modfac[n], modfacinv[r]), modfacinv[n - r]);
}
// const ll N = 1e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

int main() {
  simake();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n, k, m;
  cin >> n >> m >> k;
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll x = mul(power(m - 1, n - i - 1, mod), nCrm(n - 1, i));
    // cout<<x<<" ";
    ans = add(ans, x);
  }
  cout << (ans * m) % mod;

  return 0;
}