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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;

// auto mod int
// https://github.com/atcoder-live/library/blob/master/mint.cpp

// const int mod = 1000000007;
const int mod = 998244353;

struct mint {
  int64_t x;
  mint(int64_t 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(int64_t 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) { 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; }

// combination mod prime
// https://github.com/atcoder-live/library/blob/master/comb.cpp

struct combination {
  vector<mint> frac, ifrac;
  combination(int n) : frac(n + 1), ifrac(n + 1) {
    assert(n < mod);
    frac[0] = 1;
    for (int i = 1; i <= n; ++i)
      frac[i] = frac[i - 1] * i;
    ifrac[n] = frac[n].inv();
    for (int i = n; i >= 1; --i)
      ifrac[i - 1] = ifrac[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return frac[n] * ifrac[k] * ifrac[n - k];
  }
} c(200005);

int main() {
  ll n, m, k;
  cin >> n >> m >> k;

  mint ans = 0;
  mint col = m;
  for (int x = n - 1; x >= 0; --x) {
    if (x <= k) {
      mint now = col;
      now *= c(n - 1, x);
      ans += now;
    }
    col *= m - 1;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;

ll mod = 998244353LL;

ll n, m;
vector<ll> retu;

ll jj_(ll i, int x) {
  if (x == 1) {
    return i;
  }
  ll ans = 1;
  ans *= jj_(i, x / 2);
  ans *= ans;
  ans %= mod;
  if (x & 1) {
    ans *= i;
    ans %= mod;
  }

  return ans;
}

ll gyaku(ll i) { return jj_(i, mod - 2); }

ll jou(int i) {
  if (retu[i]) {
    return retu[i];
  }
  ll kk;
  kk = (jou(i - 1) * (m - 1)) % mod;
  return retu[i] = kk;
}

int main() {

  cin >> n >> m;
  ll k;
  cin >> k;

  retu.resize(n);
  retu[0] = 1LL;

  ll ans = 0LL;
  ll C = 1LL;
  rep(i, k + 1) {
    if (i) {
      C *= n - i;
      C %= mod;
      C *= gyaku(i);
      C %= mod;
    }
    ll v;
    v = C * m;
    v %= mod;
    v *= jou(n - i - 1);
    ans += v;
    ans %= mod;
  }

  cout << ans << '\\n';
}