Problem p02635 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

void solve() {}
const ll mod = 998244353;
ll dp[2][309][309];
ll sum[2][309][609];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  string s;
  int k;
  cin >> s >> k;
  vi v;
  int cnt = 0;
  trav(i, s) {
    if (i == '1')
      cnt++;
    else
      v.pb(cnt), cnt = 0;
  }
  v.pb(cnt);
  if (sz(v) == 1) {
    cout << 1;
    return 0;
  }
  dp[0][0][0] = 1;
  int cur = 0;
  trav(i, v) {
    fill(dp[1 - cur]);
    repA(j, 0, k) repA(x, 0, k) {
      if (x && j)
        sum[0][j][x] = (sum[0][j - 1][x - 1] + dp[cur][j][x]) % mod;
      else
        sum[0][j][x] = dp[cur][j][x];
    }
    repD(j, k, 0) repD(x, k, 0) {
      sum[1][j][x] = (sum[1][j][x + 1] + dp[cur][j][x]) % mod;
    }
    repA(j, 0, k) repA(x, 0, k) {
      dp[1 - cur][j][x] = (sum[0][j][x] + sum[1][j][x] - dp[cur][j][x] -
                           sum[1][j][x + i + 1] + 2 * mod) %
                          mod;
    }
    cur = 1 - cur;
  }
  ll fns = 0;
  repA(j, 0, k) { fns += dp[cur][j][0]; }
  cout << fns % mod;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre() {}

void solve() {}
const ll mod = 998244353;
ll dp[2][309][309];
ll sum[2][309][609];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  string s;
  int k;
  cin >> s >> k;
  k = min(k, 300);
  vi v;
  int cnt = 0;
  trav(i, s) {
    if (i == '1')
      cnt++;
    else
      v.pb(cnt), cnt = 0;
  }
  v.pb(cnt);
  if (sz(v) == 1) {
    cout << 1;
    return 0;
  }
  dp[0][0][0] = 1;
  int cur = 0;
  trav(i, v) {
    fill(dp[1 - cur]);
    repA(j, 0, k) repA(x, 0, k) {
      if (x && j)
        sum[0][j][x] = (sum[0][j - 1][x - 1] + dp[cur][j][x]) % mod;
      else
        sum[0][j][x] = dp[cur][j][x];
    }
    repD(j, k, 0) repD(x, k, 0) {
      sum[1][j][x] = (sum[1][j][x + 1] + dp[cur][j][x]) % mod;
    }
    repA(j, 0, k) repA(x, 0, k) {
      dp[1 - cur][j][x] = (sum[0][j][x] + sum[1][j][x] - dp[cur][j][x] -
                           sum[1][j][x + i + 1] + 2 * mod) %
                          mod;
    }
    cur = 1 - cur;
  }
  ll fns = 0;
  repA(j, 0, k) { fns += dp[cur][j][0]; }
  cout << fns % mod;
  return 0;
}