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

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; i++)
#define REPR(i, n) for (long long i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (long long i = m; i <= n; i++)
#define FORR(i, m, n) for (long long i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
const ll inf = 1e15;

const ll MOD = 998244353;
const ll mod2 = 998244353;

ll fac[201100], finv[201100], inv[201100];

void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 201100; 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;
  }
}

ll power(ll a, ll b) {
  return b ? power(a * a % mod2, b / 2) * (b % 2 ? a : 1) % mod2 : 1;
}

ll C(ll n, ll k) {
  ll x = 1, y = 1;
  for (int i = 1; i <= k; i++) {
    x = x * (n - i + 1) % mod2;
    y = y * i % mod2;
  }
  return x * power(y, mod2 - 2) % mod2;
}

int main() {
  FIN

      ll n,
      m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  init();
  REP(i, k + 1) {
    ans += (((m * fac[n - 1]) % MOD * finv[i]) % MOD * finv[n - 1 - i]) % MOD *
           power(m - 1, n - 1 - i);
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < n; i++)
#define REPR(i, n) for (long long i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (long long i = m; i <= n; i++)
#define FORR(i, m, n) for (long long i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(), (v).end()
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 1e15;

ll power(ll a, ll b) {
  return b ? power(a * a % mod2, b / 2) * (b % 2 ? a : 1) % mod2 : 1;
}

ll C(ll n, ll k) {
  ll x = 1, y = 1;
  for (int i = 1; i <= k; i++) {
    x = x * (n - i + 1) % mod2;
    y = y * i % mod2;
  }
  return x * power(y, mod2 - 2) % mod2;
}

int main() {
  FIN

      ll n,
      m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  REP(i, k + 1) {
    ll sum = m;
    sum *= power(m - 1, n - i - 1);
    sum %= mod2;
    sum *= C(n - 1, i);
    sum %= mod2;
    ans += sum;
    ans %= mod2;
  }
  cout << ans << endl;
  return 0;
}