Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define ALL(x) (x).begin(), (x).end()
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 998244353;
const long long INF = 1LL << 60;
using P = pair<int, int>;

//// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
////nCr (mod MOD)を求める
const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
////

signed _main() {
  int N, M, K;
  cin >> N >> M >> K;
  COMinit();
  int ans = 0;
  rep(i, K + 1) {
    ans +=
        ((((COM(N - 1, i) % MOD) * M) % MOD) * modpow(M - 1, N - i - 1, MOD)) %
        MOD;
  }
  cout << ans % MOD << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define SZ(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, a) for (auto &i : a)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;
#define ALL(x) (x).begin(), (x).end()
signed _main();
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> using v = vector<T>;
template <class T> using vv = vector<v<T>>;
const int MOD = 998244353;
const long long INF = 1LL << 60;
using P = pair<int, int>;

// auto mod int
using ll = long long;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }

  mint comb(int r) { // O(r)で間に合うようなrのみ使用可能
    mint x = 1, y = 1;
    for (int i = 0; i <= r - 1; i++) {
      y *= *this - i;
      x *= i + 1;
    }
    return y / x;
  }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
/*
//使用例
mint ans=mint(2).pow(n)-1-mint(n).comb(a)-mint(n).comb(b);
cout << ans.x << endl;
*/

signed _main() {
  int N, M, K;
  cin >> N >> M >> K;
  mint ans = mint(0);
  rep(i, K + 1) {
    ans += mint(N - 1).comb(i) * mint(M) * mint(M - 1).pow(N - i - 1);
  }
  cout << ans.x << endl;
  return 0;
}