Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 998244353
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int bin_exp(int b, int e) {
  if (e == 0)
    return 1;
  int a = bin_exp(b, e / 2);
  if (e & 1)
    return a * a * b;
  else
    return a * a;
}

int powm(int x, int y, int m = MOD) {
  x = x % m;
  int res = 1;
  while (y) {
    if (y & 1)
      res = res * x;
    res %= m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}

int modInverse(int a, int m = MOD) { return powm(a, m - 2, m); }

int fact[100005];
int nCr(int n, int r) {
  int res = (fact[n] * modInverse(fact[r])) % MOD;
  res = (res * modInverse(fact[(n - r)])) % MOD;
  return res;
}

int32_t main() {
  fastIO inp_out();
  int n, m, k;
  cin >> n >> m >> k;

  fact[0] = 1;
  for (int i = 1; i < n; ++i)
    fact[i] = (fact[i - 1] * i) % MOD;

  int fa = 0;
  for (int i = 0; i <= k; ++i) {
    int ans = nCr(n - 1, i);
    int bleft = n - i;
    ans *= m;
    ans %= MOD;
    ans *= powm(m - 1, bleft - 1);
    ans %= MOD;
    fa += ans;
    fa %= MOD;
  }

  cout << fa;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 998244353
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int bin_exp(int b, int e) {
  if (e == 0)
    return 1;
  int a = bin_exp(b, e / 2);
  if (e & 1)
    return a * a * b;
  else
    return a * a;
}

int powm(int x, int y, int m = MOD) {
  x = x % m;
  int res = 1;
  while (y) {
    if (y & 1)
      res = res * x;
    res %= m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}

int modInverse(int a, int m = MOD) { return powm(a, m - 2, m); }

int fact[200005];
int nCr(int n, int r) {
  int res = (fact[n] * modInverse(fact[r])) % MOD;
  res = (res * modInverse(fact[(n - r)])) % MOD;
  return res;
}

int32_t main() {
  fastIO inp_out();
  int n, m, k;
  cin >> n >> m >> k;

  fact[0] = 1;
  for (int i = 1; i < n; ++i)
    fact[i] = (fact[i - 1] * i) % MOD;

  int fa = 0;
  for (int i = 0; i <= k; ++i) {
    int ans = nCr(n - 1, i);
    int bleft = n - i;
    ans *= m;
    ans %= MOD;
    ans *= powm(m - 1, bleft - 1);
    ans %= MOD;
    fa += ans;
    fa %= MOD;
  }

  cout << fa;
  return 0;
}