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

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

const int mod = 998244353;

int n;
int m;
int k;

long long inv[N];
long long fac[N];
long long finv[N];

void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N; 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 nCr(long long n, long long r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % mod) % mod;
}

int fastP(int b, int e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * 1ll * fastP(b * 1ll * b % mod, e >> 1) % mod;
  return fastP(b * 1ll * b % mod, e >> 1) % mod;
}

void mul(long long &a, long long b) {
  a *= b;
  a %= mod;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  cominit();
  long long ways = 0;
  for (int i = 0; i <= k; i++) {
    long long sofar = 1;
    mul(sofar, m);
    mul(sofar, fastP(m - 1, n - 1 - i));
    mul(sofar, nCr(n - 1, i));
    ways += sofar;
    ways %= mod;
  }
  printf("%lld\\n", ways);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 3e3 + 5;

const int mod = 998244353;

int n;
int m;
int k;

long long inv[N];
long long fac[N];
long long finv[N];

void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < N; 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 nCr(long long n, long long r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % mod) % mod;
}

int fastP(int b, int e) {
  if (!e)
    return 1;
  if (e & 1)
    return b * 1ll * fastP(b * 1ll * b % mod, e >> 1) % mod;
  return fastP(b * 1ll * b % mod, e >> 1) % mod;
}

int main() {
  scanf("%d%d%d", &n, &m, &k);
  cominit();
  long long ways = 0;
  for (int i = 0; i <= k; i++) {
    ways += 1LL * m * fastP(m - 1, n - 1 - k) * nCr(n - 1, i);
    ways %= mod;
  }
  printf("%lld\\n", ways);
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;
/* ちゃんと考えてわかって実装 */

const ll MOD = 998244353;

// mod p における掛け算
ll mul_mod(ll a, ll b, ll modp) { return (a * b) % modp; }

// mod p における累乗
ll mod_pow(ll x, ll n, ll modp) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % modp;
    x = x * x % modp;
    n >>= 1;
  }
  return res;
}

/*----------------------------------
// 1 : O(n)でテーブル準備 計算はO(1) //
// 使用例 : COMinit(); COM(10, 4); //
----------------------------------*/
const ll MAX = 510000;
ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理 : O(n)
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;
  }
}

// 二項係数計算 : O(1)
ll COM(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;
}

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

  COMinit();

  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll cmb = COM(n - 1, i);
    ll Mn = mod_pow(m - 1, n - i - 1, MOD);
    ll tmp_ans = mul_mod(cmb, Mn, MOD);
    tmp_ans = mul_mod(tmp_ans, m, MOD);
    ans += tmp_ans;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;
/* ちゃんと考えてわかって実装 */

const ll MOD = 998244353;

// mod p における掛け算
ll mul_mod(ll a, ll b, ll modp) { return (a * b) % modp; }

// mod p における累乗
ll mod_pow(ll x, ll n, ll modp) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % modp;
    x = x * x % modp;
    n >>= 1;
  }
  return res;
}

// nCkの計算 : O(k)
ll mod_comb(ll n, ll k, ll modp) {
  ll X = 1;
  for (int i = 0; i < k; i++) {
    X = mul_mod(X, n - i, modp);
  }
  ll Y = 1;
  for (int i = 0; i < k; i++) {
    Y = mul_mod(Y, k - i, modp);
  }
  ll Y_pow = mod_pow(Y, modp - 2, modp);
  return mul_mod(X, Y_pow, modp);
}

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

  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ll cmb = mod_comb(n - 1, i, MOD);
    ll Mn = mod_pow(m - 1, n - i - 1, MOD);
    ll tmp_ans = mul_mod(cmb, Mn, MOD);
    tmp_ans = mul_mod(tmp_ans, m, MOD);
    ans += tmp_ans;
    ans %= MOD;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

ll mod = 998244353;

ll com(ll N, ll K, ll L) {
  ll d = 1, e = (L * K) % mod, m_ = mod - 2;
  while (m_ != 0) {
    if (m_ & 1)
      d = (d * e) % mod;
    e = e * e % mod;
    m_ = m_ >> 1;
  }
  return (N * d) % mod;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  if (m == 1) {
    if (k == n - 1)
      print(1);
    else
      print(0);
    return 0;
  }
  lvector fact(n + 10, 1);
  rep(i, n + 5) fact[i + 1] = (fact[i] * (i + 1)) % mod;
  lvector m_m1(n + 10, m);
  rep(i, n + 5) m_m1[i + 1] = (m_m1[i] * (m - 1)) % mod;
  ans += m_m1[n - 1] % mod;
  for (ll v = 1; v <= k; ++v) {
    ans += (com(fact[n - 1], fact[v], fact[n - 1 - v]) * m_m1[n - 1 - v]) % mod;
    ans %= mod;
  }
  print(ans);
  return 0;
}


Error: -6
malloc(): corrupted top size


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define lque queue<ll>
#define lpque priority_queue<ll>
#define dlpque priority_queue<ll, lvector, greater<ll>>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

ll mod = 998244353;

ll com(ll N, ll K, ll L) {
  ll d = 1, e = (L * K) % mod, m_ = mod - 2;
  while (m_ != 0) {
    if (m_ & 1)
      d = (d * e) % mod;
    e = e * e % mod;
    m_ = m_ >> 1;
  }
  return (N * d) % mod;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k, ans = 0;
  cin >> n >> m >> k;
  if (m == 1) {
    if (k == n - 1)
      print(1);
    else
      print(0);
    return 0;
  }
  lvector fact(n + 10, 1);
  rep(i, n + 10) fact[i + 1] = (fact[i] * (i + 1)) % mod;
  lvector m_m1(n + 10, m);
  rep(i, n) m_m1[i + 1] = (m_m1[i] * (m - 1)) % mod;
  ans += m_m1[n - 1] % mod;
  for (ll v = 1; v <= k; ++v) {
    ans += (com(fact[n - 1], fact[v], fact[n - 1 - v]) * m_m1[n - 1 - v]) % mod;
    ans %= mod;
  }
  print(ans);
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int mod = 998244353;

ll calc(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll d = calc(a, b / 2, p);
    return (d * d) % p;
  } else
    return (a * calc(a, b - 1, p)) % p;
}
ll fac[MAX];
void COMinit(void) {
  fac[0] = fac[1] = 1;
  for (int i = 2; i <= MAX; i++) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }
}

ll nCr(ll n, ll r) {
  ll denom = fac[r] * fac[n - r] % mod;
  return fac[n] * calc(denom, mod - 2, mod) % mod;
}

signed main(void) {
  int n, m, k;
  ll ans, tmp;

  cin >> n >> m >> k;
  COMinit();
  ans = 0;
  tmp = m;
  for (int x = n - 1; x >= 0; x--) {
    if (x <= k) {
      ans += tmp * nCr(n - 1, x) % mod;
      ans %= mod;
    }
    tmp *= m - 1;
    tmp %= mod;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

const int mod = 998244353;

ll calc(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll d = calc(a, b / 2, p);
    return (d * d) % p;
  } else
    return (a * calc(a, b - 1, p)) % p;
}

ll nCr(ll n, ll r) {
  ll dp[n + 1];
  dp[0] = dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    dp[i] = dp[i - 1] * i;
    dp[i] %= mod;
  }
  return dp[n] * calc(dp[r] * dp[n - r] % mod, mod - 2, mod) % mod;
}

signed main(void) {
  int n, m, k;
  ll ans, tmp;

  cin >> n >> m >> k;
  ans = 0;
  tmp = m;
  for (int x = n - 1; x >= 0; x--) {
    if (x <= k) {
      ans += tmp * nCr(n - 1, x) % mod;
      ans %= mod;
    }
    tmp *= m - 1;
    tmp %= mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <chrono>
using namespace std::chrono;
using namespace std;
#define fr(x, i, n) for (int x = i; x < n; ++x)
#define fb(x, i, n) for (int x = i; x >= n; --x)
#define pr pair<int, int>
#define ll long long int
const ll mod = 998244353;
/*    auto start = high_resolution_clock::now();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cout << "\\nduration="<<duration.count()/1e6 << endl;
*/
ll f(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n % 2 == 1)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n /= 2;
  }
  return r;
}
vector<ll> fact(2e5 + 10, 1), ifact(2e5 + 10);
void build() {
  for (ll i = 2; i < fact.size(); i++)
    fact[i] = (i * fact[i - 1]) % mod;
  fr(i, 0, fact.size()) { ifact[i] = f(fact[i], mod - 2); }
}
ll C(int n, int r) {
  if (n < r)
    return 0;
  return (((fact[n] * ifact[r]) % mod) * ifact[n - r]) % mod;
}
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  // let l blocks are colored same color as their left adjacent
  ll ans = 0;
  fr(l, 0, k + 1) {
    ll x;
    x = (C(n - 1, l) * m) % mod;
    x = (x * f(m - 1, n - l - 1)) % mod;
    x = (x + mod) % mod;
    ans = (ans + x) % mod;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  build();
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
using namespace std::chrono;
using namespace std;
#define fr(x, i, n) for (int x = i; x < n; ++x)
#define fb(x, i, n) for (int x = i; x >= n; --x)
#define pr pair<int, int>
#define ll long long int
const ll mod = 998244353;
/*    auto start = high_resolution_clock::now();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cout << "\\nduration="<<duration.count()/1e6 << endl;
*/
ll f(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n % 2 == 1)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n /= 2;
  }
  return r;
}
vector<ll> fact(1e5 + 10, 1), ifact(1e5 + 10);
void build() {
  for (ll i = 2; i < fact.size(); i++)
    fact[i] = (i * fact[i - 1]) % mod;
  fr(i, 0, fact.size()) { ifact[i] = f(fact[i], mod - 2); }
}
ll C(int n, int r) {
  if (n < r)
    return 0;
  return (((fact[n] * ifact[r]) % mod) * ifact[n - r]) % mod;
}
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  // let l blocks are colored same color as their left adjacent
  ll ans = 0;
  fr(l, 0, k + 1) {
    ll x;
    x = (C(n - 1, l) * m) % mod;
    x = (x * f(m - 1, n - l - 1)) % mod;
    x = (x + mod) % mod;
    ans = (ans + x) % mod;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  build();
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const ll MOD = 998244353;

ll box[200001];

void init() {
  box[0] = 1;
  for (int i = 1; i < 200001; i++) {
    box[i] = box[i - 1] * i;
    box[i] %= MOD;
  }
}

ll modpow(ll x, ll n) {
  x %= MOD;
  if (n == 0)
    return 1; // 再帰終了

  else if (n % 2 == 1) {
    return (x * modpow(x, n - 1)) % MOD; // nが奇数ならnを1ずらす
  } else {
    return modpow((x * x) % MOD, n / 2) % MOD; // nが偶数ならnは半分になる
  }
}

ll comb(ll n, ll k) {

  return box[n] * modpow(box[k] * box[n - k] % MOD, MOD - 2) % MOD;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  init();
  ll ans = 0LL;
  // cout << box[10101] << endl;
  rep(i, k + 1) {
    ans = ans +
          ((((m * comb(n - 1, i)) % MOD) * (modpow(m - 1, n - 1 - i)) % MOD) %
           MOD) %
              MOD;
  }
  while (ans < 0) {
    ans += MOD;
  }
  cout << ans % MOD << endl;
}

Error: TLE

Fixed submission:

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

const ll MOD = 998244353;

ll modpow(ll x, ll n) {
  x %= MOD;
  if (n == 0)
    return 1; // 再帰終了

  else if (n % 2 == 1) {
    return (x * modpow(x, n - 1)) % MOD; // nが奇数ならnを1ずらす
  } else {
    return modpow((x * x) % MOD, n / 2) % MOD; // nが偶数ならnは半分になる
  }
}

ll comb(ll n, ll k) {
  ll x = 1;
  for (int i = n - k + 1; i <= n; i++) {
    x = x * i % MOD;
  }

  ll y = 1;
  for (int i = 1; i <= k; i++) {
    y = y * i % MOD;
  }
  y = modpow(y, MOD - 2);

  return x * y % MOD;
}

int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  ll ans = 0LL;
  rep(i, k + 1) {
    ans = ans +
          ((((m * comb(n - 1, i)) % MOD) * (modpow(m - 1, n - 1 - i)) % MOD) %
           MOD) %
              MOD;
  }
  while (ans < 0) {
    ans += MOD;
  }
  cout << ans % MOD << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define INF (1LL << 50)

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() {
  // 前処理
  COMinit();
  ll N, M, K;
  cin >> N >> M >> K;
  // N-1個の境目の中からk個選んで同じ色の組を作る
  ll ans = 0;
  ll tmp = M;
  rep(i, N - 1 - K) {
    tmp *= M - 1;
    tmp %= MOD;
  }
  ans += COM(N - 1, K) * tmp;
  ans %= MOD;
  for (int k = K - 1; k >= 0; k--) {
    tmp *= M - 1;
    tmp %= MOD;
    ans += COM(N - 1, k) * tmp;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define INF (1LL << 50)
#define MOD 998244353

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  vector<vector<ll>> memo(N + 1, vector<ll>(N + 1));
  memo[1][0] = M; // 初めのブロックはM通りの選び方がある
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < i + 1; j++) {
      // 違う色で塗る塗り方
      memo[i + 1][j] += memo[i][j] * (M - 1);
      memo[i + 1][j] %= MOD;
      // 同じ色で塗る塗り方
      memo[i + 1][j + 1] = memo[i][j];
    }
  }
  ll ans = 0;
  for (int j = 0; j <= K; j++) {
    ans += memo[N][j];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

const int mod = 998244353;

vector<int> getketa(long k) {
  vector<int> ret;
  while (k > 0) {
    ret.push_back(k % 2);
    k /= 2;
  }
  return ret;
}

long getrev(long k) {
  vector<int> modm2 = getketa(mod - 2);
  vector<long> powk(32); // k^(2^i)
  powk[0] = k;
  for (int i = 1; i < 32; i++) {
    powk[i] = (powk[i - 1] * powk[i - 1]) % mod;
  }
  long ret = 1;
  for (int i = 0; i < modm2.size(); i++) {
    if (modm2[i]) {
      ret *= powk[i];
      ret %= mod;
    }
  }
  return ret;
}

int main() {
  long n, m, k;
  cin >> n >> m >> k;
  vector<long> rev(n, 0);
  for (int i = 1; i < n; i++) {
    rev[i] = getrev(i);
  }
  vector<long> powm1(n + 1);
  powm1[0] = 1;
  for (int i = 1; i <= n; i++) {
    powm1[i] = ((m - 1) * powm1[i - 1]) % mod;
  }
  vector<long> n1ci(n);
  n1ci[0] = 1;
  for (int i = 1; i < n; i++) {
    n1ci[i] = ((n1ci[i - 1] * (n - i)) % mod * rev[i]) % mod;
  }
  long ret = 0;
  for (int i = n - k - 1; i < n; i++) {
    ret += n1ci[i] * ((powm1[i] + powm1[i + 1]) % mod) % mod;
    ret %= mod;
  }
  cout << ret;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

const int mod = 998244353;

vector<int> getketa(long k) {
  vector<int> ret;
  while (k > 0) {
    ret.push_back(k % 2);
    k /= 2;
  }
  return ret;
}

long getrev(long k) {
  vector<int> modm2 = getketa(mod - 2);
  vector<long> powk(32); // k^(2^i)
  powk[0] = k;
  for (int i = 1; i < 32; i++) {
    powk[i] = (powk[i - 1] * powk[i - 1]) % mod;
  }
  long ret = 1;
  for (int i = 0; i < modm2.size(); i++) {
    if (modm2[i]) {
      ret *= powk[i];
      ret %= mod;
    }
  }
  return ret;
}

int main() {
  long n, m, k;
  cin >> n >> m >> k;
  vector<long> rev(n, 0);
  for (int i = 1; i < n; i++) {
    rev[i] = getrev(i);
    cerr << "rev[" << i << "]=" << rev[i] << endl;
  }
  vector<long> powm1(n + 1);
  powm1[0] = 1;
  for (int i = 1; i <= n; i++) {
    powm1[i] = ((m - 1) * powm1[i - 1]) % mod;
    cerr << "powm1[" << i << "]=" << powm1[i];
  }
  vector<long> n1ci(n);
  n1ci[0] = 1;
  for (int i = 1; i < n; i++) {
    n1ci[i] = ((n1ci[i - 1] * (n - i)) % mod * rev[i]) % mod;
  }
  long ret = 0;
  for (int i = n - k - 1; i < n; i++) {
    ret += n1ci[i] * ((powm1[i] + powm1[i + 1]) % mod) % mod;
    ret %= mod;
  }
  cout << ret;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

const int mod = 998244353;

const int MAX = 200005;
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 mod_comb(int n, int k, int mod) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

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

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

  COMinit();

  int N, M, K;
  cin >> N >> M >> K;

  long long ans = 0;
  for (int k = 0; k <= K; k++) {
    long long add_v = 0;
    add_v = (M * mod_pow(M - 1, N - k - 1, mod)) % mod;
    add_v = (add_v * mod_comb(N - 1, k, mod)) % mod;
    ans = (ans + add_v) % mod;
  }

  if (ans < 0) {
    ans += mod;
  }

  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

const int mod = 998244353;

const int MAX = 100005;
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 mod_comb(int n, int k, int mod) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

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

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

  COMinit();

  int N, M, K;
  cin >> N >> M >> K;

  long long ans = 0;
  for (int k = 0; k <= K; k++) {
    long long add_v = 0;
    add_v = (M * mod_pow(M - 1, N - k - 1, mod)) % mod;
    add_v = (add_v * mod_comb(N - 1, k, mod)) % mod;
    ans = (ans + add_v) % mod;
  }

  if (ans < 0) {
    ans += mod;
  }

  cout << ans << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
#define MAXN 400005
#define MOD 998244353
#define LL long long int
LL ans, res, C[MAXN], fac[MAXN], revfac[MAXN];
int n, m, k;
LL qpow(LL a, int b) {
  LL tmp = 1;
  while (b) {
    if (b & 1)
      tmp = tmp * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return tmp % MOD;
}
void facrev() {
  C[0] = 1;
  C[1] = (n - 1);
  fac[1] = 1;
  fac[0] = 1;
  revfac[0] = 1;
  revfac[1] = 1;
  for (int i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    C[i] = C[i - 1] * (n - i) % MOD;
  }
  revfac[n] = qpow(fac[n], MOD - 2);
  for (int i = n - 1; i >= 1; i--) {
    revfac[i] = revfac[i + 1] * (i + 1) % MOD;
    C[i] = C[i] * revfac[i] % MOD;
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  facrev();
  int p = n - k;
  res = m;
  for (int i = 1; i <= p - 1; i++) {
    res = res * (m - 1) % MOD;
  }
  ans = res * C[p - 1] % MOD;
  for (int j = p + 1; j <= n; j++) {
    res = res * (m - 1) % MOD;
    ans = (ans + res * C[j - 1] % MOD) % MOD;
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAXN 400005
#define MOD 998244353
#define LL long long int
LL ans, res, C[MAXN], fac[MAXN], revfac[MAXN];
int n, m, k;
LL qpow(LL a, int b) {
  LL tmp = 1;
  while (b) {
    if (b & 1)
      tmp = tmp * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return tmp;
}
int facrev() {
  C[0] = 1;
  C[1] = (n - 1);
  fac[1] = 1;
  for (int i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    C[i] = C[i - 1] * (n - i) % MOD;
  }
  revfac[n] = qpow(fac[n], MOD - 2);
  for (int i = n - 1; i >= 1; i--)
    revfac[i] = revfac[i + 1] * (i + 1) % MOD, C[i] = C[i] * revfac[i] % MOD;
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  facrev();
  int p = n - k;
  res = m;
  for (int i = 1; i <= p - 1; i++) {
    res = res * (m - 1) % MOD;
  }
  ans = res * C[p - 1] % MOD;
  for (int j = p + 1; j <= n; j++) {
    res = res * (m - 1) % MOD;
    ans = (ans + res * C[j - 1] % MOD) % MOD;
  }
  printf("%lld\\n", ans);
  return 0;
}