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

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 998244353;
void dbg() { cerr << "\\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T, class T2> void amax(T &a, T2 b) {
  if (a < b)
    a = b;
}

vector<int64_t> fct, inv;
void finit(int cmx, ll mod = MD) {
  fct.resize(cmx + 20);
  inv.resize(cmx + 20);
  fct[0] = fct[1] = inv[1] = inv[cmx] = 1;
  for (int i = 2; i < cmx + 3; i++)
    fct[i] = fct[i - 1] * i % mod;
  for (ll x = fct[cmx], n = mod - 2; n > 0; n >>= 1, x = x * x % mod)
    if (n & 1)
      inv[cmx] = inv[cmx] * x % mod;
  for (int i = cmx; i >= 1; i--)
    inv[i - 1] = inv[i] * i % mod;
}
ll nCk(ll n, ll k, ll mod = MD) {
  if (k > n || n < 0 || k < 0)
    return 0;
  ll ret = fct[n] * inv[k] % mod * inv[n - k] % mod;
  return ret;
}
ll bin_pow(ll x, ll n, ll mod = MD) {
  ll res = 1LL;
  for (; n > 0; n >>= 1, x = x * x % mod)
    if (n & 1)
      res = res * x % mod;
  return res;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  finit(n + 1000, MD);
  ll ans = 0;
  rep(i, k + 1) {
    ll sm = m * bin_pow(m - 1, n - i - 1) % MD * nCk(n - 1, i);
    (ans += sm) %= MD;
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 998244353;
void dbg() { cerr << "\\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}
template <class T, class T2> void amax(T &a, T2 b) {
  if (a < b)
    a = b;
}

vector<int64_t> fct, inv;
void finit(int cmx, ll mod = MD) {
  fct.resize(cmx + 20);
  inv.resize(cmx + 20);
  fct[0] = fct[1] = inv[1] = inv[cmx] = 1;
  for (int i = 2; i < cmx + 3; i++)
    fct[i] = fct[i - 1] * i % mod;
  for (ll x = fct[cmx], n = mod - 2; n > 0; n >>= 1, x = x * x % mod)
    if (n & 1)
      inv[cmx] = inv[cmx] * x % mod;
  for (int i = cmx; i >= 1; i--)
    inv[i - 1] = inv[i] * i % mod;
}
ll nCk(ll n, ll k, ll mod = MD) {
  if (k > n || n < 0 || k < 0)
    return 0;
  ll ret = fct[n] * inv[k] % mod * inv[n - k] % mod;
  return ret;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m, k;
  cin >> n >> m >> k;
  finit(n + 1000, MD);
  ll ans = 0;
  rep(i, k + 1) {
    ll sm = m;
    rep(j, n - i - 1)(sm *= m - 1) %= MD;
    sm *= nCk(n - 1, i);
    (ans += sm) %= MD;
  }
  cout << ans << "\\n";
  return 0;
}