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

#include <iostream>
using namespace std;
int n, dp[1001][8];
string str;
int change(char ch) {
  if (ch == 'J')
    return 0;
  if (ch == 'O')
    return 1;
  else
    return 2;
}
int main() {
  cin >> n >> str;
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 8; j++) {
      dp[i][j] = 0;
    }
  }
  // j=0 o=1 i=2
  dp[0][4] = 1;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'J') {
      dp[i + 1][1] = 0;                                         // I
      dp[i + 1][2] = 0;                                         // O
      dp[i + 1][3] = 0;                                         // OI
      dp[i + 1][4] = dp[i][7] + dp[i][6] + dp[i][5] + dp[i][4]; // J
      dp[i + 1][5] =
          dp[i][1] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] +
          dp[i]
            [7]; // JI
                 // I&#227;&#129;&#8249;J&#227;&#129;&#338;&#233;&#141;&#181;&#227;&#8218;’&#229;‰&#141;&#230;—\\&#227;&#8218;&#8218;&#227;&#129;&#161;&#227;&#129;&#8249;&#227;&#129;&#710;&#227;&#129;£&#227;&#129;&#376;
      dp[i + 1][6] =
          dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; // JO
      dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] +
                     dp[i][6] + dp[i][7]; // JOI
    } else if (str[i] == 'O') {
      dp[i + 1][1] = 0;                                         // I
      dp[i + 1][2] = dp[i][2] + dp[i][3] + dp[i][6] + dp[i][7]; // O
      dp[i + 1][3] =
          dp[i][1] + dp[i][2] + dp[i][3] + dp[i][5] + dp[i][6] + dp[i][7]; // IO
      dp[i + 1][4] = 0;
      dp[i + 1][5] = 0;
      dp[i + 1][6] =
          dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
      dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] +
                     dp[i][6] + dp[i][7]; // JOI
    } else {
      dp[i + 1][1] = dp[i][1] + dp[i][3] + dp[i][5] + dp[i][7];
      dp[i + 1][2] = 0;
      dp[i + 1][3] =
          dp[i][1] + dp[i][2] + dp[i][3] + dp[i][5] + dp[i][6] + dp[i][7]; // IO
      dp[i + 1][4] = 0;
      dp[i + 1][5] =
          dp[i][1] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
      dp[i + 1][6] = 0;
      dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] +
                     dp[i][6] + dp[i][7]; // JOI
    }
    for (int j = 0; j < 8; j++)
      dp[i + 1][j] %= 10007;
  }
  int sum = 0;
  for (int i = 0; i < 8; i++)
    sum += dp[n][i];
  cout << (sum) % 10007 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int n, dp[1001][8];
string str;
int change(char ch) {
  if (ch == 'J')
    return 0;
  if (ch == 'O')
    return 1;
  else
    return 2;
}
int main() {
  cin >> n >> str;
  for (int i = 0; i < 1001; i++) {
    for (int j = 0; j < 8; j++) {
      dp[i][j] = 0;
    }
  }
  int flg;
  // j=0 o=1 i=2
  dp[0][4] = 1;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'J') {
      flg = 0;
      for (int j = 1; j < 8; j++) {
        dp[i + 1][i] += dp[i][j];
      }
      dp[i + 1][1] = 0;                                         // I
      dp[i + 1][2] = 0;                                         // O
      dp[i + 1][3] = 0;                                         // OI
      dp[i + 1][4] = dp[i][7] + dp[i][6] + dp[i][5] + dp[i][4]; // J
      dp[i + 1][5] = dp[i][1] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] +
                     dp[i][7]; // JI IかJが鍵を前日もちかえった
      dp[i + 1][6] =
          dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7]; // JO
      dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] +
                     dp[i][6] + dp[i][7]; // JOI
    } else if (str[i] == 'O') {
      dp[i + 1][1] = 0;                                         // I
      dp[i + 1][2] = dp[i][2] + dp[i][3] + dp[i][6] + dp[i][7]; // O
      dp[i + 1][3] =
          dp[i][1] + dp[i][2] + dp[i][3] + dp[i][5] + dp[i][6] + dp[i][7]; // IO
      dp[i + 1][4] = 0;
      dp[i + 1][5] = 0;
      dp[i + 1][6] =
          dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
      dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] +
                     dp[i][6] + dp[i][7]; // JOI
    } else {
      dp[i + 1][1] = dp[i][1] + dp[i][3] + dp[i][5] + dp[i][7];
      dp[i + 1][2] = 0;
      dp[i + 1][3] =
          dp[i][1] + dp[i][2] + dp[i][3] + dp[i][5] + dp[i][6] + dp[i][7]; // IO
      dp[i + 1][4] = 0;
      dp[i + 1][5] =
          dp[i][1] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
      dp[i + 1][6] = 0;
      dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] +
                     dp[i][6] + dp[i][7]; // JOI
    }
    for (int j = 0; j < 8; j++)
      dp[i + 1][j] %= 10007;
  }
  int sum = 0;
  for (int i = 0; i < 8; i++)
    sum += dp[n][i];
  cout << (sum) % 10007 << endl;

  return 0;
}