alexjercan/bugnet huggingface dataset:
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

const int MAX = 510000;
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() {
  ll n, m, k;
  cin >> n;
  cin >> m;
  cin >> k;
  COMinit();
  vector<ll> power(n, 1);
  FOR(i, 1, n) {
    power[i] = power[i - 1] * (m - 1);
    power[i] %= MOD;
  }
  ll ans = 0;
  REP(i, k + 1) {
    ll tmp = m;
    tmp *= power[n - 1 - i];
    tmp %= MOD;
    // cout<<"/"<<tmp;
    tmp *= COM(n - 1, i);
    tmp %= MOD;
    // cout<<" /"<<tmp<<endl;
    ans += tmp;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18
#define MOD 998244353

ll n, m, k, ans;
vector<ll> p;
void DFS(deque<ll> s, deque<ll> kindCnt) {
  if (s.size() == n) {
    // cout << "s: "; REP(i,(signed)s.size()) cout<<s[i]<<" ";
    // //----------cout----------// cout<<":"<<p[kindCnt.size()]<<endl;
    ans += p[kindCnt.size()];
    ans %= MOD;
    return;
  }
  ll useKind = kindCnt.size();
  REP(i, useKind) {
    if (s[s.size() - 1] == i) {
      s.push_back(i);
      kindCnt[i]++;
      if (kindCnt[i] <= k) {
        DFS(s, kindCnt);
      }
      s.pop_back();
      kindCnt[i]--;
    } else {
      s.push_back(i);
      DFS(s, kindCnt);
      s.pop_back();
    }
  }
  if (useKind < m) {
    kindCnt.push_back(0);
    s.push_back(useKind);
    DFS(s, kindCnt);
    s.pop_back();
    kindCnt.pop_back();
  }
}

int main() {
  cin >> n;
  cin >> m;
  cin >> k;
  p.resize(m + 1);
  p[0] = 1;
  FOR(i, 1, m + 1) {
    p[i] = p[i - 1] * (m - i + 1);
    p[i] %= MOD;
  }
  // cout << "p: "; REP(i,(signed)p.size()) cout<<p[i]<<" "; cout << endl;
  // //----------cout----------//
  deque<ll> s;
  s.push_back(0);
  DFS(s, deque<ll>(1, 0));
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/

using namespace std;
// using namespace __gnu_pbds;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repa(i, arr, n) for (int i = arr; i <= n; i++)
#define repd(i, arr, n) for (int i = arr; i >= n; i--)
#define trav(arr, x) for (auto &arr : x)
#define all(c) c.begin(), c.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(arr) arr.size()
#define pi 3.14159265358979323846
#define MAX (1000 * 1000 * 1000 + 7)
#define vi vector<int>
#define pii pair<int, int>
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define endl "\\n"

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set;

const int mod = 998244353;
int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

int32_t main() {
  fast;

  int n, m, k, ans = 0, val, val1;
  cin >> n >> m >> k;

  vi fac(n + 2, 0);

  fac[0] = 1;
  fac[1] = 1;

  repa(i, 2, n) { fac[i] = (fac[i - 1] * i) % mod; }

  rep(i, k + 1) {
    val = fac[n - i - 1] * fac[i];
    val %= mod;

    val1 = fac[n - 1] * power(val, mod - 2);
    val1 %= mod;

    val = power(m - 1, n - i - 1);
    val *= val1;
    val %= mod;

    val *= m;
    val % +mod;
    ans += val;
    ans %= mod;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define N 7
#define ll long long

using namespace std;

ll n, m, k, MOD = 1e9 + 7;
ll dp[N][N + 1][N];

ll util(ll i, ll c, ll a) {
  if (i >= n) {
    if (a <= k)
      return 1;
    else
      return 0;
  }
  if (a > k)
    return 0;
  if (dp[i][c][a] != -1)
    return dp[i][c][a];
  ll ans = 0;
  for (int j = 1; j <= m; j++) {
    if (j == c)
      ans = (ans + util(i + 1, c, a + 1)) % MOD;
    else
      ans = (ans + util(i + 1, j, a)) % MOD;
  }
  return dp[i][c][a] = ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> m >> k;
  memset(dp, -1, sizeof(dp));
  cout << util(0, 0, 0) << endl;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class Mod {
public:
  ll mod;
  ll kaijou[200005];
  ll inv[200005];
  // mod must be a prime number
  Mod(ll m) {
    mod = m;
    kaijou[0] = 1;
    for (ll i = 1; i <= 200003; i++) {
      kaijou[i] = kaijou[i - 1] * i % mod;
      inv[i] = modpow(kaijou[i], mod - 2);
    }
  }
  // a^p
  ll modpow(ll a, ll p) {
    if (p == 0)
      return 1;
    if (p % 2 == 0) {
      ll half = modpow(a, p / 2);
      return half * half % mod;
    } else
      return a * modpow(a, p - 1) % mod;
  }

  // nCr
  ll comb(ll n, ll r) {
    if (r > n - r)
      return comb(n, n - r);
    if (r == 0)
      return 1;
    return (kaijou[n] * inv[n - r] % mod) * inv[r] % mod;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  ll ans = 0;

  Mod m(998244353);

  for (ll i = 0; i <= K; i++) {
    ll sum =
        (m.comb(N - 1, i) * M % m.mod) * m.modpow(M - 1, N - i - 1) % m.mod;
    // cout << sum << " " << m.comb(N-1,i) << endl;
    ans += sum;
    ans %= m.mod;
  }

  cout << ans << endl;

  /*while(true){
      ll a,b; cin >> a >> b;
      cout << m.kaijou[a] << " " << m.kaijou[b] << endl;
      cout << m.kaijou[a]*m.inv[a]%m.mod << endl;
      cout << m.comb(a,b) << endl;
  }*/

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

class Mod {
public:
  ll mod;
  // mod must be a prime number
  Mod(ll m) { mod = m; }
  // a^p
  ll modpow(ll a, ll p) {
    if (p == 0)
      return 1;
    if (p % 2 == 0) {
      ll half = modpow(a, p / 2);
      return half * half % mod;
    } else
      return a * modpow(a, p - 1) % mod;
  }

  // nCr
  ll comb(ll n, ll r) {
    if (r > n - r)
      return comb(n, n - r);
    ll mul = 1;
    ll div = 1;
    for (int i = 0; i < r; i++) {
      mul *= n - i;
      mul %= mod;
      div *= i + 1;
      div %= mod;
    }
    return mul * modpow(div, mod - 2) % mod;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  ll ans = 0;

  Mod m(998244353);

  for (ll i = 0; i <= K; i++) {
    ll sum =
        (m.comb(N - 1, i) * M % m.mod) * m.modpow(M - 1, N - i - 1) % m.mod;
    ans += sum;
    ans %= m.mod;
  }

  cout << ans << endl;

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

#include <iostream>

using namespace std;
typedef long long ll;

const ll mod = 998244353;

ll fac[200005];
ll rfac[200005];

ll f(ll a, ll x) {
  ll res = 1;

  while (x > 0) {
    if (x & 1)
      res = a * res % mod;

    a = a * a % mod;
    x >>= 1;
  }

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

  ll ans = 0;

  fac[0] = rfac[0] = 1;

  for (int i = 1; i <= max(n, m); i++) {
    fac[i] = fac[i - 1] * i % mod;
    rfac[i] = f(fac[i], mod - 2);
  }

  for (ll i = 0; i <= k; i++) {
    ll tmp = m * f(m - 1, n - i - 1) % mod * fac[n - 1] % mod *
             rfac[n - i - 1] % mod * rfac[i] % mod;
    ans = (ans + tmp) % mod;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;
typedef long long ll;

const ll mod = 998244353;

ll fac[200005];
ll rfac[200005];

ll f(ll a, ll x) {
  ll res = 1;

  while (x > 0) {
    if (x & 1)
      res = a * res % mod;

    a = a * a % mod;
    x >>= 1;
  }

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

  ll ans = f(m, n);

  fac[0] = rfac[0] = 1;

  for (int i = 1; i <= max(n, m); i++) {
    fac[i] = fac[i - 1] * i % mod;
    rfac[i] = f(fac[i], mod - 2);
  }

  if (k < n - 1)
    ans -= m;
  for (ll i = n - 1; i > k; i--) {
    ll tmp = fac[n + i] * rfac[n] % mod * rfac[i] % mod;

    ans = (ans - tmp + mod) % mod;
  }

  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

vector<ll> fac(1e6), finv(1e6), inv(1e6);
ll mod = 998244353;

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;

  for (int i = 2; i < 1e6; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll COM(ll n, ll k) {
  if (n < k || k < 0)
    return 0;

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

ll mod_pow(ll a, ll x, ll mod) {
  if (x == 0)
    return 1;

  else if (x % 2) {
    return a * mod_pow(a, x - 1, mod) % mod;
  } else {
    ll tmp = mod_pow(a, x / 2, mod);
    return tmp * tmp % mod;
  }
}

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

  COMinit();

  for (int i = 0; i <= k; i++) {
    ans += m * mod_pow(m - 1, n - i - 1, mod) % mod * COM(n - 1, i) % mod;
    ans %= mod;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)

typedef long long ll;

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

const int MAX = 300000;
const int MOD = 998244353;

ll mod = 998244353;
//---------------------------------------------------------------------------------------------------
ll f[201010], rf[201010];
ll inv(ll x) {
  ll res = 1;
  ll k = mod - 2;
  ll y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod;
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}
void init() {
  f[0] = 1;
  rep(i, 1, 201010) f[i] = (f[i - 1] * i) % mod;
  rep(i, 0, 201010) rf[i] = inv(f[i]);
}
//---------------------------------------------------------------------------------------------------
ll C(int n, int k) {
  ll a = f[n];      // = n!
  ll b = rf[n - k]; // = (n-k)!
  ll c = rf[k];     // = k!

  ll bc = (b * c) % mod;

  return (a * bc) % mod;
}

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

  ll ans = 0;

  init();

  for (ll l = 0; l <= k; l++) {
    ll tmp = m;
    tmp *= mod_pow(m - 1, n - l - 1, MOD);
    tmp %= MOD;
    tmp *= C(n - 1, l);
    tmp %= MOD;
    ans += tmp;
    ans %= MOD;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef long long ll;

const ll MOD = 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;
  }
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll powm[200010];
  powm[0] = 1;
  rep(i, 1, 200010) {
    powm[i] = powm[i - 1] * (m - 1);
    powm[i] %= MOD;
  }
  ll an = 0;
  init();
  rep(i, 0, k + 1) {
    an += (((m * fac[n - 1]) % MOD * finv[i]) % MOD * finv[n - 1 - i]) % MOD *
          powm[n - 1 - i];
    an %= MOD;
  }
  cout << an << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
typedef long long ll;

const ll MOD = 998244353;

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

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

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll powm[200010];
  powm[0] = 1;
  rep(i, 1, 200010) {
    powm[i] = powm[i - 1] * (m - 1);
    powm[i] %= MOD;
  }
  ll an = 0;
  init();
  rep(i, 0, k + 1) {
    an += (((m * fac[n - 1]) % MOD * finv[i]) % MOD * finv[n - 1 - i]) % MOD *
          powm[n - 1 - i];
    an %= MOD;
  }
  cout << an << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT                                                                 \\
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \\
      cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 998244353;
int N, M, K;
ll Comb[200005], Zyo[200005]; // n-1Ck.

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
ll modinv(ll a) { return modpow(a, MOD - 2); }

void init() {
  Comb[0] = 1ll;
  rep(i, N) {
    Comb[i + 1] = Comb[i] * (N - 1 - i) % MOD * modinv(i + 1);
    Comb[i + 1] %= MOD;
  }
  Zyo[0] = 1ll;
  rep(i, N) {
    if (i == 0)
      continue;
    Zyo[i] = Zyo[i - 1] * (M - 1);
    Zyo[i] %= MOD;
  }
}

// 隣り合う組で同じ色で塗られているのをK色と固定
// 左端の色はM通り、それ以外は左と違う色がN-1-K, 同じがK個。それの並べ替え
// M*n-1Ck*(M-1)^(N-1-K)*1^K

int main() {
  INCANT;
  cin >> N >> M >> K;
  init();
  ll SUM = 0;
  rep(i, K + 1) {
    SUM += M * Comb[i] % MOD * Zyo[N - 1 - i];
    SUM %= MOD;
  }
  if (SUM < 0)
    SUM += MOD;

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

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT                                                                 \\
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \\
      cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 998244353;
int N, M, K;
ll Comb[100005], Zyo[100005]; // n-1Ck.

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a) { return modpow(a, MOD - 2); }

void init() {
  Comb[0] = 1ll;
  rep(i, N) {
    Comb[i + 1] = Comb[i] * (N - 1 - i) % MOD * modinv(i + 1);
    Comb[i + 1] %= MOD;
  }
  Zyo[0] = 1ll;
  rep(i, N) {
    if (i == 0)
      continue;
    Zyo[i] = Zyo[i - 1] * (M - 1);
    Zyo[i] %= MOD;
  }
}

// 隣り合う組で同じ色で塗られているのをK色と固定
// 左端の色はM通り、それ以外は左と違う色がN-1-K, 同じがK個。それの並べ替え
// M*n-1Ck*(M-1)^(N-1-K)*1^K

int main() {
  INCANT;
  cin >> N >> M >> K;
  init();
  ll SUM = 0;
  rep(i, K + 1) {
    SUM += M * Comb[i] % MOD * Zyo[N - 1 - i];
    SUM %= MOD;
  }
  if (SUM < 0)
    SUM += MOD;

  cout << SUM << "\\n";
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const Int MOD = 998244353;

vector<Int> f(200010), invf(200010);

//---------- べき乗 ----------
Int power(Int a, Int n) {
  if (a == 0)
    return 0;
  if (n == 0)
    return 1;

  Int res = 1;
  while (n > 0) {
    if (n % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n /= 2;
  }
  return res;
}

//---------- 逆元 ----------
Int inv(Int a) { return power(a, MOD - 2); }

//---------- 階乗 ----------
vector<Int> factorial(Int n) {
  vector<Int> res(n + 1, 1);
  for (int i = 1; i <= n; i++)
    res[i] = i * res[i - 1] % MOD;

  return res; // nまでの階乗の配列を返す
}

//--------- 組み合わせ ---------
Int comb(Int n, Int r) { return (f[n] * invf[r] % MOD) * invf[n - r] % MOD; }

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

  if (M == 1) {
    if (K == N - 1)
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }

  f = factorial(N);
  rep(i, f.size()) invf[i] = inv(f[i]);

  Int ans = 0, p = inv(M - 1);
  for (Int i = N - 1; i >= 0; i--) {
    p = p * (M - 1) % MOD;
    // cout << p << endl;
    if (i > K)
      continue;

    Int tmp = M;
    tmp = tmp * comb(N - 1, i) % MOD;
    tmp = tmp * p % MOD;

    ans = (ans + tmp) % MOD;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

const Int MOD = 998244353;

//---------- べき乗 ----------
Int power(Int a, Int n) {
  if (a == 0)
    return 0;
  if (n == 0)
    return 1;

  Int res = 1;
  while (n > 0) {
    if (n % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    n /= 2;
  }
  return res;
}

//---------- 逆元 ----------
Int inv(Int a) { return power(a, MOD - 2); }

//---------- 階乗 ----------
vector<Int> factorial(Int n) {
  vector<Int> res(n + 1, 1);
  for (int i = 1; i <= n; i++)
    res[i] = i * res[i - 1] % MOD;

  return res; // nまでの階乗の配列を返す
}

//--------- 組み合わせ ---------
Int comb(Int n, Int r, vector<Int> f) {
  return (f[n] * inv(f[r]) % MOD) * inv(f[n - r]) % MOD;
}

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

  if (M == 1) {
    if (K == N - 1)
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }

  vector<Int> f = factorial(N);

  Int ans = 0;
  for (Int i = 0; i <= K; i++) {
    Int tmp = M;
    tmp = tmp * comb(N - 1, i, f) % MOD;
    tmp = tmp * power(M - 1, N - 1 - i) % MOD;
    ans = (ans + tmp) % MOD;
  }

  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

const LL mod = 998244353;

LL esp(LL x, LL e) {
  if (e == 0)
    return 1;
  if (e % 2 == 1)
    return (x * esp(x, e - 1)) % mod;
  return esp((x * x) % mod, e / 2);
}

// Computes the inverse of n modulo m.
// Precondition: n, m > 0 and gcd(n, m) == 1.
// The returned value is always > 0.
// ACHTUNG: It must hold max(m, n) <= 2**31 to avoid integer overflow.
ULL Inverse(ULL n, ULL m) {
  if (n == 1)
    return 1;
  LL y = Inverse(m % n, n);
  return m - ((m * y - 1) / n) % m;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  LL N, M, K;
  cin >> N >> M >> K;
  if (N == 1) {
    cout << M << "\\n";
    return 0;
  }
  if (M == 1) {
    if (K == N - 1)
      cout << 1 << "\\n";
    else
      cout << 0 << "\\n";
    return 0;
  }

  LL prod = (M * esp(M - 1, N - 1)) % mod;
  LL res = prod;
  LL binom = 1;
  LL invM1 = Inverse(M - 1, mod);

  for (int i = 1; i <= K; i++) {
    prod = (prod * invM1) % mod;
    binom = (binom * (N - i)) % mod;
    binom = (binom * Inverse(i, mod)) % mod;
    res += (prod * binom) % mod;
    res %= mod;
  }
  cout << res << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else

#endif

const LL mod = 998244353;

LL esp(LL x, LL e) {
  if (e == 0)
    return 1;
  if (e % 2 == 1)
    return (x * esp(x, e - 1)) % mod;
  return esp((x * x) % mod, e / 2);
}

// Computes the inverse of n modulo m.
// Precondition: n, m > 0 and gcd(n, m) == 1.
// The returned value is always > 0.
// ACHTUNG: It must hold max(m, n) <= 2**31 to avoid integer overflow.
ULL Inverse(ULL n, ULL m) {
  if (n == 1)
    return 1;
  LL y = Inverse(m % n, n);
  return m - ((m * y - 1) / n) % m;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); // Togliere nei problemi con query online

  LL N, M, K;
  cin >> N >> M >> K;

  LL prod = (M * esp(M - 1, N - 1)) % mod;
  LL res = prod;
  LL binom = 1;
  LL invM1 = Inverse(M - 1, mod);

  for (int i = 1; i <= K; i++) {
    //~ LL parres = M;
    //~ parres *= esp(M-1, N-1-i);
    //~ parres %= mod;
    prod = (prod * invM1) % mod;
    binom = (binom * (N - i)) % mod;
    binom = (binom * Inverse(i, mod)) % mod;
    //~ parres *= binom;
    //~ parres %= mod;
    res += (prod * binom) % mod;
    res %= mod;
  }
  cout << res << "\\n";
}