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

// snuke syakyou
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// Write From this Line
//  auto mod int
//  https://youtu.be/L8grWxBlIZ4?t=9858
//  https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
//  https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} c(200005);
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  mint ans = 0;
  mint col = m;
  for (int x = n - 1; x >= 0; --x) { // 同色がx組
    mint now = col; // x個のグループをm色で塗る時が何通りあるか
    now *= c(n - 1, x); // 組の作り方が何通りあるかをnowにかける。
    // now を計算する
    if (x <= k)
      ans += now;
    col *= m - 1;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define SORT(a) sort((a).begin(), (a).end())
#define rSORT(a) reverse((a).begin(), (a).end())
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// Write From this Line

const ll mod = 998244353;

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  // n個のブロック,作れるのは最大で n / 2　セット
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1));

  rep(j, n + 1) dp[0][j] = 0, dp[1][j] = 0;
  dp[1][0] = m;

  // 　0セットをまず作ろう
  for (int i = 2; i <= n; i++) {
    dp[i][0] = (dp[i - 1][0] * (m - 1)) % mod;
  }

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i - 2][j - 1] * 2 + dp[i - 1][j] * 3;
      dp[i][j] %= mod;
    }
  }

  ll ans = 0;
  for (int j = 0; j <= k; j++) {
    ans += (dp[n][j]) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}