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

////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;

#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#endif

// -- typedefs -- //
#define EPS 1e-9
typedef long long llong;

// -- loop macros -- //
#define LOOP_TYPE(s, n)                                                        \\
  std::remove_reference_t<std::remove_cv_t<decltype((n) - (s))>>
#define FOR(i, s, n)                                                           \\
  for (auto i = LOOP_TYPE(s, n)(s); i < LOOP_TYPE(s, n)(n); i++)
#define FORR(i, s, n)                                                          \\
  for (auto i = LOOP_TYPE(s, n)(n) - 1; i != LOOP_TYPE(s, n)(s) - 1; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FORR(i, 0, n)

#define FORE(exp) for (auto &&exp)
#define allof(c) begin(c), end(c)
#define partof(c, s, e) begin(c) + (s), begin(c) + (e)

// -- functors -- //
#define PREDICATE(t, a, ...) [&](const t &a) -> bool { return __VA_ARGS__; }
#define PRED(a, ...) PREDICATE(auto, a, __VA_ARGS__)
#define COMPARISON(t, a, b, ...)                                               \\
  [&](const t &a, const t &b) -> bool { return __VA_ARGS__; }
#define COMP(a, b, ...) COMPARISON(auto, a, b, __VA_ARGS__)
#define CONV1(a, ...)                                                          \\
  [&](const auto &a) -> auto{ return __VA_ARGS__; }
#define CONV2(a, b, ...)                                                       \\
  [&](const auto &a, const auto &b) -> auto{ return __VA_ARGS__; }
#define CONV3(a, b, c, ...)                                                    \\
  [&](const auto &a, const auto &b, const auto &c) -> auto{                    \\
    return __VA_ARGS__;                                                        \\
  }

#define TIE(...)                                                               \\
  auto tie() const { return std::tie(__VA_ARGS__); }
#define LESS(t)                                                                \\
  bool operator<(const t &rhs) const { return tie() < rhs.tie(); }
#define GREATER(t)                                                             \\
  bool operator>(const t &rhs) const { return tie() > rhs.tie(); }

// -- I/O Helper -- //
struct _Reader {
  istream &cin;
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
struct _Writer {
  ostream &cout;
  const char *d{" "};
  bool f{};
  template <class T> _Writer operator,(const T &rhs) {
    cout << (f ? d : "") << rhs;
    f = 1;
    return *this;
  }
};
#define READ(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  (_Reader{cin}), __VA_ARGS__
#define WRITE(...)                                                             \\
  do {                                                                         \\
    (_Writer{cout}), __VA_ARGS__;                                              \\
    cout << "\\n";                                                              \\
  } while (false)
#ifdef _DEBUG
#define DEBUG(...)                                                             \\
  do {                                                                         \\
    (_Writer{cerr}), "DEBUG -> ", __VA_ARGS__;                                 \\
    cerr << "\\n";                                                              \\
  } while (false)
#else
#define DEBUG(...)
#endif

// -- vevector -- //
template <class T> struct vevector : vector<vector<T>> {
  vevector(size_t n = 0, size_t m = 0, const T &initial = T())
      : vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : vector<vevector<T>> {
  vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
      : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : vector<vevevector<T>> {
  vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
               const T &initial = T())
      : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};

namespace std {
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << p.first << " " << p.second;
  return out;
}
} // namespace std

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
  vevector<T> v;
  REP(i, n) v.push_back(readjag<T>());
  return v;
}

template <class T> struct iter_range_t {
  T beg, end;
};
template <class T> iter_range_t<T> iter_range(T beg, T end) {
  return iter_range_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_range_t<T> v) {
  if (v.beg != v.end) {
    out << *v.beg++;
    while (v.beg != v.end) {
      out << " " << *v.beg++;
    }
  }
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  return out << iter_range(allof(v));
}

// -- etc -- //
template <class T> T infinity_value();
template <> int infinity_value<int>() { return int(1) << 30; }
template <> llong infinity_value<llong>() { return llong(1) << 60; }
template <> double infinity_value<double>() { return 1e+300 * 1e+300; }
#define INF(T) infinity_value<T>()

inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
  return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
  return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
  return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
  vector<int> r(count);
  iota(allof(r), start);
  return r;
}

//// start up ////
void solve();
int32_t main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(std::numeric_limits<double>::max_digits10);

  solve();

  // for Google code jam
  // READ(int, testcases);
  // REP(i, testcases)
  //{
  //	cout << "Case #" << (i + 1) << ": ";
  //	solve();
  //}

  // for Google code jam interactive
  // #ifdef _MSC_VER
  //	_setmode(_fileno(stdout), _O_BINARY);
  // #endif
  //
  //	READ(int, T, B);
  //	DEBUG("T,B = ", T, B);
  //	REP(i, T) solve(B);

  return 0;
}

// Mod な n! nCr nPr nHr を求める。
// 初期化に O(n) sizeof(llong) * n * 3 のメモリを要する 1000万(10^7)で256MBギリ
// 初期化してしまえば、あとは O(1)
struct ModCombination {
  // modFact[n] = MODを法とした n!
  vector<llong> modFact{};
  void initModFact(int N, llong MOD) {
    modFact.clear();
    modFact.reserve(N + 1);
    llong r = 1;
    modFact.push_back(r);
    FOR(i, 1, N + 1) {
      r = r * i % MOD;
      modFact.push_back(r);
    }
  }

  // modInv[n] = MODを法とした n の逆元
  vector<llong> modInv{};
  void initModInv(int N, llong MOD) {
    modInv.clear();
    modInv.reserve(N + 1);
    modInv.push_back(0);
    modInv.push_back(1);
    FOR(i, 2, N + 1) {
      modInv.push_back(modInv[MOD % i] * (MOD - MOD / i) % MOD);
    }
  }

  // modFactInv[n] = MODを法とした n! の逆元
  vector<llong> modFactInv{};
  void initModFactInv(int N, llong MOD) {
    modFactInv.clear();
    modFactInv.reserve(N + 1);
    llong r = 1;
    modFactInv.push_back(r);
    FOR(i, 1, N + 1) {
      r = r * modInv[i] % MOD;
      modFactInv.push_back(r);
    }
  }

  int N;
  llong MOD;
  ModCombination(int N, llong MOD) : N(N), MOD(MOD) {
    initModFact(N + 1, MOD);
    initModInv(N + 1, MOD);
    initModFactInv(N + 1, MOD);
  }

  llong Fact(int n) { return modFact[n]; }
  llong Permutation(int n, int k) {
    return k >= 0 && n >= k ? modFact[n] * modFactInv[n - k] % MOD : 0;
  }
  llong Combination(int n, int k) {
    return k >= 0 && n >= k ? Permutation(n, k) * modFactInv[k] % MOD : 0;
  }
  llong HomogeneousProduct(int n, int k) {
    return n == 0 && k == 0 ? 1 : Combination(n + k - 1, k);
  }
};

// modを法としたxのy乗を求める。(繰り返し二乗法)
inline llong modPow(llong x, llong y, llong mod) {
  llong r = 1;
  for (; y > 0; y >>= 1) {
    if (y & 1) {
      r = r * x % mod;
    }
    x = x * x % mod;
  }
  return r;
}

////////////////////
//// template end
////////////////////

void solve() {
  READ(llong, N, M, K);
  if (N == 1) {
    WRITE(M);
    return;
  }

  if (M == 1) {
    WRITE(K >= N - 1 ? 1 : 0);
    return;
  }

  llong mod = 998244353;
  ModCombination MOD(200000, mod);

  llong ans = 0;
  REP(i, K + 1) {
    llong ad = M;
    ad *= modPow(M - 1, N - i - 1, mod);
    ad %= mod;
    ad *= MOD.Combination(N - 1, i);
    ad %= mod;
    ans += ad;
    ans %= mod;
  }
  WRITE(ans);
}


Error: 0

Fixed submission:

////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include "bits/stdc++.h"
using namespace std;

#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#endif

// -- typedefs -- //
#define EPS 1e-9
typedef long long llong;

// -- loop macros -- //
#define LOOP_TYPE(s, n)                                                        \\
  std::remove_reference_t<std::remove_cv_t<decltype((n) - (s))>>
#define FOR(i, s, n)                                                           \\
  for (auto i = LOOP_TYPE(s, n)(s); i < LOOP_TYPE(s, n)(n); i++)
#define FORR(i, s, n)                                                          \\
  for (auto i = LOOP_TYPE(s, n)(n) - 1; i != LOOP_TYPE(s, n)(s) - 1; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) FORR(i, 0, n)

#define FORE(exp) for (auto &&exp)
#define allof(c) begin(c), end(c)
#define partof(c, s, e) begin(c) + (s), begin(c) + (e)

// -- functors -- //
#define PREDICATE(t, a, ...) [&](const t &a) -> bool { return __VA_ARGS__; }
#define PRED(a, ...) PREDICATE(auto, a, __VA_ARGS__)
#define COMPARISON(t, a, b, ...)                                               \\
  [&](const t &a, const t &b) -> bool { return __VA_ARGS__; }
#define COMP(a, b, ...) COMPARISON(auto, a, b, __VA_ARGS__)
#define CONV1(a, ...)                                                          \\
  [&](const auto &a) -> auto{ return __VA_ARGS__; }
#define CONV2(a, b, ...)                                                       \\
  [&](const auto &a, const auto &b) -> auto{ return __VA_ARGS__; }
#define CONV3(a, b, c, ...)                                                    \\
  [&](const auto &a, const auto &b, const auto &c) -> auto{                    \\
    return __VA_ARGS__;                                                        \\
  }

#define TIE(...)                                                               \\
  auto tie() const { return std::tie(__VA_ARGS__); }
#define LESS(t)                                                                \\
  bool operator<(const t &rhs) const { return tie() < rhs.tie(); }
#define GREATER(t)                                                             \\
  bool operator>(const t &rhs) const { return tie() > rhs.tie(); }

// -- I/O Helper -- //
struct _Reader {
  istream &cin;
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
struct _Writer {
  ostream &cout;
  const char *d{" "};
  bool f{};
  template <class T> _Writer operator,(const T &rhs) {
    cout << (f ? d : "") << rhs;
    f = 1;
    return *this;
  }
};
#define READ(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  (_Reader{cin}), __VA_ARGS__
#define WRITE(...)                                                             \\
  do {                                                                         \\
    (_Writer{cout}), __VA_ARGS__;                                              \\
    cout << "\\n";                                                              \\
  } while (false)
#ifdef _DEBUG
#define DEBUG(...)                                                             \\
  do {                                                                         \\
    (_Writer{cerr}), "DEBUG -> ", __VA_ARGS__;                                 \\
    cerr << "\\n";                                                              \\
  } while (false)
#else
#define DEBUG(...)
#endif

// -- vevector -- //
template <class T> struct vevector : vector<vector<T>> {
  vevector(size_t n = 0, size_t m = 0, const T &initial = T())
      : vector<vector<T>>(n, vector<T>(m, initial)) {}
};
template <class T> struct vevevector : vector<vevector<T>> {
  vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T())
      : vector<vevector<T>>(n, vevector<T>(m, l, initial)) {}
};
template <class T> struct vevevevector : vector<vevevector<T>> {
  vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0,
               const T &initial = T())
      : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) {}
};

namespace std {
template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << p.first << " " << p.second;
  return out;
}
} // namespace std

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) {
  vevector<T> v;
  REP(i, n) v.push_back(readjag<T>());
  return v;
}

template <class T> struct iter_range_t {
  T beg, end;
};
template <class T> iter_range_t<T> iter_range(T beg, T end) {
  return iter_range_t<T>{beg, end};
}
template <class T> ostream &operator<<(ostream &out, iter_range_t<T> v) {
  if (v.beg != v.end) {
    out << *v.beg++;
    while (v.beg != v.end) {
      out << " " << *v.beg++;
    }
  }
  return out;
}
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  return out << iter_range(allof(v));
}

// -- etc -- //
template <class T> T infinity_value();
template <> int infinity_value<int>() { return int(1) << 30; }
template <> llong infinity_value<llong>() { return llong(1) << 60; }
template <> double infinity_value<double>() { return 1e+300 * 1e+300; }
#define INF(T) infinity_value<T>()

inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
template <class TInt> bool in_range(TInt val, TInt min, TInt max) {
  return val >= min && val < max;
}
template <> bool in_range<double>(double val, double min, double max) {
  return val - min > -EPS && val - max < EPS;
}
template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) {
  return x >= 0 && x < w && y >= 0 && y < h;
}
vector<int> iotavn(int start, int count) {
  vector<int> r(count);
  iota(allof(r), start);
  return r;
}

//// start up ////
void solve();
int32_t main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(std::numeric_limits<double>::max_digits10);

  solve();

  // for Google code jam
  // READ(int, testcases);
  // REP(i, testcases)
  //{
  //	cout << "Case #" << (i + 1) << ": ";
  //	solve();
  //}

  // for Google code jam interactive
  // #ifdef _MSC_VER
  //	_setmode(_fileno(stdout), _O_BINARY);
  // #endif
  //
  //	READ(int, T, B);
  //	DEBUG("T,B = ", T, B);
  //	REP(i, T) solve(B);

  return 0;
}

// Mod な n! nCr nPr nHr を求める。
// 初期化に O(n) sizeof(llong) * n * 3 のメモリを要する 1000万(10^7)で256MBギリ
// 初期化してしまえば、あとは O(1)
struct ModCombination {
  // modFact[n] = MODを法とした n!
  vector<llong> modFact{};
  void initModFact(int N, llong MOD) {
    modFact.clear();
    modFact.reserve(N + 1);
    llong r = 1;
    modFact.push_back(r);
    FOR(i, 1, N + 1) {
      r = r * i % MOD;
      modFact.push_back(r);
    }
  }

  // modInv[n] = MODを法とした n の逆元
  vector<llong> modInv{};
  void initModInv(int N, llong MOD) {
    modInv.clear();
    modInv.reserve(N + 1);
    modInv.push_back(0);
    modInv.push_back(1);
    FOR(i, 2, N + 1) {
      modInv.push_back(modInv[MOD % i] * (MOD - MOD / i) % MOD);
    }
  }

  // modFactInv[n] = MODを法とした n! の逆元
  vector<llong> modFactInv{};
  void initModFactInv(int N, llong MOD) {
    modFactInv.clear();
    modFactInv.reserve(N + 1);
    llong r = 1;
    modFactInv.push_back(r);
    FOR(i, 1, N + 1) {
      r = r * modInv[i] % MOD;
      modFactInv.push_back(r);
    }
  }

  int N;
  llong MOD;
  ModCombination(int N, llong MOD) : N(N), MOD(MOD) {
    initModFact(N + 1, MOD);
    initModInv(N + 1, MOD);
    initModFactInv(N + 1, MOD);
  }

  llong Fact(int n) { return modFact[n]; }
  llong Permutation(int n, int k) {
    return k >= 0 && n >= k ? modFact[n] * modFactInv[n - k] % MOD : 0;
  }
  llong Combination(int n, int k) {
    return k >= 0 && n >= k ? Permutation(n, k) * modFactInv[k] % MOD : 0;
  }
  llong HomogeneousProduct(int n, int k) {
    return n == 0 && k == 0 ? 1 : Combination(n + k - 1, k);
  }
};

// modを法としたxのy乗を求める。(繰り返し二乗法)
inline llong modPow(llong x, llong y, llong mod) {
  llong r = 1;
  for (; y > 0; y >>= 1) {
    if (y & 1) {
      r = r * x % mod;
    }
    x = x * x % mod;
  }
  return r;
}

////////////////////
//// template end
////////////////////

void solve() {
  READ(llong, N, M, K);
  if (N == 1) {
    WRITE(M);
    return;
  }

  if (M == 1) {
    WRITE(K < N - 1 ? 1 : 0);
    return;
  }

  llong mod = 998244353;
  ModCombination MOD(100000, mod);

  llong ans = 0;
  REP(i, K + 1) {
    // DEBUG("i", i, M * modPow(M - 1, N - i - 1, mod), MOD.Combination(N - 1,
    // i));
    ans += M * modPow(M - 1, N - i - 1, mod) % mod * MOD.Combination(N - 1, i) %
           mod;
    ans %= mod;
  }
  WRITE(ans);
}