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;
using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;
class mint {
  long long x;

public:
  mint(long long 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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
  mint choose(int n, int a) {
    mint x = 1, y = 1;
    for (int i = 0; i < a; ++i) {
      x *= n - i;
      y *= i + 1;
    }
    return x / y;
  }
  friend istream &operator>>(istream &is, mint &m) {
    is >> m.x;
    return is;
  }
  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

mint fact[200010];

mint choose2(int n, int a) { return fact[n] / (fact[n - a] * fact[a]); }

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;

  // 階乗を前計算
  fact[0] = 1;
  for (int i = 1; i < 200010; ++i) {
    fact[i] = fact[i - 1] * i;
  }

  for (int i = 0; i <= k; ++i) {
    ans += choose2(n - 1, i) * (mint)m * mint(m - 1).pow(n - i - 1);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;
class mint {
  long long x;

public:
  mint(long long 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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
  mint choose(int n, int a) {
    mint x = 1, y = 1;
    for (int i = 0; i < a; ++i) {
      x *= n - i;
      y *= i + 1;
    }
    return x / y;
  }
  friend istream &operator>>(istream &is, mint &m) {
    is >> m.x;
    return is;
  }
  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;
  for (int i = 0; i <= k; ++i) {
    ans += mint().choose(n - 1, i) * (mint)m * mint(m - 1).pow(n - i - 1);
  }
  cout << ans << endl;
}