Problem p02685 (C++) — buggy submission, verdict: Runtime Error

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

template <class T> struct rge {
  T b, e;
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  template <class T> debug &operator<<(T x) {
    cerr << boolalpha << x;
    return *this;
  }
  template <class B, class C> debug &operator<<(pair<B, C> x) {
    return *this << "(" << x.first << ", " << x.second << ")";
  }
  template <class T> debug &operator<<(rge<T> x) {
    *this << "[";
    for (auto it = x.b; it != x.e; it++) {
      *this << ", " + 2 * (it == x.b) << *it;
    }
    return *this << "]";
  }
  template <class T> debug &operator<<(vector<T> x) {
    return *this << range(x.begin(), x.end());
  }
#else
  template <class T> debug &operator<<(const T &) { return *this; }
#endif
};
#define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
using ll = long long;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)

template <typename T> void min_self(T &a, T b) { a = min(a, b); }
template <typename T> void max_self(T &a, T b) { a = max(a, b); }

const int mod = 998244353;
const int mxN = 200 * 1000 + 5;
int fact[mxN], finv[mxN];

int add(int a, int b) {
  a += b;
  while (a >= mod) {
    a -= mod;
  }
  return a;
}

void add_self(int &a, int b) { a = add(a, b); }

int mul(int a, int b) { return 1LL * a * b % mod; }

int mpow(int a, int n) {
  if (n == 0) {
    return 1;
  }
  int x = mpow(a, n / 2);
  x = mul(x, x);
  if (n & 1) {
    x = mul(x, a);
  }
  return x;
}

int binomial(int n, int k) { return mul(fact[n], mul(finv[n - k], finv[k])); }

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

  int n, m, k;
  cin >> n >> m >> k;
  fact[0] = finv[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = mul(i, fact[i - 1]);
    finv[i] = mpow(fact[i], mod - 2);
  }
  int ans = 0;
  forn(i, k + 1) {
    add_self(ans, mul(mul(m, binomial(n - 1, i)), mpow(m - 1, n - i - 1)));
  }
  cout << ans << endl;
}

// Those who say they can, and those who say they can't - are both usually
// right.

// Read statement carefully
// Solve on paper first!
// Overflows (long long)

// Author: blondie

Error: 0

Fixed submission:

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

template <class T> struct rge {
  T b, e;
};
template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  template <class T> debug &operator<<(T x) {
    cerr << boolalpha << x;
    return *this;
  }
  template <class B, class C> debug &operator<<(pair<B, C> x) {
    return *this << "(" << x.first << ", " << x.second << ")";
  }
  template <class T> debug &operator<<(rge<T> x) {
    *this << "[";
    for (auto it = x.b; it != x.e; it++) {
      *this << ", " + 2 * (it == x.b) << *it;
    }
    return *this << "]";
  }
  template <class T> debug &operator<<(vector<T> x) {
    return *this << range(x.begin(), x.end());
  }
#else
  template <class T> debug &operator<<(const T &) { return *this; }
#endif
};
#define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] "
using ll = long long;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)

template <typename T> void min_self(T &a, T b) { a = min(a, b); }
template <typename T> void max_self(T &a, T b) { a = max(a, b); }

const int mod = 998244353;
const int mxN = 200 * 1000 + 5;
int fact[mxN], finv[mxN];

int add(int a, int b) {
  a += b;
  while (a >= mod) {
    a -= mod;
  }
  return a;
}

int add_self(int &a, int b) { a = add(a, b); }

int mul(int a, int b) { return 1LL * a * b % mod; }

int mpow(int a, int n) {
  if (n == 0) {
    return 1;
  }
  int x = mpow(a, n / 2);
  x = mul(x, x);
  if (n & 1) {
    x = mul(x, a);
  }
  return x;
}

int binomial(int n, int k) { return mul(fact[n], mul(finv[n - k], finv[k])); }

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

  int n, m, k;
  cin >> n >> m >> k;
  fact[0] = finv[0] = 1;
  for (int i = 1; i < n; i++) {
    fact[i] = mul(i, fact[i - 1]);
    finv[i] = mpow(fact[i], mod - 2);
  }
  int ans = 0;
  forn(i, k + 1) {
    add_self(ans, mul(mul(m, binomial(n - 1, i)), mpow(m - 1, n - i - 1)));
  }
  cout << ans << endl;
}

// Those who say they can, and those who say they can't - are both usually
// right.

// Read statement carefully
// Solve on paper first!
// Overflows (long long)

// Author: blondie