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

#include <bits/stdc++.h>

#define MOD 998244353
// #define INF 100000000000

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "[";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "]";
  return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<vector<T>> &obj) {
  o << "[" << endl;
  for (int i = 0; i < (int)obj.size(); ++i)
    o << "  " << obj[i] << endl;
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "(" << obj.first << ", " << obj.second << ")";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const queue<T> &obj) {
  queue<T> obj_cp = obj;
  bool first = true;
  o << "{";
  while (!obj_cp.empty()) {
    o << (!first ? ", " : "") << obj_cp.front();
    obj_cp.pop();
    first = false;
  }
  o << "}";
  return o;
}

void print() { cout << "\\n"; }
template <class Head, class... Body> void print(Head head, Body... body) {
  cout << head << " ";
  print(body...);
}

ull pow_mod(ull x, ull n) {
  ull ans = 1;
  while (n != 0) {
    if (n & 1) {
      ans = ans * x % MOD;
    }
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

#define N_MAX 200000
vector<ull> fac(N_MAX + 1);
vector<ull> ifac(N_MAX + 1);

void init_fac() {
  fac[0] = 1;
  ifac[0] = 1;
  for (int i = 1; i <= N_MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = ifac[i - 1] * pow_mod(i, MOD - 2) % MOD;
  }
}

ull comb_mod(ull a, ull b) {
  assert(a > 0 && b >= 0 && a >= b);
  ull tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

int main(int argc, char const *argv[]) {
  ull N, M, K;
  cin >> N >> M >> K;

  if (N == 1) {
    cout << M << endl;
    return 0;
  }

  init_fac();

  ull ans = 0;
  for (unsigned int k = 0; k <= K; ++k) {
    ull x = (comb_mod(N - 1, k) * pow_mod(M - 1, N - 1 - k)) % MOD;
    ans = (ans + x) % MOD;
  }
  ans = (ans * M) % MOD;
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define MOD 998244353
// #define INF 100000000000

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

template <class T> ostream &operator<<(ostream &o, const vector<T> &obj) {
  o << "[";
  for (int i = 0; i < (int)obj.size(); ++i)
    o << (i > 0 ? ", " : "") << obj[i];
  o << "]";
  return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<vector<T>> &obj) {
  o << "[" << endl;
  for (int i = 0; i < (int)obj.size(); ++i)
    o << "  " << obj[i] << endl;
  o << "]";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &obj) {
  o << "(" << obj.first << ", " << obj.second << ")";
  return o;
}
template <class T, class U>
ostream &operator<<(ostream &o, const map<T, U> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const set<T> &obj) {
  o << "{";
  for (auto itr = obj.begin(); itr != obj.end(); ++itr)
    o << (itr != obj.begin() ? ", " : "") << *itr;
  o << "}";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const queue<T> &obj) {
  queue<T> obj_cp = obj;
  bool first = true;
  o << "{";
  while (!obj_cp.empty()) {
    o << (!first ? ", " : "") << obj_cp.front();
    obj_cp.pop();
    first = false;
  }
  o << "}";
  return o;
}

void print() { cout << "\\n"; }
template <class Head, class... Body> void print(Head head, Body... body) {
  cout << head << " ";
  print(body...);
}

ull pow_mod(ull x, ull n) {
  ull ans = 1;
  while (n != 0) {
    if (n & 1) {
      ans = ans * x % MOD;
    }
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

#define N_MAX 200000
vector<ull> fac(N_MAX + 1);
vector<ull> ifac(N_MAX + 1);

void init_fac() {
  fac[0] = 1;
  ifac[0] = 1;
  for (int i = 1; i <= N_MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    ifac[i] = ifac[i - 1] * pow_mod(i, MOD - 2) % MOD;
  }
}

ull comb_mod(ull a, ull b) {
  assert(a > 0 && b >= 0 && a >= b);
  ull tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

int main(int argc, char const *argv[]) {
  ull N, M, K;
  cin >> N >> M >> K;

  init_fac();

  ull ans = 0;
  for (unsigned int k = 0; k <= K; ++k) {
    ull x = (comb_mod(N - 1, k) * pow_mod(M - 1, N - 1 - k)) % MOD;
    ans = (ans + x) % MOD;
  }
  ans = (ans * M) % MOD;
  cout << ans << endl;

  return 0;
}