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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define mod 998244353
using ll = long long;
using namespace std;
int modPow(ll a, ll n, ll p) { // 繰り返し二乗法 a**n % p
  if (n == 1)
    return a % p;
  if (!n)
    return 1;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}
int main() {
  vector<ll> fact(200001), invfact(200001);
  fact[0] = 1;
  for (int i = 1; i <= 200000; i++) {
    fact[i] = fact[i - 1] * i % mod;
  }
  invfact[200000] = modPow(fact[200000], mod - 2, mod);
  for (int i = 200000 - 1; i >= 0; i--) {
    invfact[i] = invfact[i + 1] * (i + 1) % mod;
  }
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll num = m * modPow(m - 1, n - i - 1, mod) % mod;
    ll val = fact[n - 1] * (invfact[i] * invfact[n - i - 1] % mod) % mod;
    ans += num * val % mod;
    ans %= mod;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define mod 998244353
using ll = long long;
using namespace std;
int modPow(ll a, ll n, ll p) { // 繰り返し二乗法 a**n % p
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  ll t = modPow(a, n / 2, p);
  return (t * t) % p;
}
ll choose(ll n, ll k) {
  if (k < 1)
    return 1;
  ll num = 1;
  ll waru = 1;
  for (ll i = 0; i < k; i++) {
    num *= n - i;
    waru *= i + 1;
    num %= mod;
    waru %= mod;
  }
  waru = modPow(waru, mod - 2, mod);
  return num * waru % mod;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll num = m * modPow(m - 1, n - i - 1, mod) % mod;
    ll val = choose(n - 1, i);
    ans += num * val % mod;
    ans %= mod;
  }
  cout << ans << endl;
}