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

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> inline bool kbit(const T &x, const int &k) {
  return ((x >> k) & 1LL);
}
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T> void zip(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class T> int lwb(V<T> &v, const T &x) {
  return lower_bound(all(v), x) - v.begin();
}
void dump() { cerr << '\\n'; }
template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) {
  cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
  dump(std::move(tail)...);
}
template <class T> void print(const vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << (i + 1 == v.size() ? '\\n' : ' ');
}
template <class T> void read(vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
}
constexpr char sp = ' ', newl = '\\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 998244353LL;
//////////////////////////////////////////////INSERT ABOVE HERE
// from http://noshi91.hatenablog.com/entry/2019/03/31/174006
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint operator^(const u64 rhs) const noexcept {
    return modint(*this) ^= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
  constexpr modint &operator^=(u64 exp) {
    modint rhs = modint(*this);
    a = 1;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
  friend ostream &operator<<(ostream &os, const modint &x) {
    os << x.a;
    return os;
  }
  friend istream &operator>>(istream &is, modint &x) {
    is >> x.a;
    return is;
  }
};
using mint = modint<MOD>;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = s.length();
  V<int> a(n + 1);
  int co = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      co++;
    } else {
      a[co]++;
    }
  }
  V<V<V<mint>>> dp(n + 1, V<V<mint>>(n + 1, V<mint>(n + 1)));
  dp[n][0][0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        for (int l = 1; l <= a[i]; l++) {
          if (j + l > n || k + l > n)
            break;
          dp[i][j + l][k + l] += dp[i + 1][j][k];
        }
      }
      //    cout << i << " " << j << " ";
      // print(dp[i][j]);
    }
    for (int k = 0; k <= n; k++) {
      mint sum = 0;
      for (int j = n; j >= 0; j--) {
        sum += dp[i + 1][j][k];
        dp[i][j][k] += sum;
      }
    }
  }
  mint res = 0;
  for (int i = 0; i <= min(n, K); i++) {
    res += dp[0][0][i];
  }
  cout << res << newl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
template <class T> inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T> inline bool kbit(const T &x, const int &k) {
  return ((x >> k) & 1LL);
}
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T> void zip(V<T> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
template <class T> int lwb(V<T> &v, const T &x) {
  return lower_bound(all(v), x) - v.begin();
}
void dump() { cerr << '\\n'; }
template <class Head, class... Tail> void dump(Head &&head, Tail &&...tail) {
  cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
  dump(std::move(tail)...);
}
template <class T> void print(const vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cout << v[i] << (i + 1 == v.size() ? '\\n' : ' ');
}
template <class T> void read(vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    cin >> v[i];
}
constexpr char sp = ' ', newl = '\\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 998244353LL;
//////////////////////////////////////////////INSERT ABOVE HERE
// from http://noshi91.hatenablog.com/entry/2019/03/31/174006
template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;
  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint operator^(const u64 rhs) const noexcept {
    return modint(*this) ^= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
  constexpr modint &operator^=(u64 exp) {
    modint rhs = modint(*this);
    a = 1;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
  friend ostream &operator<<(ostream &os, const modint &x) {
    os << x.a;
    return os;
  }
  friend istream &operator>>(istream &is, modint &x) {
    is >> x.a;
    return is;
  }
};
using mint = modint<MOD>;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  cin >> s;
  int K;
  cin >> K;
  int n = s.length();
  V<int> a(n + 1);
  int co = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      co++;
    } else {
      a[co]++;
    }
  }
  V<V<V<mint>>> dp(n + 1, V<V<mint>>(n + 1, V<mint>(n + 1)));
  dp[n][0][0] = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= n; k++) {
        for (int l = 1; l <= a[i]; l++) {
          if (j + l > n || k + l > n)
            break;
          dp[i][j + l][k + l] += dp[i + 1][j][k];
        }
      }
      //    cout << i << " " << j << " ";
      // print(dp[i][j]);
    }
    for (int k = 0; k <= n; k++) {
      mint sum = 0;
      for (int j = n; j >= 0; j--) {
        sum += dp[i + 1][j][k];
        dp[i][j][k] += sum;
      }
    }
  }
  mint res = 0;
  for (int i = 0; i <= K; i++) {
    res += dp[0][0][i];
  }
  cout << res << newl;
}