Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#pragma GCC optimize("-O3")
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  cerr.tie(NULL);
#define tolower(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define toupper(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define ll long long int
ll max(ll a, ll b) { return a > b ? a : b; }
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return b == 0 ? a : gcd(b, a % b);
}
#define case(i) cout << "Case #" << i << ": ";
#define debug(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << '\n';                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ]";
  err(++it, args...);
}
#define pii pair<ll, ll>
#define pb push_back
#define PRECISION 10
const ll inf = 1e16;
const int MAXN = 100001;
const int LOGN = 22;
const int MAXBIT = 61;
const int DIGITSIZE = 19;
ll mod = 1e9 + 7;
ll MOD = 998244353;
int x4[] = {1, -1, 0, 0};
int y4[] = {0, 0, 1, -1};
bool take_test_cases = 0;
bool print_case_no = 0;
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
ll factorial[MAXN];
ll modinverse(ll b, ll m) {
  ll x, y;
  ll g = gcdExtended(b, m, &x, &y);
  if (g != 1)
    return -1;
  return (x % m + m) % m;
}
ll exponentMod(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = ((res % p) * (x % p)) % p;
    y = y >> 1;
    x = ((x % p) * (x % p)) % p;
  }
  return res % p;
}
ll ncrmodp(ll n, ll r, ll p) {
  if (r == 0) {
    return 1;
  } else {
    return (factorial[n] * modinverse(factorial[r], p) % p *
            modinverse(factorial[n - r], p) % p) %
           p;
  }
}
void getac(int T) {
  ll n, m, k;
  cin >> n >> m >> k;
  factorial[0] = 1;
  for (ll i = 1; i <= n; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= MOD;
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ans += ((m * (ncrmodp(n - 1, i, MOD)) % MOD) *
            (exponentMod(m - 1, n - 1 - i, MOD) % MOD)) %
           MOD;
    ans %= MOD;
  }
  cout << ans % MOD << '\n';
}
int main() {

  fastio;
  int t = 1;
  if (take_test_cases)
    cin >> t;
  int t_t = 1;
  while (t--) {
    if (print_case_no)
        case (t_t);
        getac(t_t);
        t_t++;
    }
    return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#pragma GCC optimize("-O3")
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);                                                              \
  cerr.tie(NULL);
#define tolower(s) transform(s.begin(), s.end(), s.begin(), ::tolower);
#define toupper(s) transform(s.begin(), s.end(), s.begin(), ::toupper);
#define ll long long int
ll max(ll a, ll b) { return a > b ? a : b; }
ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return b == 0 ? a : gcd(b, a % b);
}
#define case(i) cout << "Case #" << i << ": ";
#define debug(args...)                                                         \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << '\n';                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ]";
  err(++it, args...);
}
#define pii pair<ll, ll>
#define pb push_back
#define PRECISION 10
const ll inf = 1e16;
const int MAXN = 200001;
const int LOGN = 22;
const int MAXBIT = 61;
const int DIGITSIZE = 19;
ll mod = 1e9 + 7;
ll MOD = 998244353;
int x4[] = {1, -1, 0, 0};
int y4[] = {0, 0, 1, -1};
bool take_test_cases = 0;
bool print_case_no = 0;
ll gcdExtended(ll a, ll b, ll *x, ll *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
ll factorial[MAXN];
ll modinverse(ll b, ll m) {
  ll x, y;
  ll g = gcdExtended(b, m, &x, &y);
  if (g != 1)
    return -1;
  return (x % m + m) % m;
}
ll exponentMod(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = ((res % p) * (x % p)) % p;
    y = y >> 1;
    x = ((x % p) * (x % p)) % p;
  }
  return res % p;
}
ll ncrmodp(ll n, ll r, ll p) {
  if (r == 0) {
    return 1;
  } else {
    return (factorial[n] * modinverse(factorial[r], p) % p *
            modinverse(factorial[n - r], p) % p) %
           p;
  }
}
void getac(int T) {
  ll n, m, k;
  cin >> n >> m >> k;
  factorial[0] = 1;
  for (ll i = 1; i <= n; i++) {
    factorial[i] = factorial[i - 1] * i;
    factorial[i] %= MOD;
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ans += ((m * (ncrmodp(n - 1, i, MOD)) % MOD) *
            (exponentMod(m - 1, n - 1 - i, MOD) % MOD)) %
           MOD;
    ans %= MOD;
  }
  cout << ans % MOD << '\n';
}
int main() {

  fastio;
  int t = 1;
  if (take_test_cases)
    cin >> t;
  int t_t = 1;
  while (t--) {
    if (print_case_no)
        case (t_t);
        getac(t_t);
        t_t++;
    }
    return 0;
}