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

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

constexpr int mod = 998244353;
constexpr int M = 350;
long long U[M][M][2 * M], D[M][M][2 * M], P[M][M][2 * M];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  int K;
  cin >> K;
  K = min(K, M - 10);
  vector<int> T;
  int cnt = 0, A = 0;
  for (auto &x : S) {
    if (x == '0') {
      T.emplace_back(cnt);
      cnt = 0;
    } else {
      cnt++, A++;
    }
  }
  int B = A + 10;
  T.emplace_back(cnt);
  int N = T.size(), C = 0;
  P[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    C += T[i];
    for (int j = 0; j <= B; j++) {
      for (int k = 0; k <= B * 2; k++) {
        P[i][j][k] %= mod;
        int nj = j + T[i], nk = k;
        if (C > nj) {
          int d = C - nj;
          nj += d, nk += d;
        }
        if (nj <= A) {
          if (nj < M && nk < 2 * M) {
            U[i + 1][nj][nk] += P[i][j][k];
            U[i + 1][nj][nk] %= mod;
          }
          if (A + 1 - nj + nk < 2 * M) {
            U[i + 1][A + 1][A + 1 - nj + nk] += mod - P[i][j][k];
            U[i + 1][A + 1][A + 1 - nj + nk] %= mod;
          }
        }
      }
    }
    for (int j = 0; j <= B; j++) {
      for (int k = 0; k <= B * 2; k++) {
        U[i + 1][j + 1][k + 1] += U[i + 1][j][k];
        U[i + 1][j + 1][k + 1] %= mod;
      }
    }
    for (int j = 0; j <= B; j++) {
      for (int k = 0; k <= B * 2; k++) {
        int nj = j, nk = k + T[i];
        if (nj < C) {
          int d = C - nj;
          nj += d, nk -= d;
        }
        if (nk > k) {
          if (nj < M && nk < 2 * M) {
            D[i + 1][nj][nk] += P[i][j][k];
            D[i + 1][nj][nk] %= mod;
          }
          if (j + T[i] < M) {
            D[i + 1][j + T[i]][k] += mod - P[i][j][k];
            D[i + 1][j + T[i]][k] %= mod;
          }
        }
      }
    }
    for (int j = 0; j <= B; j++) {
      for (int k = 0; k <= B * 2; k++) {
        if (k) {
          D[i + 1][j + 1][k - 1] += D[i + 1][j][k];
          D[i + 1][j + 1][k - 1] %= mod;
        }
      }
    }
    for (int j = 0; j <= B; j++) {
      for (int k = 0; k <= B * 2; k++) {
        assert(U[i + 1][j][k] >= 0 && P[i + 1][j][k] >= 0 &&
               D[i + 1][j][k] >= 0);
        P[i + 1][j][k] = (U[i + 1][j][k] + D[i + 1][j][k]) % mod;
      }
    }
  }
  long long Ans = 0;
  for (int i = 0; i <= 2 * K; i++) {
    if (i & 1)
      assert(P[N][A][i] == 0);
    Ans += P[N][A][i] % mod;
  }
  cout << (Ans % mod + mod) % mod << "\\n";
  return 0;
}

Error: -11

Fixed submission:

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

constexpr int mod = 998244353;
constexpr int M = 300;
long long U[M][M][2 * M], D[M][M][2 * M], P[M][M][2 * M];

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string S;
  cin >> S;
  int K;
  cin >> K;
  K = min(K, M);
  vector<int> T;
  int cnt = 0, A = 0;
  for (auto &x : S) {
    if (x == '0') {
      T.emplace_back(cnt);
      cnt = 0;
    } else {
      cnt++, A++;
    }
  }
  T.emplace_back(cnt);
  int N = T.size(), C = 0;
  P[0][0][0] = 1;
  for (int i = 0; i < N; i++) {
    C += T[i];
    for (int j = 0; j <= A; j++) {
      for (int k = 0; k <= A * 2; k++) {
        P[i][j][k] %= mod;
        int nj = j + T[i], nk = k;
        if (C > nj) {
          int d = C - nj;
          nj += d, nk += d;
        }
        if (nj <= A) {
          U[i + 1][nj][nk] += P[i][j][k];
          U[i + 1][A + 1][A + 1 - nj + nk] -= P[i][j][k];
        }
        U[i + 1][j + 1][k + 1] += U[i + 1][j][k];
      }
    }
    for (int j = 0; j <= A; j++) {
      for (int k = 0; k <= A * 2; k++) {
        int nj = j, nk = k + T[i];
        if (nj < C) {
          int d = C - nj;
          nj += d, nk -= d;
        }
        if (nk > k) {
          D[i + 1][nj][nk] += P[i][j][k];
          D[i + 1][j + T[i]][k] -= P[i][j][k];
        }
        if (k)
          D[i + 1][j + 1][k - 1] += D[i + 1][j][k];
      }
    }
    for (int j = 0; j <= A; j++) {
      for (int k = 0; k <= A * 2; k++) {
        P[i + 1][j][k] = (U[i + 1][j][k] + D[i + 1][j][k]) % mod;
      }
    }
  }
  long long Ans = 0;
  for (int i = 0; i <= 2 * K; i++) {
    Ans += P[N][A][i];
  }
  cout << Ans % mod << "\\n";
  return 0;
}