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

#include <bits/stdc++.h>
#define mod 1000000009
#define ll long long
using namespace std;
long long int n, num, a[200005], vis[200005];
int main() {
  scanf("%lld%lld", &n, &num);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), vis[i] = 0;
  long long int pos1 = 1, pos2;
  while (vis[pos1] != 1) {
    vis[pos1] = 1;
    pos1 = a[pos1];
  }
  pos2 = pos1;
  for (int j = 1; j <= n; j++)
    vis[j] = 0;
  int t = 0;
  while (vis[pos2] != 1) {
    vis[pos2] = 1;
    t++;
    pos2 = a[pos2];
  }
  int b = 0;
  pos2 = 1;
  while (1) {
    if (pos1 == pos2)
      break;
    pos2 = a[pos2], b++;
  }
  int c = 0;
  if (num <= b) {
    pos2 = 1;
    while (1) {
      if (num == c)
        break;
      pos2 = a[pos2], c++;
    }
    printf("%d\\n", pos2);
    return 0;
  }
  num -= b;
  num %= t;
  pos2 = pos1;
  c = 0;
  while (1) {
    if (num == c)
      break;
    pos2 = a[pos2], c++;
  }
  printf("%d\\n", pos2);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000009
#define ll long long
using namespace std;
long long int n, num, a[20005], vis[20005];
int main() {
  scanf("%lld%lld", &n, &num);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]), vis[i] = 0;
  long long int pos1 = 1, pos2;
  while (vis[pos1] != 1) {
    vis[pos1] = 1;
    pos1 = a[pos1];
  }
  pos2 = pos1;
  for (int j = 1; j <= n; j++)
    vis[j] = 0;
  int t = 0;
  while (vis[pos2] != 1) {
    vis[pos2] = 1;
    t++;
    pos2 = a[pos2];
  }
  int b = 0;
  pos2 = 1;
  while (1) {
    if (pos1 == pos2)
      break;
    pos2 = a[pos2], b++;
  }
  int c = 0;
  if (num <= b) {
    pos2 = 1;
    while (1) {
      if (num == c)
        break;
      pos2 = a[pos2], c++;
    }
    printf("%d\\n", pos2);
    return 0;
  }
  num -= b;
  num %= t;
  pos2 = pos1;
  c = 0;
  while (1) {
    if (num == c)
      break;
    pos2 = a[pos2], c++;
  }
  printf("%d\\n", pos2);
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define forin(in)                                                              \\
  for (int i = 0; i < (int)in.size(); i++)                                     \\
  cin >> in[i]
#define forout(out)                                                            \\
  for (int i = 0; i < (int)out.size(); i++)                                    \\
  cout << out[i] << endl
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) {
    cin >> A[i];
    A[i]--;
  }

  deque<int> a;
  vector<bool> seen(N, false);
  int cur = 0;

  while (true) {
    if (seen[cur]) {
      while (a[0] != cur) {
        K--;
        a.pop_front();

        if (K == 0) {
          cout << a[0] + 1 << endl;
          return 0;
        }
      }
      break;
    }
    a.push_back(cur);
    seen[cur] = true;
    cur = A[cur];
  }
  cout << a[K % a.size()] + 1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define forin(in)                                                              \\
  for (int i = 0; i < (int)in.size(); i++)                                     \\
  cin >> in[i]
#define forout(out)                                                            \\
  for (int i = 0; i < (int)out.size(); i++)                                    \\
  cout << out[i] << endl
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> judge(n, 0), a(n);
  vector<ll> tele;

  rep(i, n) {
    cin >> a[i];
    a[i]--;
  }

  ll cur = 0;
  while (true) {
    if (judge[cur] == 2)
      break;
    if (judge[cur] == 1)
      tele.push_back(cur + 1);
    judge[cur]++;
    cur = a[cur];
  }

  ll cnt;
  rep(i, n) {
    if (a[i] == tele[0] - 1) {
      cnt = i;
      break;
    }
  }
  if (k - cnt < 0)
    cout << tele[k] << endl;
  else
    cout << tele[(k - cnt) % tele.size()] << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define Rep(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define For(i, a, b) for (long long i = (a); i < b; i++)
typedef long long ll;

using namespace std;
int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N, 0);
  Rep(i, N) {
    cin >> A[i];
    A[i]--;
  }
  ll cur_town = 0;
  set<ll> hist{0};
  map<ll, ll> dict{{0, 0}}; // 初期位置、移動回数0
  ll loop_num = 0;
  ll loop_start = 0;
  bool finished = false;
  For(i, 1, K + 1) {
    cur_town = A[cur_town];
    if (hist.find(cur_town) != hist.end()) {
      loop_num = i - dict[cur_town];
      loop_start = dict[cur_town];
      finished = true;
      break;
    }
    dict[cur_town] = i; // 位置、それまでの移動回数
    hist.insert(cur_town);
    if (finished)
      break;
  }
  ll rest = finished ? (K - loop_start) % loop_num : K;
  cur_town = 0;
  ll pre_town = 0, post_town = 0;
  // cout << "rest:" << rest << endl;
  // cout << "loopnum:" << loop_num << endl;
  // cout << "loopstart:" << loop_start << endl;
  For(i, 0, rest + loop_start) {
    cur_town = A[cur_town];
    if (i == rest + loop_start - 2)
      pre_town = cur_town + 1;
    // if (cur_town == 60213)
    //     cout << "found60214 at:" << i << endl;
  }
  post_town = A[cur_town] + 1;
  cur_town++;
  cout << cur_town << endl;
  // cout << "pre:" << pre_town << endl;
  // cout << "post:" << post_town << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)

using namespace std;
int main() {
  long long N, K;
  cin >> N >> K;
  vector<int> A(N, 0);
  REP(i, N) {
    cin >> A[i];
    A[i]--;
  }
  // cout << N << K << endl;
  // REP(i, N){
  //     cout << A[i] << endl;
  // }
  int cur_town = 0;
  vector<int> hist(N);
  for (long i = 0; i < N + 1; i++) {
    hist[i] = cur_town;
    cur_town = A[cur_town];
  }
  int loop_num = 0;
  int loop_start = 0;
  REP(i, N + 1) {
    bool finished = false;
    REP(j, N + 1) {
      if (i != j && hist[i] == hist[j]) {
        loop_num = j - i;
        loop_start = i;
        finished = true;
        break;
      }
    }
    if (finished)
      break;
  }
  long rest = (K - loop_start) % loop_num;
  cur_town = 0;
  for (long i = 0; i < rest + loop_start; i++) {
    cur_town = A[cur_town];
  }
  cur_town++;
  cout << cur_town << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
typedef long long int ll;
const ll N = 2 * 1e5 + 10;
using namespace std;
ll a[N];

int main() {
  // your code goes here
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // cout.tie(NULL);
  ll n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<ll> c;
  vector<int> vis(n + 1, 0);
  int next = 1;
  while (!vis[a[next]]) {
    vis[a[next]] = 1;
    c.push_back(a[next]);
    next = a[next];
  }
  ll z = c.size();
  // for(auto i:c)
  // cout<<i<<"\\n";
  if (k <= z) {
    cout << c[k - 1];
    return 0;
  }

  ll idx = 0;
  for (int i = 0; i < z; i++) {
    if (c[i] == a[c[z - 1]]) {
      idx = i;
      break;
    }
  }
  ll size = k - idx;

  ll r = size % (z - idx);
  // cout<<r<<"\\n";

  if (r == 0)
    cout << c[z - 1];
  else
    cout << c[idx + r - 1];

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
const ll N = 1e5 + 10;
using namespace std;
ll a[N];

int main() {
  // your code goes here
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  // cout.tie(NULL);
  ll n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> a[i];
  vector<ll> c;
  vector<int> vis(n + 1, 0);
  int next = 1;
  while (!vis[a[next]]) {
    vis[a[next]] = 1;
    c.push_back(a[next]);
    next = a[next];
  }
  ll z = c.size();
  // for(auto i:c)
  // cout<<i<<"\\n";
  if (k <= z) {
    cout << c[k - 1];
    return 0;
  }

  ll idx = 0;
  for (int i = 0; i < z; i++) {
    if (c[i] == a[c[z - 1]]) {
      idx = i;
      break;
    }
  }
  ll size = k - idx;

  ll r = size % (z - idx);
  // cout<<r<<"\\n";
  if (r == 0)
    cout << c[z - 1];
  else {
    if (idx + r - 1 < 0 || idx + r - 1 >= z)
      cout << c[idx];
    else
      cout << c[idx + r - 1];
  }

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

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

using ll = long long;
using P = pair<int, int>;

bool flag[200010];
vector<ll> v;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N + 1];
  for (int i = 1; i < N + 1; i++)
    cin >> a[i];

  ll now = 1;
  flag[now] = true;
  while (1) {
    now = a[now];
    if (flag[now])
      break;
    flag[now] = true;
  }

  ll pivot = now;
  v.push_back(pivot);
  while (1) {
    now = a[now];
    if (now == pivot)
      break;
    v.push_back(now);
  }

  ll ans = -1;
  ll cnt = 1;
  now = 1;
  while (1) {
    if (now == pivot)
      break;
    now = a[now];
    if (cnt == K) {
      ans = now;
      break;
    }
    cnt++;
  }

  if (ans != -1) {
    cout << ans << endl;
  } else {
    ll tmp = K - (cnt - 1);
    ll sz = v.size();
    cout << v[tmp % sz] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using ll = long long;
using P = pair<int, int>;

bool flag[200010];
vector<ll> v;

int main() {
  ll N, K;
  cin >> N >> K;
  ll a[N + 1];
  for (int i = 1; i < N + 1; i++)
    cin >> a[i];

  ll now = 1;
  flag[now] = true;
  while (1) {
    now = a[now];
    if (flag[now])
      break;
    flag[now] = true;
  }

  ll pivot = now;
  v.push_back(pivot);
  while (1) {
    now = a[now];
    if (now == pivot)
      break;
    v.push_back(now);
  }

  ll cnt = 1;
  now = 1;
  while (1) {
    if (now == pivot)
      break;
    now = a[now];
    if (cnt == K) {
      cout << now << endl;
      return 0;
    }
    cnt++;
  }

  ll tmp = v.size() - (cnt - 1);

  cout << v[K % tmp] << endl;
  return 0;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define sz(x) ((int)(x).size())
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define db(x)                                                                  \\
  { cerr << #x << " = " << x << endl; }
using namespace std;

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int32_t main() {
  c_p_c();
  int n, k;
  cin >> n >> k;
  vector<int> a, p;
  unordered_set<int> check;
  a.push_back(0);
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  p.push_back(1);
  check.insert(1);
  int index = 1;
  while (1) {
    if (check.count(a[index]) == 1) {
      break;
    }
    p.push_back(a[index]);
    check.insert(a[index]);
    index = a[index];
  }
  int s = p.size();
  if (a[index] == 1) {
    cout << p[k % s] << endl;
  } else {
    if (k < s) {
      cout << p[k % s] << endl;
    } else {
      vector<int> b;
      int f = 0;
      for (int i = 0; i < s; i++) {
        if (a[index] == p[i]) {
          k = k - i;
          f = 1;
        }
        if (f == 1)
          b.push_back(p[i]);
      }
      s = b.size();
      cout << b[k % s] << endl;
    }
  }

  return 0;
}

// g++ -std=c++17 -Wshadow -Wall -o "sol" "sol.cpp" -g -fsanitize=address
// -fsanitize=undefined -D_GLIBCXX_DEBUG


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define sz(x) ((int)(x).size())
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define db(x)                                                                  \\
  { cerr << #x << " = " << x << endl; }
using namespace std;

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int32_t main() {
  c_p_c();
  int n, k;
  cin >> n >> k;
  vector<int> a, p;
  a.push_back(0);
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  p.push_back(1);
  int index = 1;
  while (1) {
    if (a[index] == 1) {
      break;
    }
    p.push_back(a[index]);
    index = a[index];
  }
  int s = p.size();
  cout << p[k % s] << endl;
  ;
  return 0;
}

// g++ -std=c++17 -Wshadow -Wall -o "sol" "sol.cpp" -g -fsanitize=address
// -fsanitize=undefined -D_GLIBCXX_DEBUG
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0), check(N + 1, 0);
  set<ll> S;
  deque<ll> D;
  for (ll i = 1; i <= N; i++)
    cin >> A[i];
  ll next = 1;
  for (ll i = 1; i <= N + 1; i++) { // loop探す
    if (S.count(next) > 0) {
      while (D.front() != next && K > 0) {
        D.pop_front();
        K--;
      }
      break;
    }
    S.insert(next);
    D.push_back(next);
    next = A[next];
  }
  ll loop = D.size(), aft;
  if (loop == 0)
    cout << D[0] << endl;
  else {
    aft = K % loop;
    cout << D[aft] << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0), check(N + 1, 0);
  set<ll> S;
  deque<ll> D;
  for (ll i = 1; i <= N; i++)
    cin >> A[i];
  ll next = 1;
  for (ll i = 1; i <= N + 1; i++) { // loop探す
    if (S.count(next)) {
      while (D.front() != next)
        D.pop_front();
      break;
    }
    S.insert(next);
    D.push_back(next);
    next = A[next];
    K--;
  }
  ll loop = D.size(), aft;
  if (K == 0)
    cout << next << endl;
  else {
    aft = K % loop;
    cout << D[aft] << endl;
  }
}
Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*

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

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 10, 0), seen(N + 10, 0);
  rep(i, N) cin >> A[i + 1];
  ll i = 1;
  while (seen[i] < 2 && K) {
    seen[i]++;
    K--;
    i = A[i];
  }
  ll count = 0;
  rep(i, N + 1) {
    if (seen[i] == 2)
      count++;
  }
  if (count > 0)
    K %= count;
  while (K) {
    K--;
    i = A[i];
  }
  cout << i << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
/*

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

  ll N, K;
  cin >> N >> K;
  vector<ll> A(N + 1, 0), seen(N + 1, 0);
  rep(i, N) cin >> A[i + 1];
  ll i = 1;
  while (seen[i] < 2 && K) {
    seen[i]++;
    K--;
    i = A[i];
  }
  ll count = 0;
  rep(i, N) {
    if (seen[i] == 2)
      count++;
  }
  K %= count;
  while (K) {
    K--;
    i = A[i];
  }
  cout << i << endl;
}
Problem p02684 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  ll K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<int> went_place;
  vector<int> ord(N + 1, -1);
  int c = 1, l = 0;
  {
    int v = 1;
    while (ord[v] == -1) {
      ord[v] = went_place.size();
      went_place.push_back(v);
      v = A[v - 1];
    }
    c = went_place.size() - ord[v];
    l = ord[v]; // 例外サイズ
  }

  if (K < l)
    cout << went_place[K] << endl;
  else {
    K -= l;
    K %= c;
    cout << went_place[l + K] << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  int64_t K;
  cin >> N >> K;
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A.at(i);

  int now_place = 1;

  for (int i = 1; i <= K; i++) {
    int next_place = A.at(now_place);
    now_place = next_place;
  }

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

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

static const long MM = 998244353;
static const long MAX = 200000;
long a[MAX], b[MAX], Mm[MAX];
long N, M, K;

long power(long n, long m) {
  long ans = 1;
  if (m > 0) {
    ans = power(n, m / 2);
    if (m % 2 == 0)
      ans = (ans * ans) % MM;
    else
      ans = (((ans * ans) % MM) * n) % MM;
  }
  return ans;
}

void setab() {
  a[0] = 1;
  b[0] = 1;
  for (long i = 1; i < N; i++) {
    a[i] = a[i - 1] * i % MM;
    b[i] = b[i - 1] * power(i, MM - 2) % MM;
  }
  return;
}

void setMm() {
  Mm[0] = 1;
  for (long i = 1; i < N; i++) {
    Mm[i] = Mm[i - 1] * (M - 1) % MM;
  }
  return;
}

int main() {
  cin >> N >> M >> K;
  setab();
  setMm();
  long ans = 0;
  for (int i = 0; i < K + 1; i++) {
    ans =
        (ans + (((((((a[N - 1] * b[i]) % MM) * b[N - 1 - i]) % MM) * M) % MM) *
                Mm[N - 1 - i])) %
        MM;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

static const long MAX = 998244353;

int main() {
  long N, M, K;
  cin >> N >> M >> K;
  long dp[N][K + 1];
  fill(dp[0], dp[0] + K + 1, 0);
  dp[0][0] = M;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= K; j++) {
      if (j) {
        dp[i][j] = dp[i - 1][j - 1] % MAX + (M - 1) * dp[i - 1][j] % MAX;
      } else {
        dp[i][j] = (M - 1) * dp[i - 1][j] % MAX;
      }
    }
  }
  long ans = 0;
  for (long i = 0; i <= K; i++) {
    ans = (ans + dp[N - 1][i]) % MAX;
  }
  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
#define MOD 998244353LL

long long int modpow(long long x, long long y) {
  if (y == 0)
    return 1;
  if (y < 0)
    return 0;
  if (y % 2)
    return (modpow(x, y - 1) * x) % MOD;
  else {
    long long buf = modpow(x, y / 2);
    return (buf * buf) % MOD;
  }
}

long long fac[300000] = {};
long long factorial(long long x) {
  if (x <= 0)
    return 1;
  if (fac[x] != 0)
    return fac[x];
  return fac[x] = (x * factorial(x - 1)) % MOD;
}

long long ncr(long long n, long long r) {
  return (factorial(n) *
          modpow((factorial(n - r) * factorial(r) % MOD), MOD - 2)) %
         MOD;
}

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  long long ans = 0;
  for (int i = 0; k >= i; i++) {
    ans += (((modpow(m - 1, n - i - 1) * m) % MOD) * ncr(n - 1, i)) % MOD;
    ans %= MOD;
  }

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
#define MOD 998244353LL

long long int modpow(long long x, long long y) {
  if (y == 0)
    return 1;
  if (y < 0)
    return 0;
  if (y % 2)
    return (modpow(x, y - 1) * x) % MOD;
  else {
    long long buf = modpow(x, y / 2);
    return (buf * buf) % MOD;
  }
}

long long ncr(long long n, long long r) {
  long long bo = 1, si = 1;
  for (int i = 0; r > i; i++) {
    si *= n - i;
    si %= MOD;
  }
  for (int i = 1; r >= i; i++) {
    bo *= i;
    bo %= MOD;
  }
  return (si * modpow(bo, MOD - 2)) % MOD;
}

int main() {
  long long n, m, k;
  cin >> n >> m >> k;
  long long ans = 0;
  for (int i = 0; k >= i; i++) {
    ans += (((modpow(m - 1, n - i - 1) * m) % MOD) * ncr(n - 1, i)) % MOD;
    ans %= MOD;
  }

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

#include <iostream>
#include <vector>

uint32_t pow_mod(uint32_t base, uint32_t exp, uint32_t mod) {
  if (exp == 0)
    return 1;

  uint64_t half = pow_mod(base, exp / 2, mod);
  uint64_t ans = (half * half) % mod;
  return (ans * (exp % 2 == 0 ? 1ll : base)) % mod;
}

uint32_t inv_mod(uint32_t x, uint32_t mod) { return pow_mod(x, mod - 2, mod); }

uint32_t comb_mod(uint32_t n, uint32_t k, const std::vector<uint64_t> &fact_mod,
                  uint32_t mod) {
  if (n < k)
    return -1;
  return (fact_mod[n] * inv_mod((fact_mod[n - k] * fact_mod[k]) % mod, mod)) %
         mod;
}

int main() {
  const uint32_t mod = 998'244'353;
  uint32_t N = 0, M = 0, K = 0;
  std::cin >> N >> M >> K;

  std::vector<uint64_t> fact_mod(N, 1);
  for (size_t i = 1; i < fact_mod.size(); i++)
    fact_mod[i] = (i * fact_mod[i - 1]) % mod;

  uint32_t ans = 0;
  for (size_t k = 0; k < K + 1; k++) {
    uint64_t pow = pow_mod(M - 1, N - 1 - k, mod);
    pow = (pow * M) % mod;
    uint64_t comb = comb_mod(N - 1, k, fact_mod, mod);
    ans = (ans + (pow * comb) % mod) % mod;
  }

  std::cout << ans << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

uint32_t pow_mod(uint32_t base, uint32_t exp, uint32_t mod) {
  if (exp == 1)
    return base % mod;

  uint64_t half = pow_mod(base, exp / 2, mod);
  uint64_t ans = (half * half) % mod;
  return (ans * (exp % 2 == 0 ? 1ll : base)) % mod;
}

uint32_t inv_mod(uint32_t x, uint32_t mod) { return pow_mod(x, mod - 2, mod); }

uint32_t comb_mod(uint32_t n, uint32_t k, const std::vector<uint64_t> &fact_mod,
                  uint32_t mod) {
  if (n < k)
    return -1;
  return (fact_mod[n] * inv_mod((fact_mod[n - k] * fact_mod[k]) % mod, mod)) %
         mod;
}

int main() {
  const uint32_t mod = 998'244'353;
  uint32_t N = 0, M = 0, K = 0;
  std::cin >> N >> M >> K;

  std::vector<uint64_t> fact_mod(N, 1);
  for (size_t i = 1; i < fact_mod.size(); i++)
    fact_mod[i] = (i * fact_mod[i - 1]) % mod;

  uint32_t ans = 0;
  for (size_t k = 0; k < K + 1; k++) {
    uint64_t pow = pow_mod(M - 1, N - 1 - k, mod);
    pow = (pow * M) % mod;
    uint64_t comb = comb_mod(N - 1, k, fact_mod, mod);
    ans = (ans + (pow * comb) % mod) % mod;
  }

  std::cout << ans << std::endl;

  return 0;
}