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

#include <bits/stdc++.h>
#define ll long long
#define ar array
#define nl '\\n'
#define AC                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
using namespace std;
const int MOD = 998244353;
const int N = 2 * 1e5;

struct mi {
  ll v;
  explicit operator ll() const { return v; }
  mi() { v = 0; }
  mi(ll _v) {
    v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
    if (v < 0)
      v += MOD;
  }
  friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }

  mi &operator+=(const mi &m) {
    if ((v += m.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((v -= m.v) < 0)
      v += MOD;
    return *this;
  }
  mi &operator*=(const mi &m) {
    v = v * m.v % MOD;
    return *this;
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }
  friend mi pow(mi a, ll p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a)
      if (p & 1)
        ans *= a;
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
  }

  mi operator-() const { return mi(-v); }
  mi &operator++() { return *this += 1; }
  mi &operator--() { return *this -= 1; }
  mi operator++(int) {
    mi temp;
    temp.v = v++;
    return temp;
  }
  mi operator--(int) {
    mi temp;
    temp.v = v--;
    return temp;
  }
  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }
  friend ostream &operator<<(ostream &os, const mi &m) {
    os << m.v;
    return os;
  }
  friend istream &operator>>(istream &is, mi &m) {
    ll x;
    is >> x;
    m.v = x;
    return is;
  }
};

int MX = 2 * 1e5;
ll n, k, mm;
mi fac[N + 5];
mi facInv[N + 5];

// initialize factorials
void initFac() {
  fac[0] = 1;
  facInv[0] = 1;
  for (int i = 1; i <= MX; i++) {
    fac[i] = fac[i - 1] * i;
    facInv[i] = inv(fac[i]);
  }
}

// C(n, k)
mi choose(int n, int k) {
  if (k < 0)
    return 0;
  if (n < 0)
    return 0;
  if (k > n)
    return 0;

  mi res = fac[n] * facInv[n - k] * facInv[k];
  return res;
}

int main() {
  AC initFac();
  cin >> n >> mm >> k;
  mi m = mm;
  mi ans = 0;
  for (int i = 0; i <= k; i++) {
    ans += choose(n - 1, i) * m * pow(m - 1, n - 1 - i);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ar array
#define nl '\\n'
#define AC                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
using namespace std;
const int MOD = 998244353;
const int N = 2 * 1e5;
const ll MX = 2 * 1e5 + 5;

struct mi {
  ll v;
  explicit operator ll() const { return v; }
  mi() { v = 0; }
  mi(ll _v) {
    v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
    if (v < 0)
      v += MOD;
  }
  friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
  friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
  friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }

  mi &operator+=(const mi &m) {
    if ((v += m.v) >= MOD)
      v -= MOD;
    return *this;
  }
  mi &operator-=(const mi &m) {
    if ((v -= m.v) < 0)
      v += MOD;
    return *this;
  }
  mi &operator*=(const mi &m) {
    v = v * m.v % MOD;
    return *this;
  }
  mi &operator/=(const mi &m) { return (*this) *= inv(m); }
  friend mi pow(mi a, ll p) {
    mi ans = 1;
    assert(p >= 0);
    for (; p; p /= 2, a *= a)
      if (p & 1)
        ans *= a;
    return ans;
  }
  friend mi inv(const mi &a) {
    assert(a.v != 0);
    return pow(a, MOD - 2);
  }

  mi operator-() const { return mi(-v); }
  mi &operator++() { return *this += 1; }
  mi &operator--() { return *this -= 1; }
  mi operator++(int) {
    mi temp;
    temp.v = v++;
    return temp;
  }
  mi operator--(int) {
    mi temp;
    temp.v = v--;
    return temp;
  }
  friend mi operator+(mi a, const mi &b) { return a += b; }
  friend mi operator-(mi a, const mi &b) { return a -= b; }
  friend mi operator*(mi a, const mi &b) { return a *= b; }
  friend mi operator/(mi a, const mi &b) { return a /= b; }
  friend ostream &operator<<(ostream &os, const mi &m) {
    os << m.v;
    return os;
  }
  friend istream &operator>>(istream &is, mi &m) {
    ll x;
    is >> x;
    m.v = x;
    return is;
  }
};

mi fac[N + 5];
mi facInv[N + 5];

// initialize factorials
void initFac() {
  fac[0] = 1;
  facInv[0] = 1;
  for (ll i = 1; i <= MX; i++) {
    fac[i] = fac[i - 1] * i;
    facInv[i] = inv(fac[i]);
  }
}

// C(n, k)
mi choose(int n, int k) {
  if (k < 0)
    return 0;
  if (n < 0)
    return 0;
  if (k > n)
    return 0;

  mi res = fac[n] * facInv[n - k] * facInv[k];
  return res;
}

int main() {
  AC ll n, k, mm;
  initFac();
  cin >> n >> mm >> k;
  mi m = mm;
  mi ans = 0;
  for (int i = 0; i <= k; i++) {
    ans += choose(n - 1, i) * m * pow(m - 1, n - 1 - i);
  }
  cout << ans << endl;
}