prithivMLmods/Coder-Stat huggingface dataset:
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
// WeirdBugsButOkay

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

using namespace std;

const ll N = 100001;
ll a[N], vis[N];
ll ccs, pl;
vector<ll> seq;

void dfs(ll u, ll p = 0) {
  vis[u] = vis[p] + 1;
  seq.pb(u);
  ll v = a[u];
  if (!vis[v])
    dfs(v, u);
  else
    ccs = vis[u] + 1 - vis[v];
}

void solve() {
  ll n, x;
  cin >> n >> x;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  pl = seq.size() - ccs;
  // cout << pl << ccs << endl;
  if (x < seq.size())
    cout << seq[x] << endl;
  else {
    ll tot = x - pl;
    tot %= ccs;
    cout << seq[pl + tot] << endl;
  }
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}

Fixed submission:
// WeirdBugsButOkay

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

using namespace std;

const ll N = 200001;
ll a[N], vis[N];
ll ccs, pl;
vector<ll> seq;

void dfs(ll u, ll p = 0) {
  vis[u] = vis[p] + 1;
  seq.pb(u);
  ll v = a[u];
  if (!vis[v])
    dfs(v, u);
  else
    ccs = vis[u] + 1 - vis[v];
}

void solve() {
  ll n, x;
  cin >> n >> x;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  dfs(1);
  pl = seq.size() - ccs;
  // cout << pl << ccs << endl;
  if (x < seq.size())
    cout << seq[x] << endl;
  else {
    ll tot = x - pl;
    tot %= ccs;
    cout << seq[pl + tot] << endl;
  }
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int q = 1;
  // cin >> q;
  for (int i = 1; i <= q; i++) {
    solve();
  }
  return 0;
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
  }
  vector<ll> s;

  ll t = n + 1;
  ll i = 1;
  ll l = 0;
  bool flag = true;
  set<ll> r;

  while (t--) {
    if (r.find(i) != r.end()) {
      auto it = find(s.begin(), s.end(), i);
      if (k < (it - s.begin()))
        ;
      { cout << s[k] << endl; }
      k -= (ll)(it - s.begin());
      ll temp = s.end() - it;
      flag = false;
      ll y = (k % temp + temp) % temp;
      cout << *(it + y) << endl;
      break;
    } else {
      r.insert(i);
      s.push_back(i);
      i = a[i - 1];
    }
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

void solve() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (ll i = 0; i < n; ++i) {
    /* code */
    cin >> a[i];
  }
  vector<ll> s;

  ll t = n + 1;
  ll i = 1;
  ll l = 0;
  bool flag = true;
  set<ll> r;

  while (t--) {
    if (r.find(i) != r.end()) {
      auto it = find(s.begin(), s.end(), i);
      // cout<<it-s.begin()<<endl;
      if (k < (ll)(it - s.begin())) {
        cout << s[k] << endl;
        break;
      }
      k -= (ll)(it - s.begin());
      ll temp = s.end() - it;
      flag = false;
      ll y = (k % temp + temp) % temp;
      cout << *(it + y) << endl;
      break;
    } else {
      r.insert(i);
      s.push_back(i);
      i = a[i - 1];
    }
  }
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;
struct custom_hash {
  static unsigned long long splitmix64(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(unsigned long long x) const {
    static const unsigned long long FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
mt19937
    random_generator(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5 + 3;

int n;
long long k;
int a[maxn];
void read() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}

bool used[maxn];
void solve() {
  vector<int> v;
  int curr = 1;
  while (!used[curr]) {
    used[curr] = true;
    v.push_back(curr);

    curr = a[curr];
  }

  for (int i = 0; i < (int)v.size(); i++)
    if (v[i] == curr) {
      if (k < i)
        cout << v[k] << endl;

      else {
        int r = k - i;
        int l = (int)v.size() - i;

        r %= l;
        cout << v[i + r] << endl;
      }

      return;
    }
}

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

  read();
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;
struct custom_hash {
  static unsigned long long splitmix64(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(unsigned long long x) const {
    static const unsigned long long FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
mt19937
    random_generator(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 2e5 + 3;

int n;
long long k;
int a[maxn];
void read() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}

bool used[maxn];
void solve() {
  vector<int> v;
  int curr = 1;
  while (!used[curr]) {
    used[curr] = true;
    v.push_back(curr);

    curr = a[curr];
  }

  for (int i = 0; i < (int)v.size(); i++)
    if (v[i] == curr) {
      if (k < i)
        cout << v[k] << endl;

      else {
        long long r = k - i;
        int l = (int)v.size() - i;

        r %= l;
        cout << v[i + r] << endl;
      }

      return;
    }
}

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

  read();
  solve();
}
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int pos = 1;
  int key = 0;
  vector<int> count(n + 1);
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    count[pos]++;
    if (count[pos] == 2) {
      key = pos;
      break;
    }
  }

  int preCount = 0;
  int keyCount = 0;
  pos = 1;
  vector<int> town;
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    if (pos == key)
      keyCount++;
    if (keyCount == 0)
      preCount++;
    else if (keyCount == 1)
      town.push_back(pos);
    else if (keyCount == 2)
      break;
  }
  int back = town[town.size() - 1];
  town.pop_back();
  town.insert(town.begin(), back);
  cout << town[(k - preCount) % town.size()] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int pos = 1;
  int key = 0;
  vector<int> count(n + 1);
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    count[pos]++;
    if (count[pos] == 2) {
      key = pos;
      break;
    }
  }

  int preCount = 0;
  int keyCount = 0;
  pos = 1;
  vector<int> town;
  for (int i = 1; i <= k; i++) {
    pos = a[pos];
    if (pos == key)
      keyCount++;
    if (keyCount == 0)
      preCount++;
    else if (keyCount == 1)
      town.push_back(pos);
    else if (keyCount == 2)
      break;
  }
  if (0 <= k - preCount - 1)
    cout << town[(k - preCount - 1) % town.size()] << endl;
  else {
    int pos = 1;
    for (int i = 1; i <= k; i++)
      pos = a[pos];
    cout << pos << endl;
  }
}
Problem p02684 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    import sys

    n, k, *a = map(int, sys.stdin.read().split())
    a = [0] + a
    check = [0] * (n + 1)
    check[1] = 1

    i = 0
    nw = 1
    path = [1]
    while 1:
        nx = a[nw]
        i += 1
        if i == k:
            print(nx)
            break
        if check[nx]:
            j = path.index(nx)
            d = j + (k - i) % (i - j)
            print(path[d])
            break
        path = path + [nx]
        check[nx] = 1
        nw = nx


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    import sys

    n, k, *a = map(int, sys.stdin.read().split())
    a = [0] + a
    check = [0] * (n + 1)
    check[1] = 1

    i = 0
    nw = 1
    path = [1]
    while 1:
        nx = a[nw]
        i += 1
        if i == k:
            print(nx)
            break
        if check[nx]:
            j = path.index(nx)
            d = j + (k - i) % (i - j)
            print(path[d])
            break
        path.append(nx)
        check[nx] = 1
        nw = nx


if __name__ == "__main__":
    main()
Problem p02684 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    R = [False] * (N + 1)
    D = []

    p = 1

    while True:
        if R[p]:
            break
        D.append(p)
        R[p] = True
        p = A[p - 1]

    m = D.index(p)
    c = len(D) - m

    if K <= m:
        print(D[K])
    else:
        print(D[m + K - m] % c)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    R = [False] * (N + 1)
    D = []

    p = 1

    while True:
        if R[p]:
            break
        D.append(p)
        R[p] = True
        p = A[p - 1]

    m = D.index(p)
    c = len(D) - m

    if K <= m:
        print(D[K])
    else:
        print(D[m + (K - m) % c])


if __name__ == "__main__":
    main()
Problem p02684 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> s;
  vector<ll> ord(n + 1, -1);
  ll c = 1, l = 0;
  ll v = 1;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - ord[v];
  l = ord[v];
  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[(k - l) % c + k] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  ll k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  vector<ll> s;
  vector<ll> ord(n + 1, -1);
  ll c = 1, l = 0;
  ll v = 1;
  while (ord[v] == -1) {
    ord[v] = s.size();
    s.emplace_back(v);
    v = a[v - 1];
  }
  c = s.size() - ord[v];
  l = ord[v];
  if (k < l)
    cout << s[k] << endl;
  else {
    k -= l;
    k %= c;
    cout << s[l + k] << endl;
  }
}
Problem p02684 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
portals = [0] + list(map(int, input().split()))

visitTowns = list()
visitTimes = [0 for _ in range(N + 1)]

curTown = 1
timeBackTo = 0

curTime = 0
while True:
    if visitTimes[curTown] > 0:
        timeBackTo = visitTimes[curTown]
        break

    visitTowns.append(curTown)
    visitTimes[curTown] = curTime

    # teleport
    curTown = portals[curTown]

    curTime += 1
    print(visitTowns, visitTimes[1:])

nonLoopCount = timeBackTo
loopSpan = len(visitTowns) - nonLoopCount

if K <= nonLoopCount:
    print(visitTowns[K])
    exit()

rem = (K - nonLoopCount) % loopSpan
print(visitTowns[nonLoopCount + rem])


Fixed submission:
N, K = map(int, input().split())
portals = [0] + list(map(int, input().split()))

visitTowns = list()
visitTimes = [0 for _ in range(N + 1)]

curTown = 1
timeBackTo = 0

curTime = 0
while True:
    if visitTimes[curTown] > 0:
        timeBackTo = visitTimes[curTown]
        break

    visitTowns.append(curTown)
    visitTimes[curTown] = curTime

    # teleport
    curTown = portals[curTown]

    curTime += 1

nonLoopCount = timeBackTo
loopSpan = len(visitTowns) - nonLoopCount

if K <= nonLoopCount:
    print(visitTowns[K])
    exit()

rem = (K - nonLoopCount) % loopSpan
print(visitTowns[nonLoopCount + rem])
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair

#define BASEP 998244353

ll powmod(ll x, ll pow, ll mod) {
  ll result = 1;
  while (pow > 0) {
    if (pow & 1) {
      result *= x;
      result %= mod;
    }

    x *= x;
    x %= mod;

    pow >>= 1;
  }
  return result;
}

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

  ll Ndash = N - 1;
  ll Mdash = M - 1;

  // 累乗を求めておく
  vector<ll> vpow(Ndash + 1);
  vpow[0] = 1;
  for (ll i = 0; i < Ndash; i++) {
    vpow[i + 1] = (vpow[i] * Mdash) % BASEP;
  }

  // 階乗を求めておく
  vector<ll> vfact(Ndash + 1);
  vfact[0] = 1;
  for (ll i = 0; i <= Ndash;) {
    ll back = vfact[i];
    i++;
    vfact[i] = (back * i) % BASEP;
  }

  // 階乗の逆元を求めておく
  vector<ll> vinv(Ndash + 1);
  for (ll i = 0; i <= Ndash; i++) {
    vinv[i] = powmod(vfact[i], BASEP - 2, BASEP);
  }

  // 組み合わせnCkを求めておく
  vector<ll> vnck(Ndash + 1);
  vnck[0] = vnck[Ndash] = 1;
  for (ll i = 1; i <= Ndash / 2; i++) {
    ll result = (vfact[Ndash] * vinv[i]) % BASEP;
    result = (result * vinv[Ndash - i]) % BASEP;
    vnck[i] = vnck[Ndash - i] = result;
  }

  // 目的の数式を計算する
  ll result = 0;
  for (ll i = 0; i <= K; i++) {
    result += (vnck[i] * vpow[Ndash - i]) % BASEP;
    result %= BASEP;
  }
  result *= M;
  result %= BASEP;
  OUTX(result);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define INX(x) (cin >> x)
#define OUTX(x) (cout << x << endl)

#define ADD emplace_back
#define MP make_pair

#define BASEP 998244353

ll powmod(ll x, ll pow, ll mod) {
  ll result = 1;
  while (pow > 0) {
    if (pow & 1) {
      result *= x;
      result %= mod;
    }

    x *= x;
    x %= mod;

    pow >>= 1;
  }
  return result;
}

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

  ll Ndash = N - 1;
  ll Mdash = M - 1;

  // 累乗を求めておく
  vector<ll> vpow(Ndash + 1);
  vpow[0] = 1;
  for (ll i = 0; i < Ndash; i++) {
    vpow[i + 1] = (vpow[i] * Mdash) % BASEP;
  }

  // 階乗を求めておく
  // vector<ll> vfact(Ndash + 1);
  // //ここvectorだとなぜかデストラクタ？で例外が起きる
  ll vfact[Ndash + 1];
  vfact[0] = 1;
  for (ll i = 0; i <= Ndash;) {
    ll back = vfact[i];
    i++;
    vfact[i] = (back * i) % BASEP;
  }

  // 階乗の逆元を求めておく
  vector<ll> vinv(Ndash + 1);
  for (ll i = 0; i <= Ndash; i++) {
    vinv[i] = powmod(vfact[i], BASEP - 2, BASEP);
  }

  // 組み合わせnCkを求めておく
  vector<ll> vnck(Ndash + 1);
  vnck[0] = vnck[Ndash] = 1;
  for (ll i = 1; i <= Ndash / 2; i++) {
    ll result = (vfact[Ndash] * vinv[i]) % BASEP;
    result = (result * vinv[Ndash - i]) % BASEP;
    vnck[i] = vnck[Ndash - i] = result;
  }

  // 目的の数式を計算する
  ll result = 0;
  for (ll i = 0; i <= K; i++) {
    result += (vnck[i] * vpow[Ndash - i]) % BASEP;
    result %= BASEP;
  }
  result *= M;
  result %= BASEP;
  OUTX(result);

  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
static const ll MOD = 998244353;
static const int MAX = 100000;

ll 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;
  }
}

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;
}

ll ModPow(ll a, ll cur = MOD - 2) {
  ll r = 1;
  while (cur)
    r = r * ((cur % 2) ? a : 1) % MOD, a = a * a % MOD, cur >>= 1;
  return r;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  /*
  先頭の選び方がM
  iが[0,K]で、
  先頭と同じ色のブロックの選び方はcomb(N-1,i)
  残りのブロックの色の選び方はM^(N-1-i);
  */

  ll ans = 0;
  COMinit();

  for (int i = 0; i <= K; i++) {
    ans += M * COM(N - 1, i) % MOD * ModPow(M - 1, N - 1 - i) % MOD;
  }

  cout << ans % MOD << endl;

  return 0;
}

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

typedef long long ll;
static const ll MOD = 998244353;
static const int MAX = 500000 + 100;

ll 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;
  }
}

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;
}

ll ModPow(ll a, ll cur = MOD - 2) {
  ll r = 1;
  while (cur)
    r = r * ((cur % 2) ? a : 1) % MOD, a = a * a % MOD, cur >>= 1;
  return r;
}

int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  /*
  先頭の選び方がM
  iが[0,K]で、
  先頭と同じ色のブロックの選び方はcomb(N-1,i)
  残りのブロックの色の選び方はM^(N-1-i);
  */

  ll ans = 0;
  COMinit();

  for (int i = 0; i <= K; i++) {
    ans += M * COM(N - 1, i) % MOD * ModPow(M - 1, N - 1 - i) % MOD;
  }

  cout << ans % MOD << endl;

  return 0;
}
Problem p02685 (C++)

Buggy submission (status: 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 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;
}

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 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;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int M = 998244353;
const int MAX = 1e5 + 10;

long long kai[MAX], kair[MAX], rev[MAX];

void com_p() {
  kai[0] = kai[1] = 1;
  kair[0] = kair[1] = 1;
  rev[1] = 1;
  for (int i = 2; i < MAX; i++) {
    kai[i] = kai[i - 1] * i % M;
    rev[i] = M - rev[M % i] * (M / i) % M;
    kair[i] = kair[i - 1] * rev[i] % M;
  }
}
long long ncr(int n, int r) {
  if (n < 0 || r < 0)
    return 0;
  if (n < r)
    return 0;
  return kai[n] * (kair[r] * kair[n - r] % M) % M;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  com_p();
  long long sum = 0, x = m;
  for (int i = 0; i < n - k - 1; i++) {
    x = x * (m - 1) % M;
  }
  for (int i = k; i >= 0; i--) {
    sum += x * ncr(n - 1, i) % M;
    sum %= M;
    x = x * (m - 1) % M;
  }
  cout << sum << endl;
}

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

const int M = 998244353;
const int MAX = 1e5 * 2 + 5;

long long kai[MAX], kair[MAX], rev[MAX];

void com_p() {
  kai[0] = kai[1] = 1;
  kair[0] = kair[1] = 1;
  rev[1] = 1;
  for (int i = 2; i < MAX; i++) {
    kai[i] = kai[i - 1] * i % M;
    rev[i] = M - rev[M % i] * (M / i) % M;
    kair[i] = kair[i - 1] * rev[i] % M;
  }
}
long long ncr(int n, int r) {
  if (n < 0 || r < 0)
    return 0;
  if (n < r)
    return 0;
  return kai[n] * (kair[r] * kair[n - r] % M) % M;
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  com_p();
  long long sum = 0, x = m;
  for (int i = 0; i < n - k - 1; i++) {
    x = x * (m - 1) % M;
  }
  for (int i = k; i >= 0; i--) {
    sum += x * ncr(n - 1, i) % M;
    sum %= M;
    x = x * (m - 1) % M;
  }
  cout << sum << endl;
}
Problem p02685 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \
      cout << fixed << setprecision(20);
#define int long long
#define double long double
const int INF = 1e18, MOD = 998244353;

vector<int> fact;

int modinit(int n) {
  fact.push_back(1);
  int now = 1;
  for (int i = 1; i <= n; i++) {
    now *= i;
    now %= MOD;
    fact.push_back(now);
  }
}

int modpow(int x, int n) {
  return n == 0 ? 1 : modpow(x * x % MOD, n / 2) * (n % 2 ? x : 1) % MOD;
}

int modcomb(int n, int r) {
  if (n < r)
    return 0;
  return fact[n] * modpow(fact[n - r], MOD - 2) % MOD *
         modpow(fact[r], MOD - 2) % MOD;
}

signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int res = 0;
  modinit(222222);
  for (int i = 0; i <= k; i++) {
    res += m * modcomb(n - 1, i) % MOD * modpow(m - 1, n - i - 1) % MOD;
    res %= MOD;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \
      cout << fixed << setprecision(20);
#define int long long
#define double long double
const int INF = 1e18, MOD = 998244353;

vector<int> fact;

void modinit(int n) {
  fact.push_back(1);
  int now = 1;
  for (int i = 1; i <= n; i++) {
    now *= i;
    now %= MOD;
    fact.push_back(now);
  }
}

int modpow(int x, int n) {
  return n == 0 ? 1 : modpow(x * x % MOD, n / 2) * (n % 2 ? x : 1) % MOD;
}

int modcomb(int n, int r) {
  if (n < r)
    return 0;
  return fact[n] * modpow(fact[n - r], MOD - 2) % MOD *
         modpow(fact[r], MOD - 2) % MOD;
}

signed main() {
  int n, m, k;
  cin >> n >> m >> k;
  int res = 0;
  modinit(222222);
  for (int i = 0; i <= k; i++) {
    res += m * modcomb(n - 1, i) % MOD * modpow(m - 1, n - i - 1) % MOD;
    res %= MOD;
  }
  cout << res << endl;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
/* Isn't passion overrated?*/

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll mod = 998244353;
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll ans = power(a, x / 2) % mod;
  ans = (ans % mod * ans % mod) % mod;
  ans %= mod;
  if (x % 2)
    ans = (ans % mod * a) % mod;
  return ans % mod;
}

vector<ll> per(100010);

ll ncr(ll n, ll i) {
  return (per[n] % mod * power(per[i], mod - 2) % mod *
          power(per[n - i], mod - 2) % mod) %
         mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  per[0] = 1;
  ll h = per.size();
  for (ll i = 1; i < h; i++) {
    per[i] = i * per[i - 1];
    per[i] %= mod;
  }
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll temp = ncr(n - 1, i) % mod;
    temp = (temp * m) % mod;
    temp = (temp * power(m - 1, n - 1 - i) % mod) % mod;
    ans = ans + temp;
    ans %= mod;
  }
  cout << ans % mod;
  return 0;
}


Fixed submission:
/* Isn't passion overrated?*/

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll mod = 998244353;
ll power(ll a, ll x) {
  if (x == 0)
    return 1;
  ll ans = power(a, x / 2) % mod;
  ans = (ans % mod * ans % mod) % mod;
  ans %= mod;
  if (x % 2)
    ans = (ans % mod * a) % mod;
  return ans % mod;
}

vector<ll> per(200010);

ll ncr(ll n, ll i) {
  return (per[n] % mod * power(per[i], mod - 2) % mod *
          power(per[n - i], mod - 2) % mod) %
         mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, m, k;
  cin >> n >> m >> k;
  per[0] = 1;
  ll h = per.size();
  for (ll i = 1; i < h; i++) {
    per[i] = i * per[i - 1];
    per[i] %= mod;
  }
  ll ans = 0;
  for (ll i = 0; i <= k; i++) {
    ll temp = ncr(n - 1, i) % mod;
    temp = (temp * m) % mod;
    temp = (temp * power(m - 1, n - 1 - i) % mod) % mod;
    ans = ans + temp;
    ans %= mod;
  }
  cout << ans % mod;
  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

constexpr long long MOD = 998244353LL;

// MOD考慮power関数
template <typename type> inline type ModPow(type a, type b) {
  type ret = 1;
  for (type i = 0; i < b; i++) {
    ret *= a;
    ret %= MOD;
  }
  return ret;
}

// MOD考慮Combination関数使用準備関数
vector<long long> fac;
vector<long long> finv;
vector<long long> inv;
inline void ModCombiSetup(int size) {
  fac.resize(size + 1);
  finv.resize(size + 1);
  inv.resize(size + 1);
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < size; 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;
  }
}

// MOD考慮Combination関数
inline long long ModCombi(long long n, long long r) {
  if (n == 0 && r == 0)
    return 1;
  if (n < r || n < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

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

  long long ans = 0;

  ModCombiSetup(n);

  for (long long i = 0; i <= k; i++) {
    long long cnt =
        ((m * ModCombi(n - 1, i) % MOD) * ModPow(m - 1, n - 1 - i)) % MOD;
    ans = (ans + cnt) % MOD;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

constexpr long long MOD = 998244353LL;

// MOD考慮power関数
template <typename type> inline type ModPow(type a, type b) {
  type ret = 1;
  while (b > 0) {
    if (b & 0x1)
      ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}

// MOD考慮Combination関数使用準備関数
vector<long long> fac;
vector<long long> finv;
vector<long long> inv;
inline void ModCombiSetup(int size) {
  fac.resize(size + 1);
  finv.resize(size + 1);
  inv.resize(size + 1);
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < size; 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;
  }
}

// MOD考慮Combination関数
inline long long ModCombi(long long n, long long r) {
  if (n == 0 && r == 0)
    return 1;
  if (n < r || n < 0)
    return 0;
  return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}

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

  long long ans = 0;

  ModCombiSetup(n);

  for (long long i = 0; i <= k; i++) {
    long long cnt =
        ((m * ModCombi(n - 1, i) % MOD) * ModPow(m - 1, n - 1 - i)) % MOD;
    ans = (ans + cnt) % MOD;
  }

  cout << ans << endl;
}
Problem p02685 (C++)

Buggy submission (status: 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);
  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";
}

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, 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 (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int mod = 998244353;
const int inf = 2e9 + 5;
const int N = 2e5 + 5;

ll n, m, k, fact[N];

ll modexp(ll x, int n) {
  if (n == 0)
    return 1;
  ll ans = modexp((x * x) % mod, n / 2);
  if (n % 2 != 0)
    return (x * ans) % mod;
  return ans;
}

ll modinv(int x) { return modexp(x, mod - 2); }

ll choose(int i, int j) {
  ll num1 = fact[i];
  ll num2 = (fact[i - j] * fact[j]) % mod;
  num2 = modinv(num2);

  return (num1 * num2) % mod;
}

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

  cin >> n >> m >> k;

  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = (i * fact[i - 1]) % mod;

  if (k == 0) {
    cout << (m * modexp(m - 1, n - 1)) % mod;
    return 0;
  }

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

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

Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int mod = 998244353;
const int inf = 2e9 + 5;
const int N = 2e5 + 5;

ll n, m, k, fact[N];

ll modexp(ll x, int n) {
  if (n == 0)
    return 1;
  ll ans = modexp((x * x) % mod, n / 2);
  if (n % 2 != 0)
    return (x * ans) % mod;
  return ans;
}

ll modinv(int x) { return modexp(x, mod - 2); }

ll choose(int i, int j) {
  ll num1 = fact[i];
  ll num2 = (fact[i - j] * fact[j]) % mod;
  num2 = modinv(num2);

  return (num1 * num2) % mod;
}

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

  cin >> n >> m >> k;

  fact[0] = 1;
  for (int i = 1; i <= N - 5; i++)
    fact[i] = (i * fact[i - 1]) % mod;

  if (k == 0) {
    cout << (m * modexp(m - 1, n - 1)) % mod;
    return 0;
  }

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

  cout << ans << "\n";
  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b) {
    return a.first < b.first;
}
use case
sort(V.begin(), V.end(), compare_function);*/

ll laip(ll a, ll b, ll liek) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a % liek;
  }
  ll c = (a * a) % liek;
  if (b % 2 == 0) {
    return laip(c, b / 2, liek);
  }
  return (a * laip(c, b / 2, liek)) % liek;
}

ll cnk(ll n, ll i, ll liek, vector<ll> &F) {
  ll x = F[n] % liek;
  // cout<<n<<" "<<x<<endl;
  ll y = F[i] * F[n - i];
  y = y % liek;
  // cout<<i<<" "<<n-i<<" "<<x<<" "<<y<<endl;
  ll ats = x * laip(y, liek - 2, liek);
  ats = ats % liek;
  return ats;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  ll liek = 998244353;
  vector<ll> F;
  F.push_back(1);
  for (int i = 0; i < 100005; i++) {
    ll x = (i + 1) * F[i];
    x = x % liek;
    F.push_back(x);
  }
  // cout<<cnk(3,2,liek,F)<<endl;
  ll ats = 0;
  for (int i = 0; i <= k; i++) {
    ll x = cnk(n - 1, i, liek, F) * m;
    // cout<<x<<endl;
    x = x % liek;
    x = x * laip(m - 1, n - i - 1, liek);
    x = x % liek;
    // cout<<i<<" "<<x<<endl;
    ats = ats + x;
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b) {
    return a.first < b.first;
}
use case
sort(V.begin(), V.end(), compare_function);*/

ll laip(ll a, ll b, ll liek) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a % liek;
  }
  ll c = (a * a) % liek;
  if (b % 2 == 0) {
    return laip(c, b / 2, liek);
  }
  return (a * laip(c, b / 2, liek)) % liek;
}

ll cnk(ll n, ll i, ll liek, vector<ll> &F) {
  ll x = F[n] % liek;
  // cout<<n<<" "<<x<<endl;
  ll y = F[i] * F[n - i];
  y = y % liek;
  // cout<<i<<" "<<n-i<<" "<<x<<" "<<y<<endl;
  ll ats = x * laip(y, liek - 2, liek);
  ats = ats % liek;
  return ats;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  ll liek = 998244353;
  vector<ll> F;
  F.push_back(1);
  for (int i = 0; i < 200005; i++) {
    ll x = (i + 1) * F[i];
    x = x % liek;
    F.push_back(x);
  }
  // cout<<cnk(3,2,liek,F)<<endl;
  ll ats = 0;
  for (int i = 0; i <= k; i++) {
    ll x = cnk(n - 1, i, liek, F) * m;
    // cout<<x<<endl;
    x = x % liek;
    x = x * laip(m - 1, n - i - 1, liek);
    x = x % liek;
    // cout<<i<<" "<<x<<endl;
    ats = ats + x;
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}
Problem p02685 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
#define mod 998244353 // 今回のでかい素数
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

// オーバーフローしないようにその都度割っていく！
// 階乗などを配列で持っておくことで計算量を減らす！

ll n, m, k, ans, p;
ll fact[MAX + 10];    // 階乗(factorial)
ll invfact[MAX + 10]; // 階乗の逆元

ll modpow(ll A,
          ll N) { // 二分累乗法により、Nを2の累乗の和に分解してO(logN)で求める
  ll res = 1;
  while (N > 0) {
    if (N & 1)
      res = res * A % mod;
    A = A * A % mod;
    N >>= 1;
  }
  return res;
}

ll combination(ll A, ll B) { // combination(O(1))
  if (A < B)
    return 0;
  if (A < 0 || B < 0)
    return 0;
  return fact[A] * invfact[A - B] % mod * invfact[B] % mod;
}

int main() {
  cin >> n >> m >> k;
  ans = 0;
  fact[0] = 1;                                  // 0!
  FOR(i, 1, n) fact[i] = fact[i - 1] * i % mod; // fact[i] : i!の配列(factorial)

  invfact[n] =
      modpow(fact[n], mod - 2); // フェルマーの小定理よりaの逆元はa^(mod-2)
  FORR(i, n - 1, 0)
  invfact[i] = invfact[i + 1] * (i + 1) % mod; //(i+1)!の逆元にi+1かけてるだけ

  p = modpow(m - 1, n - k - 1);
  FOR(i, n - k, n) { // pow(m-1,n-i-1)の更新をループ内で行ってO(N)に
    ans = (ans + m * p % mod * combination(n - 1, i - 1) % mod) % mod;
    p = p * (m - 1) % mod;
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
#define mod 998244353 // 今回のでかい素数
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

// オーバーフローしないようにその都度割っていく！
// 階乗などを配列で持っておくことで計算量を減らす！

ll n, m, k, ans, p;
ll fact[2 * MAX + 10];    // 階乗(factorial)
ll invfact[2 * MAX + 10]; // 階乗の逆元

ll modpow(ll A,
          ll N) { // 二分累乗法により、Nを2の累乗の和に分解してO(logN)で求める
  ll res = 1;
  while (N > 0) {
    if (N & 1)
      res = res * A % mod;
    A = A * A % mod;
    N >>= 1;
  }
  return res;
}

ll combination(ll A, ll B) { // combination(O(1))
  if (A < B)
    return 0;
  if (A < 0 || B < 0)
    return 0;
  return fact[A] * invfact[A - B] % mod * invfact[B] % mod;
}

int main() {
  cin >> n >> m >> k;
  ans = 0;
  fact[0] = 1;                                  // 0!
  FOR(i, 1, n) fact[i] = fact[i - 1] * i % mod; // fact[i] : i!の配列(factorial)

  invfact[n] =
      modpow(fact[n], mod - 2); // フェルマーの小定理よりaの逆元はa^(mod-2)
  FORR(i, n - 1, 0)
  invfact[i] = invfact[i + 1] * (i + 1) % mod; //(i+1)!の逆元にi+1かけてるだけ

  p = modpow(m - 1, n - k - 1);
  FOR(i, n - k, n) { // pow(m-1,n-i-1)の更新をループ内で行ってO(N)に
    ans = (ans + m * p % mod * combination(n - 1, i - 1) % mod) % mod;
    p = p * (m - 1) % mod;
  }
  cout << ans << "\n";
  return 0;
}
Problem p02685 (C++)

Buggy submission (status: 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(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();
}


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