alexjercan/bugnet huggingface dataset:
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

const int kFacMax = 2 * 100000;

long long fac[kFacMax];
long long finv[kFacMax];
long long inv[kFacMax];

void combi_prepare(int mod) {
  fac[0] = 1;
  fac[1] = 1;
  finv[0] = 1;
  finv[1] = 1;
  inv[1] = 1;

  for (int i = 2; i < kFacMax; 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;
  }
}

long long combi(int n, int k, long long mod) {
  if (n < 0 || k < 0 || n < k)
    return 0;

  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int main() {
  const int MOD = 998244353;
  int N, M, K;
  cin >> N >> M >> K;

  // i == 0, always M patterns
  // choose 1..K pairs in N-1
  long long num = M;
  rep(i, N - 1 - K) {
    num *= M - 1;
    num %= MOD;
  }

  combi_prepare(MOD);

  long long sum = 0;
  for (int i = K; i >= 0; i--) {
    long long tmp = num * combi(N - 1, i, MOD);
    tmp %= MOD;
    sum += tmp;
    sum %= MOD;

    num *= M - 1;
    num %= MOD;
  }

  cout << sum << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main() {
  const int MOD = 998244353;
  int N, M, K;
  cin >> N >> M >> K;

  // i == 0, always M patterns
  // i > 0, M - 1 or M
  // choose K pairs of M patterns in i > 0, N-1 blocks;

#if 0
  vector<vector<long long>> dp(N);
  rep(i, N) {
    dp[i].resize(K+1);
  }
  
  dp[0][0] = M;
  rep(i, N-1) {
    rep(j, K+1) {
      dp[i+1][j] += dp[i][j] * (M-1);  // different color
      dp[i+1][j] %= MOD;

      if (j+1 <= K) {
        dp[i+1][j+1] += dp[i][j] * 1;  // same color
        dp[i+1][j+1] %= MOD;
      }
    }
  }

  long long sum = 0;
  rep(i, K+1) {
    sum += dp[N-1][i];
    sum %= MOD;
  }
#endif

  long long num = M;
  rep(i, N - 1) {
    num *= M - 1;
    num %= MOD;
  }
  long long sum = num;

  long long combo = 1;
  for (int k = 1; k <= K; k++) {
    num /= M - 1;

    combo *= N - 1 + 1 - k;
    combo /= k;

    num *= combo;
    num %= MOD;

    sum += num;
    sum %= MOD;
  }

  cout << sum << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

//============================================================================
// Name        : e.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll modPow(long long a, long long n, long long p) {
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll def = 998244353;
  if (m == 1) {
    if (k == n - 1) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }
  if (n == 1) {
    cout << m << endl;
    return 0;
  }
  vector<ll> c(n);
  c[0] = 1;
  c[1] = n - 1;
  //	cout<<'A'<<endl;
  for (int i = 2; i <= n - 1; i++) {

    c[i] = c[i - 1] * (n - 1 - i + 1);
    c[i] %= def;
    //		cout<<c[i]<<endl;
    c[i] = c[i] * modPow(i, def - 2, def);
    c[i] %= def;
    //		cout<<c[i]<<endl;
  }
  ll ans = 0;
  //	cout<<"B"<<endl;
  for (int i = 0; i <= k; i++) {
    ll add = 0;
    if (i == n - 1) {
      add = m;
      ans += m;
      ans %= def;
      break;
    }
    add = modPow(m - 1, n - i - 1, def);
    add *= m;
    add %= def;
    add *= c[i];
    add %= def;
    ans += add;
    ans %= def;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

//============================================================================
// Name        : e.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll modPow(long long a, long long n, long long p) {
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll def = 998244353;
  if (m == 1) {
    if (k == n - 1) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }
  if (n == 1) {
    cout << m << endl;
    return 0;
  }
  vector<ll> c(n);
  c[0] = 1;
  c[1] = n - 1;
  for (int i = 2; i <= n - 1; i++) {

    c[i] = c[i - 1] * (n - 1 - i + 1);
    c[i] %= def;
    //		cout<<c[i]<<endl;
    c[i] = c[i] * modPow(i, def - 2, def);
    c[i] %= def;
    //		cout<<c[i]<<endl;
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll add = 0;
    add = modPow(m - 1, n - i - 1, def);
    add *= m;
    add %= def;
    add *= c[i];
    add %= def;
    ans += add;
    ans %= def;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;
const int MAX_N = 2000010;

int64_t fa[MAX_N]; // n! mod MOD
int64_t fb[MAX_N]; // n!^(MOD - 2) mod MOD ≡ 1 / n!

int64_t modpow(int64_t base, int64_t k, int64_t mod) {
  base %= mod;
  int64_t answer = 1;
  while (k > 0) {
    if (k & 1)
      answer = (answer * base) % mod;
    base = (base * base) % mod;
    k >>= 1;
  }
  return answer;
}

// nCk =  n! / (k! * (n - k)!)
//     ≡ n! * k!^(MOD-2) * (n-k)!^(MOD-2) mod MOD
int64_t nCk(int64_t n, int64_t k, int64_t mod) {
  if (n == 0 || k == 0)
    return 1;
  if (n < 0 || k < 0)
    return 0;
  int64_t answer = fa[n];
  answer = answer * fb[k] % mod;
  answer = answer * fb[n - k] % mod;
  return answer;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  N--;

  fa[0] = 1;
  fb[0] = 1;
  for (int i = 0; i + 1 < MAX_N; i++) {
    fa[i + 1] = fa[i] * (i + 1) % MOD;
    fb[i + 1] = fb[i] * modpow(i + 1, MOD - 2, MOD) % MOD;
  }

  int64_t answer = 0;
  for (int k = 0; k <= K; k++) {
    int64_t tmp = (M * nCk(N, k, MOD)) % MOD;
    tmp = tmp * modpow(M - 1, N - k, MOD) % MOD;
    answer = (answer + tmp) % MOD;
  }

  cout << answer % MOD << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAX_M = 200010;
const int MOD = 998244353;
const int64_t MAX_DIV_THRESH = 1LL << 44;

int64_t dp[MAX_M * 3];

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  memset(dp, 0, sizeof(dp));

  int offset = MAX_M * 2;
  dp[offset - 0] = M;
  for (int i = 1; i < N; i++) {
    for (int b = 0; b <= K; b++) {
      dp[offset - 1 + b] += dp[offset + b] * (M - 1);
      if (dp[offset - 1 + b] > MAX_DIV_THRESH)
        dp[offset - 1 + b] %= MOD;
    }

    offset--;
  }

  int64_t answer = 0;
  for (int i = 0; i <= K; i++) {
    answer += dp[offset + i];
  }
  cout << answer % MOD << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;

int64_t M = 998244353;
vector<int64_t> fac(200001);  // n! (mod M)
vector<int64_t> ifac(200001); // k!^{M-2} (mod M)

int64_t mpow(int64_t x, int64_t n) {
  int64_t ans = 1;
  while (n != 0) {
    if (n & 1) {
      ans = ans * x % M;
    }
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

int64_t comb(int64_t n, int64_t k) {
  if (n == 0 && k == 0) {
    return 1;
  } else if (n < k || n < 0) {
    return 0;
  }
  int64_t temp = ifac.at(k) * ifac.at(n - k) % M;
  return temp * fac.at(n) % M;
}

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

  fac.at(0) = 1;
  ifac.at(0) = 1;
  for (int64_t i = 0; i < n; i++) {
    fac.at(i + 1) = fac.at(i) * (i + 1) % M;
    ifac.at(i + 1) = ifac.at(i) * mpow(i + 1, M - 2) % M;
  }

  int64_t ans = 0;
  for (int i = 0; i <= k; i++) {
    int64_t temp = m;
    temp = temp * comb(n - 1, i) % M;
    temp = temp * mpow(m - 1, n - 1 - i) % M;
    ans = (ans + temp) % M;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

int64_t M = 998244353;
vector<int64_t> fac(100001);  // n! (mod M)
vector<int64_t> ifac(100001); // k!^{M-2} (mod M)

int64_t mpow(int64_t x, int64_t n) {
  int64_t ans = 1;
  while (n != 0) {
    if (n & 1) {
      ans = ans * x % M;
    }
    x = x * x % M;
    n = n >> 1;
  }
  return ans;
}

int64_t comb(int64_t n, int64_t k) {
  if (n == 0 && k == 0) {
    return 1;
  } else if (n < k || n < 0) {
    return 0;
  }
  int64_t temp = ifac.at(k) * ifac.at(n - k) % M;
  return temp * fac.at(n) % M;
}

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

  fac.at(0) = 1;
  ifac.at(0) = 1;
  for (int64_t i = 0; i < n; i++) {
    fac.at(i + 1) = fac.at(i) * (i + 1) % M;
    ifac.at(i + 1) = ifac.at(i) * mpow(i + 1, M - 2) % M;
  }

  int64_t ans = 0;
  for (int i = 0; i <= k; i++) {
    int64_t temp = m;
    temp = temp * comb(n - 1, i) % M;
    temp = temp * mpow(m - 1, n - 1 - i) % M;
    ans = (ans + temp) % M;
  }

  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *  author: MOOONI
 *  hackers are welcome!
 **/
#include <bits/stdc++.h>

#define debug(x) cerr << #x << " = " << x << endl

using namespace std;

const int md = 998244353;

inline void add(int &a, int b) {
  a += b;
  while (a < 0) {
    a += md;
  }
  a %= md;
}

inline int mul(int a, int b) { return (int)((long long)a * b % md); }

inline int power(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) { return power(a, md - 2); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> fact(n + 1);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  vector<int> inv_fact(n + 1);
  inv_fact[n] = inv(fact[n]);
  for (int i = n - 1; i >= 0; i--) {
    inv_fact[i] = mul(inv_fact[i + 1], i + 1);
  }
  auto C = [&](int n, int k) {
    if (k < 0 || k > n) {
      return 0;
    }
    return mul(fact[n], mul(inv_fact[k], inv_fact[n - k]));
  };
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    add(ans, mul(C(n - 1, i), mul(m, power(m - 1, n - i - 1))));
  }
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

/**
 *  author: MOOONI
 *  hackers are welcome!
 **/
#include <bits/stdc++.h>

#define debug(x) cerr << #x << " = " << x << endl

using namespace std;

const int md = 998244353;

inline int add(int &a, int b) {
  a += b;
  while (a < 0) {
    a += md;
  }
  a %= md;
}

inline int mul(int a, int b) { return (int)((long long)a * b % md); }

inline int power(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) { return power(a, md - 2); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> fact(n + 1);
  fact[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
  vector<int> inv_fact(n + 1);
  inv_fact[n] = inv(fact[n]);
  for (int i = n - 1; i >= 0; i--) {
    inv_fact[i] = mul(inv_fact[i + 1], i + 1);
  }
  auto C = [&](int n, int k) {
    if (k < 0 || k > n) {
      return 0;
    }
    return mul(fact[n], mul(inv_fact[k], inv_fact[n - k]));
  };
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    add(ans, mul(C(n - 1, i), mul(m, power(m - 1, n - i - 1))));
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;
ll const MOD = 998244353;
const ll MAX = 400001;
ll fac[MAX], finv[MAX], inv[MAX];

ll mod_pow(ll a, ll n, ll p) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (mod_pow(a, 1, p) * mod_pow(a, n - 1, p)) % p;
  ll t = mod_pow(a, n / 2, p);
  return (t * t) % p;
}

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

long long comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

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

  comb_init();
  ll ans = 0;
  rep(i, k + 1) {
    ll tmp = mod_pow(m - 1, n - 1 - i, MOD) % MOD;
    tmp = tmp * comb(n - 1, i) % MOD;

    ans += tmp;
    ans %= MOD;
  }

  cout << ans * m % MOD << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
ll const INF = 1ll << 61;
double pi = 3.141592653589793238;
ll const MOD = 998244353;
const ll MAX = 400001;
long long fac[MAX], finv[MAX], inv[MAX];

ll mod_pow(ll a, ll n, ll p) {
  if (n == 1)
    return a % p;
  if (n % 2 == 1)
    return (mod_pow(a, 1, p) * mod_pow(a, n - 1, p)) % p;
  ll t = mod_pow(a, n / 2, p);
  return (t * t) % p;
}

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

long long comb(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

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

  comb_init();
  ll ans = 0;
  rep(i, k + 1) {
    ll tmp = mod_pow(m - 1, n - 1 - i, MOD) % MOD;
    tmp = tmp * comb(n - 1, i) % MOD;

    ans += tmp;
    ans %= MOD;
  }

  cout << ans % MOD * m % MOD << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

// g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

ll MODP = 998244353;

ll gen(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x % MODP;
  if (y % 2 == 1) {
    return ((x % MODP) * (gen(x, y - 1)) % MODP) % MODP;
  } else {
    ll t = gen(x, y / 2);
    return ((t % MODP) * (t % MODP)) % MODP;
  }
}

ll comb(ll x, ll y) {
  if (y == 0)
    return 1;
  ll res = 1;
  rep(i, y) { res = ((res % MODP) * ((x - i) % MODP)) % MODP; }
  for (ll i = 1; i <= y; i++) {
    res = ((res % MODP) * gen(i, MODP - 2)) % MODP;
  }
  return res % MODP;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;

  vector<ll> com(N, 1);
  com[0] = 1;
  com[1] = N - 1;
  for (ll i = 2; i <= N - 1; i++) {
    com[i] =
        ((com[i - 1] % MODP) * (((N - i) % MODP) * (gen(i, MODP - 2)) % MODP)) %
        MODP;
  }

  ll ans = 0;
  for (ll i = 0; i <= K; i++) {
    ll add = M;
    add = (add * com[i]) % MODP;
    add = ((add % MODP) * (gen(M - 1, N - i - 1) % MODP)) % MODP;
    ans = (ans % MODP + add % MODP) % MODP;
  }
  if (ans < 0)
    cout << ans + MODP << endl;
  else
    cout << ans % MODP << endl;

  return 0;
}


Error: TLE

Fixed submission:

// g++ -std=gnu++14 a.cpp

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

ll MODP = 998244353;

ll gen(ll x, ll y) {
  if (y == 0)
    return 1;
  if (y == 1)
    return x % MODP;
  if (y % 2 == 1) {
    return ((x % MODP) * (gen(x, y - 1)) % MODP) % MODP;
  } else {
    ll t = gen(x, y / 2);
    return ((t % MODP) * (t % MODP)) % MODP;
  }
}

ll comb(ll x, ll y) {
  if (y == 0)
    return 1;
  ll res = 1;
  rep(i, y) { res = ((res % MODP) * ((x - i) % MODP)) % MODP; }
  for (ll i = 1; i <= y; i++) {
    res = ((res % MODP) * gen(i, MODP - 2)) % MODP;
  }
  return res % MODP;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;

  ll ans = 0;
  for (ll i = 0; i <= K; i++) {
    ll add = (M * comb(N - 1, i)) % MODP;
    add = (add * gen(M - 1, N - i - 1)) % MODP;
    ans = (ans + add) % MODP;
  }

  cout << ans % MODP << endl;

  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1LL << 60;
ll mod_pow(ll, ll, ll);
ll mod_fact(ll, ll);
ll mod_inv(ll, ll);
ll gcd(ll, ll);
ll lcm(ll, ll);
//
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  const ll con = mod_fact(n - 1, MOD);
  ll fact[n];
  fact[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = fact[i - 1] * i % MOD;
  }
  for (int i = 0; i <= k; i++) {
    const ll bino = fact[n - 1] * mod_inv(fact[i], MOD) % MOD *
                    mod_inv(fact[n - i - 1], MOD) % MOD;
    ans = (ans + bino * m % MOD * mod_pow(m - 1, n - i - 1, MOD)) % MOD;
  }
  cout << ans << "\\n";
}

/*⠀⠀⠀⠀⠀⢀⣀⣀⣀
⠀⠀⠀⠰⡿⠿⠛⠛⠻⠿⣷
⠀⠀⠀⠀⠀⠀⣀⣄⡀⠀⠀⠀⠀⢀⣀⣀⣤⣄⣀⡀
⠀⠀⠀⠀⠀⢸⣿⣿⣷⠀⠀⠀⠀⠛⠛⣿⣿⣿⡛⠿⠷
⠀⠀⠀⠀⠀⠘⠿⠿⠋⠀⠀⠀⠀⠀⠀⣿⣿⣿⠇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁

⠀⠀⠀⠀⣿⣷⣄⠀⢶⣶⣷⣶⣶⣤⣀
⠀⠀⠀⠀⣿⣿⣿⠀⠀⠀⠀⠀⠈⠙⠻⠗
⠀⠀⠀⣰⣿⣿⣿⠀⠀⠀⠀⢀⣀⣠⣤⣴⣶⡄
⠀⣠⣾⣿⣿⣿⣥⣶⣶⣿⣿⣿⣿⣿⠿⠿⠛⠃
⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄
⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡁
⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁
⠀⠀⠛⢿⣿⣿⣿⣿⣿⣿⡿⠟
⠀⠀⠀⠀⠀⠉⠉⠉
*/

// library//
ll mod_fact(ll a, ll mod) {
  ll ans = 1;
  while (a > 1) {
    ans = ans * a % mod;
    a--;
  }
  return ans;
}
ll mod_pow(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
ll mod_inv(ll a, ll mod) { return mod_pow(a, mod - 2, mod); }

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1LL << 60;
ll mod_pow(ll, ll, ll);
ll mod_fact(ll, ll);
ll mod_inv(ll, ll);
ll gcd(ll, ll);
ll lcm(ll, ll);
//
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0;
  const ll con = mod_fact(n - 1, MOD);
  for (int i = 0; i <= k; i++) {
    const ll bino = con * mod_inv(mod_fact(i, MOD), MOD) % MOD *
                    mod_inv(mod_fact(n - 1 - i, MOD), MOD) % MOD;
    ans = (ans + bino * m % MOD * mod_pow(m - 1, n - i - 1, MOD)) % MOD;
  }
  cout << ans << "\\n";
}

/*⠀⠀⠀⠀⠀⢀⣀⣀⣀
⠀⠀⠀⠰⡿⠿⠛⠛⠻⠿⣷
⠀⠀⠀⠀⠀⠀⣀⣄⡀⠀⠀⠀⠀⢀⣀⣀⣤⣄⣀⡀
⠀⠀⠀⠀⠀⢸⣿⣿⣷⠀⠀⠀⠀⠛⠛⣿⣿⣿⡛⠿⠷
⠀⠀⠀⠀⠀⠘⠿⠿⠋⠀⠀⠀⠀⠀⠀⣿⣿⣿⠇
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠁

⠀⠀⠀⠀⣿⣷⣄⠀⢶⣶⣷⣶⣶⣤⣀
⠀⠀⠀⠀⣿⣿⣿⠀⠀⠀⠀⠀⠈⠙⠻⠗
⠀⠀⠀⣰⣿⣿⣿⠀⠀⠀⠀⢀⣀⣠⣤⣴⣶⡄
⠀⣠⣾⣿⣿⣿⣥⣶⣶⣿⣿⣿⣿⣿⠿⠿⠛⠃
⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡄
⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡁
⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁
⠀⠀⠛⢿⣿⣿⣿⣿⣿⣿⡿⠟
⠀⠀⠀⠀⠀⠉⠉⠉
*/

// library//
ll mod_fact(ll a, ll mod) {
  ll ans = 1;
  while (a > 1) {
    ans = ans * a % mod;
    a--;
  }
  return ans;
}
ll mod_pow(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
ll mod_inv(ll a, ll mod) { return mod_pow(a, mod - 2, mod); }
ll gcd(ll a, ll b) {
  while (a % b) {
    return gcd(b, a % b);
  }
  return b;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int64_t i = 0; i <= n; i++)
#define rep1(i, n) for (int64_t i = 1; i <= n; i++)
typedef int64_t lint;

const lint mod = 998244353;
struct mint {
  lint x;
  mint(lint 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(lint 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, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

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

int main() {
  lint N, M, K;
  cin >> N >> M >> K;
  mint a = M, b = 0, c = 0, x, y, z;
  vector<mint> v(200002);
  rep1(i, N) {
    v.at(i) = a;
    a *= M - 1;
  }
  combination C(200002);
  rep(i, K) {
    b = C(N - 1, i);
    c += v.at(N - i) * b;
  }
  cout << c << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const double PI = 3.14159265358979323846;
const long long INF = 1LL << 60;

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  int64_t a = 0, b = 0, c = 0, x, y, z;
  int64_t mod = 998244353;
  vector<int64_t> v(K + 2);

  v.at(0) = 1;
  rep(i, N - 1) {
    for (int j = K; j >= 0; j--) {
      v.at(j + 1) += v.at(j);
      v.at(j) *= M - 1;
      v.at(j) %= mod;
    }
  }
  rep(i, K + 1) {
    a += v.at(i);
    a %= mod;
  }
  a *= M;
  a %= mod;
  std::cout << a << '\\n';
}