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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define ll long long
// const int mod = 1000000007;
const int mod = 998244353;
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; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;
  vector<mint> dp(k + 1);
  for (int i = 1; i < k + 1; i++) {
    if (i == 1) {
      dp[i] = n - 1;
    } else {

      dp[i] = dp[i - 1] * (n - i) / i;
      // dp[i] = (dp[i - 1] * (n - i) / i) % mod;
    }
  }
  rep(i, k + 1) // 組みがi個になる時
  {
    mint count = 1;
    if (i == 0) {
      count *= m;
      count *= mint(m - 1).pow(n - 1); //(m-1)のn-1乗が欲しい
      ans += count;
      continue;
    }
    count *= m;
    count *= mint(m - 1).pow(n - 1 - i);
    // i個の選び方を(n-1)から考える。
    count *= dp[i]; //
    ans += count;
  }
  cout << ans.x << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define ll long long
// const int mod = 1000000007;
const int mod = 998244353;
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; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i, a) {
    x *= n - i;
    y *= i + 1;
  }
  return x / y;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;
  rep(i, k + 1) // 組みがi個になる時
  {
    mint count = 1;
    if (i == 0) {
      count *= m;
      count *= mint(m - 1).pow(n - 1); //(m-1)のn-1乗が欲しい
      ans += count;
      continue;
    }
    count *= m;
    count *= mint(m - 1).pow(n - 1 - i);
    // i個の選び方を(n-1)から考える。
    count *= choose(n - 1, i);
    ans += count;
  }
  cout << ans.x << endl;
}