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

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

typedef long long ll;
const ll MOD = 998244353;

ll po[302][302];
ll C[602][602];
void init() {
  for (int i = 0; i < 302; i++) {
    po[i][0] = 1;
    for (int j = 0; j + 1 < 302; j++) {
      po[i][j + 1] = po[i][j] * i;
      po[i][j + 1] %= MOD;
    }
  }
  for (int i = 0; i < 602; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j <= i - 1; j++) {
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
      C[i][j] %= MOD;
    }
  }
}

int main() {
  init();
  string s;
  ll k;
  cin >> s >> k;

  ll n = 0;
  ll c[302];
  ll cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      c[n++] = cnt;
      cnt = 0;
    } else
      cnt++;
  }
  c[n++] = cnt;

  /*cout<<n<<endl;
  for(int i=0;i<n;i++){
          cout<<c[i]<<" ";
  }cout<<endl;*/

  static ll dp[4][302][302] = {};
  static ll t = 0, t_ = 1;
  dp[t][0][0] = 1;
  for (int x = 0; x < n; x++) {
    for (int i = 0; i < 302; i++) {
      for (int j = 0; j < 302; j++) {
        dp[t_][i][j] = dp[2][i][j] = dp[3][i][j] = 0;
      }
    }
    for (int i = 0; i < 302; i++) {
      if (i + c[x] + 1 >= 302)
        continue;
      for (int j = 0; j < 302; j++) {
        if (dp[t][i][j] == 0)
          continue;
        for (int y = 1; y <= j; y++) {
          dp[3][i + c[x] + 1][y] +=
              MOD - dp[t][i][j] * C[j - y + c[x]][j - y] % MOD;
        }
      }
    }
    /*for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                    cout<<dp[3][i][j]<<" ";
            }cout<<endl;
    }
    puts("==========");*/
    for (int i = 1; i < 302; i++) {
      for (int j = 300; j >= 1; j--) {
        dp[2][i][j] = dp[t][i - 1][j] + dp[2][i - 1][j] + dp[2][i][j + 1];
        dp[2][i][j] %= MOD;
      }
      for (int j = 1; j <= 300; j++) {
        dp[2][i][j] += dp[3][i][j];
        dp[2][i][j] %= MOD;
      }
    }
    for (int i = 0; i < 302; i++) {
      for (int j = 0; j < 302; j++) {
        dp[t_][i][j] = dp[2][i][j];
        if (j > 0) {
          dp[t_][i][j] += dp[t][i][j - 1];
          if (dp[t_][i][j] >= MOD)
            dp[t_][i][j] -= MOD;
        }
      }
    }
    swap(t, t_);
    /*for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                    cout<<dp[t][i][j]<<" ";
            }cout<<endl;
    }
    puts("==========");*/
  }

  ll ret = 0;
  for (int i = 0; i <= min(k, (ll)301); i++) {
    for (int j = 1; j <= 301; j++)
      ret += dp[t][i][j];
  }
  cout << ret % MOD << endl;

  /*ll dp[2][302][302]={};
  ll t=0,t_=1;
  dp[t][0][0]=1;
  for(int x=0;x<n;x++){
          for(int i=0;i<302;i++){
                  for(int j=0;j<302;j++){
                          dp[t_][i][j]=0;
                  }
          }
          for(int i=0;i<302;i++){
                  for(int j=0;j<302;j++){
                          if(dp[t][i][j]==0)continue;
                          dp[t_][i][j]+=dp[t][i][j];
                          dp[t_][i][j+1]+=MOD-dp[t][i][j]*(x+1-i)%MOD;
                          dp[t_][i+1][j+1]+=dp[t][i][j]*(x-i)%MOD;
                          dp[t_][i+1][j+c[x]+1]+=MOD-dp[t][i][j]*po[x-i][c[x]+1]%MOD;
                  }
          }
          for(int i=0;i<4;i++){
                  for(int j=0;j<4;j++){
                          cout<<dp[t_][i][j]<<" ";
                  }cout<<endl;
          }
          puts("==========");
          for(int i=0;i<302;i++){
                  dp[t_][i][0]%=MOD;
                  for(int j=0;j+1<302;j++){
                          dp[t_][i][j+1]+=dp[t_][i][j]*(x+1-i);
                          dp[t_][i][j+1]%=MOD;
                  }
          }
          swap(t,t_);
          for(int i=0;i<4;i++){
                  for(int j=0;j<4;j++){
                          cout<<dp[t][i][j]<<" ";
                  }cout<<endl;
          }
          puts("==========");
  }

  ll ret=0;
  for(int i=0;i<302;i++){
          for(int j=0;j<=k;j++){
                  ret+=dp[t][i][j];
          }
  }
  cout<<ret%MOD<<endl;*/
}


Error: 0

Fixed submission:

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

typedef long long ll;
const ll MOD = 998244353;

ll po[302][302];
ll C[602][602];
void init() {
  for (int i = 0; i < 302; i++) {
    po[i][0] = 1;
    for (int j = 0; j + 1 < 302; j++) {
      po[i][j + 1] = po[i][j] * i;
      po[i][j + 1] %= MOD;
    }
  }
  for (int i = 0; i < 602; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j <= i - 1; j++) {
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
      C[i][j] %= MOD;
    }
  }
}

int main() {
  init();
  string s;
  ll k;
  cin >> s >> k;

  ll n = 0;
  ll c[302];
  ll cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      c[n++] = cnt;
      cnt = 0;
    } else
      cnt++;
  }
  c[n++] = cnt;

  /*cout<<n<<endl;
  for(int i=0;i<n;i++){
          cout<<c[i]<<" ";
  }cout<<endl;*/

  static ll dp[4][302][302] = {};
  static ll t = 0, t_ = 1;
  dp[t][0][0] = 1;
  for (int x = 0; x < n; x++) {
    for (int i = 0; i < 302; i++) {
      for (int j = 0; j < 302; j++) {
        dp[t_][i][j] = dp[2][i][j] = dp[3][i][j] = 0;
      }
    }
    for (int i = 0; i < 302; i++) {
      if (i + c[x] + 1 >= 302)
        continue;
      for (int j = 0; j < 302; j++) {
        if (dp[t][i][j] == 0)
          continue;
        for (int y = 1; y <= j; y++) {
          dp[3][i + c[x] + 1][y] +=
              MOD - dp[t][i][j] * C[j - y + c[x]][j - y] % MOD;
        }
      }
    }
    /*for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                    cout<<dp[3][i][j]<<" ";
            }cout<<endl;
    }
    puts("==========");*/
    for (int i = 1; i < 302; i++) {
      for (int j = 300; j >= 1; j--) {
        dp[2][i][j] = dp[t][i - 1][j] + dp[2][i - 1][j] + dp[2][i][j + 1];
        dp[2][i][j] %= MOD;
      }
      for (int j = 1; j <= 300; j++) {
        dp[2][i][j] += dp[3][i][j];
        dp[2][i][j] %= MOD;
      }
    }
    for (int i = 0; i < 302; i++) {
      for (int j = 0; j < 302; j++) {
        dp[t_][i][j] = dp[2][i][j];
        if (j > 0) {
          dp[t_][i][j] += dp[t][i][j - 1];
          if (dp[t_][i][j] >= MOD)
            dp[t_][i][j] -= MOD;
        }
      }
    }
    swap(t, t_);
    /*for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                    cout<<dp[t][i][j]<<" ";
            }cout<<endl;
    }
    puts("==========");*/
  }

  ll ret = 0;
  for (int i = 0; i <= k; i++) {
    for (int j = 1; j <= 301; j++)
      ret += dp[t][i][j];
  }
  cout << ret % MOD << endl;

  /*ll dp[2][302][302]={};
  ll t=0,t_=1;
  dp[t][0][0]=1;
  for(int x=0;x<n;x++){
          for(int i=0;i<302;i++){
                  for(int j=0;j<302;j++){
                          dp[t_][i][j]=0;
                  }
          }
          for(int i=0;i<302;i++){
                  for(int j=0;j<302;j++){
                          if(dp[t][i][j]==0)continue;
                          dp[t_][i][j]+=dp[t][i][j];
                          dp[t_][i][j+1]+=MOD-dp[t][i][j]*(x+1-i)%MOD;
                          dp[t_][i+1][j+1]+=dp[t][i][j]*(x-i)%MOD;
                          dp[t_][i+1][j+c[x]+1]+=MOD-dp[t][i][j]*po[x-i][c[x]+1]%MOD;
                  }
          }
          for(int i=0;i<4;i++){
                  for(int j=0;j<4;j++){
                          cout<<dp[t_][i][j]<<" ";
                  }cout<<endl;
          }
          puts("==========");
          for(int i=0;i<302;i++){
                  dp[t_][i][0]%=MOD;
                  for(int j=0;j+1<302;j++){
                          dp[t_][i][j+1]+=dp[t_][i][j]*(x+1-i);
                          dp[t_][i][j+1]%=MOD;
                  }
          }
          swap(t,t_);
          for(int i=0;i<4;i++){
                  for(int j=0;j<4;j++){
                          cout<<dp[t][i][j]<<" ";
                  }cout<<endl;
          }
          puts("==========");
  }

  ll ret=0;
  for(int i=0;i<302;i++){
          for(int j=0;j<=k;j++){
                  ret+=dp[t][i][j];
          }
  }
  cout<<ret%MOD<<endl;*/
}