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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
const int MAX = 1000000;
const int MOD = 998244353;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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() {
  long n, m, k;
  cin >> n >> m >> k;
  COMinit();
  long ans = 0;
  long l = m;
  int p = n - k - 1;
  while (p--) {
    l = l * (m - 1) % MOD;
  }
  for (int i = k; i >= 0; i--) {
    long now = COM(n - 1, i);
    now = now * l % MOD;
    l = l * (m - 1) % MOD;
    ans = (ans + now) % MOD;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
int main() {
  long mod = 998244353;
  long n, m, k;
  cin >> n >> m >> k;
  vector<vector<long>> dp(n + 1, vector<long>(k + 1));
  rep(i, k + 1) { dp[0][i] = 1; }
  rep2(i, 1, n + 1) {
    rep(j, k + 1) {
      if (j > 0) {
        if (i > 1) {
          dp[i][j] = dp[i - 1][j] * (m - 1) + dp[i - 1][j - 1];
          dp[i][j] %= mod;
        }
      } else {
        if (i != 1) {
          dp[i][j] = dp[i - 1][j] * (m - 1);
          dp[i][j] %= mod;
        } else {
          dp[i][j] = dp[i - 1][j] * m;
          dp[i][j] %= mod;
        }
      }
    }
  }
  long ans = 0;
  rep(i, k + 1) {
    ans += dp[n][i];
    ans %= mod;
  }

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

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

using ll = long long;

constexpr ll MOD = 998244353;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

vector<ll> fac(300001);  // n!(mod M)
vector<ll> ifac(300001); // k!^{M-2} (mod M)

ll mpow(ll x, ll n) { // x^n(mod
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbmod
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void init() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 300000; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD; //
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) % MOD;
  }
}

int main(int argc, char **argv) {
  init();

  ll N, M, K;
  cin >> N >> M >> K;

  vector<ll> sum(N + 1, 0);
  sum[0] = 1;
  for (ll i = 0; i < N; ++i)
    (sum[i + 1] = sum[i] * (M - 1)) %= MOD;

  ll res{0};
  for (ll c = 0; c <= K; ++c) {
    ll tmp{(M * sum[N - c - 1]) % MOD};
    (res += (comb(N - 1, c) * tmp) % MOD) %= MOD;
  }

  std::cout << res << std::endl;
}


Error: TLE

Fixed submission:

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

using ll = long long;

constexpr ll MOD = 998244353;
constexpr ll INF = 1ll << 60;

#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

vector<ll> fac(300001);  // n!(mod M)
vector<ll> ifac(300001); // k!^{M-2} (mod M)

ll mpow(ll x, ll n) { // x^n(mod
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbmod
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void init() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 300000; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD; //
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) % MOD;
  }
}

int main(int argc, char **argv) {
  init();

  ll N, M, K;
  cin >> N >> M >> K;
  ll res{0};
  for (ll c = 0; c <= K; ++c) {
    ll tmp{M};
    for (ll n = 1; n < N - c; ++n)
      (tmp *= (M - 1)) %= MOD;
    (res += (comb(N - 1, c) * tmp) % MOD) %= MOD;
  }

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

#include <bits/stdc++.h>

using namespace std;
#define ll long long int
#define mod 998244353

unsigned long long fac[200000 + 1];
long long power(unsigned long long x, int y, int p) {
  unsigned long long res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
  // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
long long modInverse(unsigned long long n, int p) { return power(n, p - 2, p); }

long long ncr(unsigned long long n, int r, int p) {
  if (r == 0)
    return 1;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}

int main() {
  ll t;
  t = 1;
  //  cin t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll ans = m;
    ll sum = 0;
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
      fac[i] = (fac[i - 1] * i) % mod;
    for (int i = 0; i <= k; i++) {
      sum =
          sum % mod +
          (ncr(n - 1, i, mod) % mod * power(m - 1, n - i - 1, mod) % mod) % mod;
      sum %= mod;
    }
    ans = (ans % mod * sum % mod) % mod;
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long int
#define mod 998244353

long long power(unsigned long long x, int y, int p) {
  unsigned long long res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
  // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

// Returns n^(-1) mod p
long long modInverse(unsigned long long n, int p) { return power(n, p - 2, p); }

long long ncr(unsigned long long n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r
  unsigned long long fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = (fac[i - 1] * i) % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}

int main() {
  ll t;
  t = 1;
  //  cin t;
  while (t--) {
    ll n, m, k;
    cin >> n >> m >> k;
    ll ans = m;
    ll sum = 0;
    for (int i = 0; i <= k; i++) {
      sum =
          sum % mod +
          (ncr(n - 1, i, mod) % mod * power(m - 1, n - i - 1, mod) % mod) % mod;
      sum %= mod;
    }
    ans = (ans % mod * sum % mod) % mod;
    cout << ans << endl;
  }
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, M, K, MOD = 998244353, ans = 0;
  cin >> N >> M >> K;
  vector<ll> f(N + 1, 1);
  rep(i, N) f[i + 1] = (f[i] * (i + 1)) % MOD;
  rep(i, K + 1) {
    ll x = M * mod_pow(M - 1, N - 1 - i, MOD);
    x %= MOD;
    x *= f[N - 1];
    x %= MOD;
    x *= mod_pow(f[i], MOD - 2, MOD);
    x %= MOD;
    x *= mod_pow(f[N - 1 - i], MOD - 2, MOD);
    x %= MOD;
    ans = (ans + x) % MOD;
  }
  cout << ans << "\\n";
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

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

typedef long long ll;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll N, M, K, MOD = 998244353, ans = 0;
  cin >> N >> M >> K;
  vector<ll> f(N, 1);
  rep(i, N) f[i + 1] = (f[i] * (i + 1)) % MOD;
  rep(i, K + 1) {
    ll x = M * mod_pow(M - 1, N - 1 - i, MOD);
    x %= MOD;
    x *= f[N - 1];
    x %= MOD;
    x *= mod_pow(f[i], MOD - 2, MOD);
    x %= MOD;
    x *= mod_pow(f[N - 1 - i], MOD - 2, MOD);
    x %= MOD;
    ans = (ans + x) % MOD;
  }
  cout << ans << "\\n";
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int MOD = 998244353;
const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  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 COM(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;
}

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  COMinit();
  ll ans = 0;
  ll mexp[n + 5];
  mexp[0] = 1;
  for (int i = 1; i < n + 5; i++) {
    mexp[i] = ((mexp[i - 1]) * (m - 1)) % MOD;
  }

  for (int i = 0; i <= k; i++) {
    ll add = m % MOD;
    add = (add * COM(n - 1, i)) % MOD;
    add = (add * mexp[n - i - 1]) % MOD;
    ans = (ans + add) % MOD;
  }

  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const ll MOD = 998244353;

vector<long long> inv, finv;
void COMinit(long long siz) {
  long long buf = 5;
  inv.resize(siz + buf);
  finv.resize(siz + buf);

  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for (int i = 2; i < siz + buf; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  long long ans = 1;
  for (long long i = n; i >= n - k + 1; i--) {
    ans *= i;
    ans %= MOD;
  }
  ans *= finv[k];
  return ans % MOD;
}

// #define int long long
// signed main(){
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m, k;
  cin >> n >> m >> k;
  COMinit(n);
  ll ans = 0;
  ll mexp[n + 5];
  mexp[0] = 1;
  for (int i = 1; i < n + 5; i++) {
    mexp[i] = ((mexp[i - 1]) * (m - 1)) % MOD;
  }

  for (int i = 0; i <= k; i++) {
    ll add = m % MOD;
    add = (add * COM(n - 1, i)) % MOD;
    add = (add * mexp[n - i - 1]) % MOD;
    ans = (ans + add) % MOD;
  }

  cout << ans << "\\n";
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 10000000000000000
#define mod 998244353

vector<ll> f(200005);
ll modpow(ll a, ll n) { // 繰り返し二乗法
  if (n == 0)
    return 1;
  if (n == 1)
    return a % mod;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1)) % mod;
  ll t = modpow(a, n / 2);
  return (t * t) % mod;
}

ll combi(ll n, ll r) {
  return f[n] * modpow(f[n - r], mod - 2) % mod * modpow(f[r], mod - 2) % mod;
}
/*mainに

f[0]=1;
for(ll i=1;i<=200000;i++){f[i]=(f[i-1]*i)%mod;}を入れる

*/
int main() {
  ll n, m, k, i; // m:色の種類 k:同色で隣接可能な数
  cin >> n >> m >> k;

  f[0] = 1;
  for (ll i = 1; i <= 200000; i++) {
    f[i] = (f[i - 1] * i) % mod;
  }

  ll ans = 0;
  for (i = 0; i <= k; i++) {
    ans += ((combi(n - 1, i) * m) % mod) * modpow(m - 1, n - 1 - i) % mod;
    ans %= mod;
  }
  // combi(n-1,i):分け方
  // m*modpow(m-1,n):色の割り当て方

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 10000000000000000
#define mod 998244353

ll MAX = 2000050;
vector<ll> fac(MAX), finv(MAX), inv(MAX);

// 前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll 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;
  }
}

ll combi(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

ll modpow(ll a, ll n) { // 繰り返し二乗法
  if (n == 1)
    return a % mod;
  if (n % 2 == 1)
    return (a * modpow(a, n - 1)) % mod;
  long long t = modpow(a, n / 2);
  return (t * t) % mod;
}

int main() {
  ll n, m, k, i; // m:色の種類 k:同色で隣接可能な数
  cin >> n >> m >> k;
  COMinit();

  ll ans = 0;
  for (i = 0; i <= k; i++) {
    ans += ((combi(n - 1, i) * m) % mod) * modpow(m - 1, n - 1 - i) % mod;
    ans %= mod;
  }
  // combi(n-1,i):分け方
  // m*modpow(m-1,n):色の割り当て方

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const long long int big = 998244353;
const int MAX = 510000;
long long int fac[MAX], finv[MAX], inv[MAX];

long long int COM(long long int n, long long int k);
void COMinit();

long long int mpow(int m, int k);

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  long long int res = 0, temp;

  COMinit();
  // cout << mpow(m - 1, n - 0 - 1) << endl;
  // cout << COM(n - 1, n - 2) << endl;
  for (int i = 0; i <= k; i++) {
    temp = (COM(n - 1, i) * ((m * mpow(m - 1, n - i - 1)) % big)) % big;
    res = res + temp;

    res = res % big;
  }
  cout << res << endl;
}

long long int mpow(int m, int k) {
  if (k == 1) {
    return m;
  } else if (k == 0) {
    return 1;
  } else {
    if (k % 2 == 1) {
      long long int a = mpow(m, k / 2);
      return (((a * a) % big) * m) % big;
    } else {
      long long int a = mpow(m, k / 2);
      return (a * a) % big;
    }
  }
}

void COMinit() {
  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 % big;
    inv[i] = big - inv[big % i] * (big / i) % big;
    finv[i] = finv[i - 1] * inv[i] % big;
  }
}

long long int COM(long long int n, long long int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  // long long int res = 1;
  // for (int i = 0; i < k; i++)
  // {
  //     res *= (n - i);
  //     res %= big;
  // }
  // return (res * (finv[k] % big)) % big;
  return (((fac[n] * (finv[n - k]) % big) * finv[k])) % big;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const long long int big = 998244353;
const int MAX = 510000;
long long int fac[MAX], finv[MAX], inv[MAX];

long long int COM(long long int n, long long int k);
void COMinit();
long long int mpow(int m, int k);

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  long long int res = 0, temp;

  COMinit();
  cout << mpow(m - 1, n - 0 - 1) << endl;
  for (int i = 0; i <= k; i++) {
    temp = (COM(n - 1, i) * ((m * mpow(m - 1, n - i - 1)) % big)) % big;
    res = res + temp;

    res = res % big;
  }
  cout << res << endl;
}

long long int mpow(int m, int k) {
  if (k == 1) {
    return m;
  } else {
    if (k % 2 == 1) {
      long long int a = mpow(m, k / 2);
      return (a * a * m) % big;
    } else {
      long long int a = mpow(m, k / 2);
      return (a * a) % big;
    }
  }
}

void COMinit() {
  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 % big;
    inv[i] = big - inv[big % i] * (big / i) % big;
    finv[i] = finv[i - 1] * inv[i] % big;
  }
}

long long int COM(long long int n, long long int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  long long int res = 1;
  for (int i = 0; i < k; i++) {
    res *= (n - i);
    res %= big;
  }
  return res * (finv[k] % big) % big;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

const int MAX = 200001;
const int MOD = 998244353;

long long i, j, k, N, M2, K, ans, n;

long long fac[MAX], finv[MAX], inv[MAX];

long long power(long long X, long long P, long long M) {
  long long s;

  if (P == 0) {
    return 1;
  } else if (P % 2 == 0) {
    s = power(X, P / 2, M);
    s *= s;
    s %= M;
    return s;
  } else {
    s = power(X, P - 1, M) * X;
    s %= M;
    return s;
  }
}

void COMint() {
  fac[0] = 1;
  finv[0] = 1;
  fac[1] = 1;
  finv[1] = 1;
  inv[1] = 1;
  for (i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;                 /// n!の計算
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; /// n^-1の計算
    finv[i] = finv[i - 1] * inv[i] % MOD;          // n!^-1の計算
  }
}

long long comb(long long X, long long Y) {
  if (X < Y)
    return 0;
  else if (X < 0 || Y < 0)
    return 0;
  return fac[X] * (finv[Y] * finv[X - Y] % MOD) % MOD;
}

int main() {
  cin >> N >> M2 >> K;

  COMint();

  ans = (M2 * power(M2 - 1, N - 1, MOD)) % MOD;
  for (i = 1; i <= K; i++) {
    n = (M2 * power(M2 - 1, N - i - 1, MOD)) % MOD;

    n = (n * comb(N - 1, i)) % MOD;
    ans += n;
    ans %= MOD;
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;

long long i, j, k, N, M2, K, MOD, ans, n;

long long power(long long X, long long P, long long M) {
  long long s;

  if (P == 0) {
    return 1;
  } else if (P % 2 == 0) {
    s = power(X, P / 2, M);
    s *= s;
    s %= M;
    return s;
  } else {
    s = power(X, P - 1, M) * X;
    s %= M;
    return s;
  }
}

long long comb(long long X, long long Y) {
  long long s, t, u;

  s = 1;
  for (t = 1; t <= Y; t++) {
    s *= t;
    s %= MOD;
  }

  s = power(s, MOD - 2, MOD);

  u = 1;
  for (t = X; t >= X - Y + 1; t--) {
    u *= t;
    u %= MOD;
  }

  s = s * u;
  s %= MOD;

  return s;
}

int main() {
  cin >> N >> M2 >> K;
  MOD = 998244353;

  ans = (M2 * power(M2 - 1, N - 1, MOD)) % MOD;
  for (i = 1; i <= K; i++) {
    n = (M2 * power(M2 - 1, N - i - 1, MOD)) % MOD;

    n = (n * comb(N - 1, i)) % MOD;
    ans += n;
    ans %= MOD;
  }

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

#include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
const int MOD = 998244353;

int64_t fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  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;
  }
}

int64_t COM(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() {
  int64_t N, M, K, ans = 0;
  cin >> N >> M >> K;
  vector<int64_t> POW(N);
  int64_t m = M;
  for (int i = 0; i < N; i++) {
    POW.at(i) = m;
    m = (m * (M - 1)) % MOD;
  }
  COMinit();
  for (int k = 0; k <= K; k++) {
    int64_t x = COM(N - 1, k) % MOD;
    x = (x * POW.at(N - 1 - k)) % MOD;
    ans = (ans + x) % MOD;
  }
  cout << ans % MOD << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
const int64_t MOD = 998244353;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  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 COM(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() {
  int64_t N, M, K, ans = 0;
  cin >> N >> M >> K;
  COMinit();
  for (int k = 0; k <= K; k++) {
    int64_t x = ((COM(N - 1, k) % MOD) * M) % MOD;
    for (size_t i = 0; i < (N - 1) - k; i++) {
      x = (x * (M - 1)) % MOD;
    }
    ans = (ans + x) % MOD;
  }
  cout << ans % MOD << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

#define endl '\\n'

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define BUFF ios::sync_with_stdio(0);

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const int MOD = 998244353;
const ld pi = acos(-1);

ll pow(ll x, ll y, ll m) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = (ret * x) % m;
    y >>= 1;
    x = (x * x) % m;
  }
  return ret;
}
int main() {
  BUFF ll n, m, k;
  cin >> n >> m >> k;
  ll acc = 0;
  vector<ll> fact(n + 10, 1), inv(n + 10, 1);
  for (ll i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    inv[i] = pow(fact[i], MOD - 2, MOD);
  }
  for (ll i = 0; i <= k; i++) {
    ll val = ((pow(m - 1, n - 1 - i, MOD) * m) % MOD);
    val =
        (val * ((((fact[n - 1] * inv[i]) % MOD) * inv[n - 1 - i]) % MOD) % MOD);
    acc = (acc + val) % MOD;
  }
  cout << acc << endl;
  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

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

#define endl '\\n'

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define BUFF ios::sync_with_stdio(0);

typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const int MOD = 998244353;
const ld pi = acos(-1);

ll pow(ll x, ll y, ll m) {
  ll ret = 1;
  while (y) {
    if (y & 1)
      ret = (ret * x) % m;
    y >>= 1;
    x = (x * x) % m;
  }
  return ret;
}
int main() {
  BUFF int n, m, k;
  cin >> n >> m >> k;
  ll acc = 0;
  vector<ll> fact(n, 1), inv(n, 1);
  for (ll i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
    inv[i] = pow(fact[i], MOD - 2, MOD);
  }
  for (ll i = 0; i <= k; i++) {
    ll val = ((pow(m - 1, n - 1 - i, MOD) * m) % MOD);
    val =
        (val * ((((fact[n - 1] * inv[i]) % MOD) * inv[n - 1 - i]) % MOD) % MOD);
    acc = (acc + val) % MOD;
  }
  cout << acc << endl;
  return 0;
}