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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

template <uint MD> struct ModInt {
  using M = ModInt;
  const static M G;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt<998244353>;

Mint dp[310][310][310];
Mint dpsum[310][310][310];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  ll K;
  cin >> K;
  vll vec;
  ll len = S.size();
  ll tmp = 0;
  REP(i, 0, len) {
    if (S[i] == '1') {
      tmp++;
    } else {
      vec.push_back(tmp);
      tmp = 0;
    }
  }
  if (tmp > 0) {
    vec.push_back(tmp);
  }
  ll veclen = vec.size();
  reverse(vec.begin(), vec.end());
  dp[0][0][0] = (Mint)1;
  REP(i, 0, 310) { dpsum[0][i][0] = (Mint)1; }
  ll q = min(K + 1, len - veclen + 1);
  REP(loop, 0, q) {
    REP(i, 0, veclen) {
      REP(j, 0, 300) {
        REP(k, max((ll)0, j - vec[i]), 300) {
          ll p = loop + max(0LL, k - j);
          if (p <= 300) {
            dp[i + 1][j][p] += dp[i][k][loop];
          }
        }
      }
    }
  }
  // REP(i, 0, veclen + 1) {
  //     REP(j, 0, 10) {
  //         cout << dp[i][j] << " ";
  //     }
  //     cout << endl;
  // }
  Mint ans = 0;
  REP(i, 0, q) { ans += dp[veclen][0][i]; }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

template <uint MD> struct ModInt {
  using M = ModInt;
  const static M G;
  uint v;
  ModInt(ll _v = 0) { set_v(uint(_v % MD + MD)); }
  M &set_v(uint _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M &r) const { return M().set_v(v + r.v); }
  M operator-(const M &r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M &r) const { return M().set_v(uint(ull(v) * r.v % MD)); }
  M operator/(const M &r) const { return *this * r.inv(); }
  M &operator+=(const M &r) { return *this = *this + r; }
  M &operator-=(const M &r) { return *this = *this - r; }
  M &operator*=(const M &r) { return *this = *this * r; }
  M &operator/=(const M &r) { return *this = *this / r; }
  bool operator==(const M &r) const { return v == r.v; }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1)
        r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream &operator<<(ostream &os, const M &r) { return os << r.v; }
};
using Mint = ModInt<998244353>;

Mint dp[310][310][310];
Mint dpsum[310][310][310];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  ll K;
  cin >> K;
  vll vec;
  ll len = S.size();
  ll tmp = 0;
  REP(i, 0, len) {
    if (S[i] == '1') {
      tmp++;
    } else {
      vec.push_back(tmp);
      tmp = 0;
    }
  }
  if (tmp > 0) {
    vec.push_back(tmp);
  }
  ll veclen = vec.size();
  reverse(vec.begin(), vec.end());
  dp[0][0][0] = (Mint)1;
  REP(i, 0, 310) { dpsum[0][i][0] = (Mint)1; }
  REP(loop, 0, min(K + 1, len - veclen + 1)) {
    REP(i, 0, veclen) {
      REP(j, 0, 300) {
        REP(k, max((ll)0, j - vec[i]), 300) {
          ll p = loop + max(0LL, k - j);
          if (p <= 300) {
            dp[i + 1][j][p] += dp[i][k][loop];
          }
        }
      }
    }
  }
  // REP(i, 0, veclen + 1) {
  //     REP(j, 0, 10) {
  //         cout << dp[i][j] << " ";
  //     }
  //     cout << endl;
  // }
  Mint ans = 0;
  REP(i, 0, K + 1) { ans += dp[veclen][0][i]; }
  cout << ans << endl;
}