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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;

#define INF 0x7f7f7f
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(i, x, n) for (int i = x; i <= n; i++)
// const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
// const double eps = 1e-6;
const ll mod = 998244353;

ll N, M, K;
ll f[maxn];
ll inv[maxn];

ll FPM(ll x, ll power) {
  ll ans = 1;
  while (power) {
    if (power & 1) {
      ans = (ans * x) % mod;
    }
    x = (x * x) % mod;
    power >>= 1;
  }
  return ans % mod;
}

ll C(ll a, ll b) { return f[a] * inv[a - b] % mod * inv[b] % mod; }

void solve() {
  cin >> N >> M >> K;
  ll m = max(N, M);
  f[0] = 1;
  for (int i = 1; i <= m; i++) // 阶乘预处理
    f[i] = f[i - 1] * i % mod;
  inv[m] = FPM(f[m], mod - 2);
  for (ll i = m - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod; // 求逆元
  ll ans = 0;
  ll cnt = FPM(M - 1, N - K - 1);
  for (ll i = N - K - 1; i <= N - 1; i++) {
    ans = (ans + C(N - 1, i) % mod * M % mod * cnt % mod) % mod;
    cnt = cnt * (M - 1) % mod;
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  // cin.tie(nullptr);
  // cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#else
  ios::sync_with_stdio(false);
  int T = 1;
  // cin >> T;
  while (T--)
    solve();
#endif
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, l, r) for (int i = l, i##end = r; i <= i##end; ++i)
#define per(i, l, r) for (int i = l, i##end = r; i >= i##end; --i)
#define repo(i, l, r) for (int i = l, i##end = r; i < i##end; ++i)
#define pero(i, l, r) for (int i = l, i##end = r; i > i##end; --i)
#define debug(x) cerr << "debug(" << #x << ')' << ": " << x << " " << endl
typedef long long ll;
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int MAXN = 1 * 1e5 + 5;
const int mod = 998244353;
const double PI = acos(-1);
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
// int cmpfunc (const void * a, const void * b){return ( *(int*)a - *(int*)b );}
// rise; qsort (a, n, sizeof(int), cmpfunc);

/*struct cmp{bool operator( ) (int x, int y) const {return a[x] + b[x] > a[y] +
 * b[y];}};*/

ll inv[MAXN];
ll f[MAXN];

ll ksm(ll ans, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = res * ans % mod;
    p >>= 1;
    ans = ans * ans % mod;
  }
  return res;
}
ll c(ll a, ll b) { return f[a] * inv[a - b] % mod * inv[b] % mod; }

void solve() {
  ll n, m, k;
  cin >> n >> m >> k;

  int maxn = max(n, m);
  f[0] = 1;
  for (ll i = 1; i <= maxn; i++)
    f[i] = f[i - 1] * i % mod; // 求i!
  inv[maxn] = ksm(f[maxn], mod - 2);
  for (ll i = maxn - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % mod; // 求逆元

  ll ans = 0; // C(n-1,n-1-i)*m*(m-1)^(n-1-i) //n-1-i为隔板数
  ll tem = ksm(m - 1, n - 1 - k);
  for (int i = n - 1 - k; i <= n - 1; i++) {
    ans = ans + c(n - 1, i) % mod * m % mod * tem % mod;
    ans = ans % mod;
    tem = tem * (m - 1) % mod;
  }
  cout << ans;
}

int main() {
#ifdef ACM_LOCAL
  freopen("in.txt", "r", stdin);
#endif
  //	int T; cin >> T;
  //	while(T--)
  solve();
  return 0;
}