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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 998244353
#define pl(a, b) a = (a + b) % mod;
#define N 350
ll dp[N][N][N];
int main() {
  string S;
  ll K;
  cin >> S >> K;
  ll n = 0, a[N];
  ll cnt = 0;
  rep(i, S.size()) {
    if (S[i] == '0') {
      a[++n] = cnt;
      cnt = 0;
    } else {
      cnt++;
    }
  }
  a[++n] = cnt;
  chmin(K, (ll)310);
  rep(i, N) rep(j, N) rep(k, N) dp[i][j][k] = 0;
  dp[0][0][0] = 1;
  for (ll i = 1; i <= n; i++) {
    ll cs1[N][N], cs2[N][N];
    rep(j, N) for (ll k = 0; k <= K; k++) {
      if (j - 1 >= 0 && k - 1 >= 0) {
        cs1[j][k] = cs1[j - 1][k - 1];
      } else {
        cs1[j][k] = 0;
      }
      pl(cs1[j][k], dp[i - 1][j][k]);
      pl(dp[i][j][k], cs1[j][k]);
      /*for(ll s=0;j-s>=0&&k-s>=0;s++){
              pl(dp[i][j][k],dp[i-1][j-s][k-s]);
      }*/
    }
    for (ll j = N - 1; j >= 0; j--)
      for (ll k = 0; k <= K; k++) {
        if (j + 2 < N) {
          cs2[j][k] = cs2[j + 1][k];
        } else {
          cs2[j][k] = 0;
        }
        pl(cs2[j][k], dp[i - 1][j][k]);
        pl(dp[i][j][k], cs2[j + 1][k]);
        if (j + a[i] + 1 < N) {
          pl(dp[i][j][k], -cs2[j + a[i] + 1][k]);
        }
        /*for(ll t=1;t<=a[i]&&j+t<N;t++){
                pl(dp[i][j][k],dp[i-1][j+t][k]);
        }*/
      }
  }
  ll ans = 0;
  for (ll k = 0; k <= K; k++) {
    pl(ans, dp[n][0][k]);
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define mod 998244353
#define pl(a, b) a = (a + b) % mod;
#define N 310
ll dp[N][N][N];
int main() {
  string S;
  ll K;
  cin >> S >> K;
  ll n = 0, a[N];
  ll cnt = 0;
  rep(i, S.size()) {
    if (S[i] == '0') {
      a[++n] = cnt;
      cnt = 0;
    } else {
      cnt++;
    }
  }
  a[++n] = cnt;
  chmin(K, (ll)300);
  rep(i, N) rep(j, N) rep(k, N) dp[i][j][k] = 0;
  dp[0][0][0] = 1;
  for (ll i = 1; i <= n; i++) {
    ll cs1[N][N], cs2[N][N];
    rep(j, N) for (ll k = 0; k <= K; k++) {
      if (j - 1 >= 0 && k - 1 >= 0) {
        cs1[j][k] = cs1[j - 1][k - 1];
      } else {
        cs1[j][k] = 0;
      }
      pl(cs1[j][k], dp[i - 1][j][k]);
      pl(dp[i][j][k], cs1[j][k]);
      /*for(ll s=0;j-s>=0&&k-s>=0;s++){
              pl(dp[i][j][k],dp[i-1][j-s][k-s]);
      }*/
    }
    for (ll j = N - 1; j >= 0; j--)
      for (ll k = 0; k <= K; k++) {
        if (j + 2 < N) {
          cs2[j][k] = cs2[j + 1][k];
        } else {
          cs2[j][k] = 0;
        }
        pl(cs2[j][k], dp[i - 1][j][k]);
        pl(dp[i][j][k], cs2[j + 1][k]);
        if (j + a[i] + 1 < N) {
          pl(dp[i][j][k], -cs2[j + a[i] + 1][k]);
        }
        /*for(ll t=1;t<=a[i]&&j+t<N;t++){
                pl(dp[i][j][k],dp[i-1][j+t][k]);
        }*/
      }
  }
  ll ans = 0;
  for (ll k = 0; k <= K; k++) {
    pl(ans, dp[n][0][k]);
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}