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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int mod = 100000;
int dp[1 << 20][3], dp_prev[1 << 20][3];
int x[409], H, W;
string S;
int power[20];
int main() {
  power[0] = 1;
  for (int i = 1; i < 20; i++)
    power[i] = power[i - 1] * 2;
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == 'J')
        x[i * W + j] = 1;
      if (S[j] == 'O')
        x[i * W + j] = 2;
      if (S[j] == 'I')
        x[i * W + j] = 3;
      if (S[j] == '?')
        x[i * W + j] = 0;
    }
  }
  if (x[0] == 0 || x[0] == 1)
    dp_prev[0][1] += 1;
  if (x[0] == 0 || x[0] == 2)
    dp_prev[0][0] += 1;
  if (x[0] == 0 || x[0] == 3)
    dp_prev[0][0] += 1;
  for (int i = 1; i < H * W; i++) {
    int mass = i % W;
    for (int j = 0; j < power[W - 1]; j++) {
      int L = 0, R = 0;
      if ((j / power[mass]) % 2 == 1) {
        L = j - power[mass];
        R = L + power[mass - 1];
        if (x[i] == 0 || x[i] == 1) {
          if (mass != W - 1) {
            dp[L][1] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][1] %= mod;
          }
          if (mass == W - 1) {
            dp[L][0] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][0] %= mod;
          }
        }
        if (x[i] == 0 || x[i] == 2) {
          dp[L][0] += dp_prev[j][0];
          dp[L][0] %= mod;
          dp[R][0] += dp_prev[j][1];
          dp[R][0] %= mod;
        }
      } else {
        L = j;
        R = j + power[mass - 1];
        if (x[i] == 0 || x[i] == 1) {
          if (mass != W - 1) {
            dp[L][1] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][1] %= mod;
          }
          if (mass == W - 1) {
            dp[L][0] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][0] %= mod;
          }
        }
        if (x[i] == 0 || x[i] == 2) {
          dp[L][0] += dp_prev[j][0];
          dp[L][0] %= mod;
          dp[R][0] += dp_prev[j][1];
          dp[R][0] %= mod;
        }
        if (x[i] == 0 || x[i] == 3) {
          dp[L][0] += dp_prev[j][0] + dp_prev[j][1];
          dp[L][0] %= mod;
        }
      }
    }
    for (int j = 0; j < power[W - 1]; j++) {
      dp_prev[j][0] = dp[j][0];
      dp_prev[j][1] = dp[j][1];
      dp[j][0] = 0;
      dp[j][1] = 0;
    }
  }
  int C = 1;
  for (int i = 0; i < H * W; i++)
    if (x[i] == 0)
      C = C * 3 % mod;
  int sum = 0;
  for (int i = 0; i < power[W - 1]; i++) {
    sum += dp_prev[i][0] + dp_prev[i][1];
    sum %= mod;
  }
  cout << (C + mod - sum) % mod << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int mod = 100000;
int dp[1 << 20][3], dp_prev[1 << 20][3];
int x[409], H, W;
string S;
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S;
    for (int j = 0; j < W; j++) {
      if (S[j] == 'J')
        x[i * W + j] = 1;
      if (S[j] == 'O')
        x[i * W + j] = 2;
      if (S[j] == 'I')
        x[i * W + j] = 3;
      if (S[j] == '?')
        x[i * W + j] = 0;
    }
  }
  if (x[0] == 0 || x[0] == 1)
    dp_prev[0][1] += 1;
  if (x[0] == 0 || x[0] == 2)
    dp_prev[0][0] += 1;
  if (x[0] == 0 || x[0] == 3)
    dp_prev[0][0] += 1;
  for (int i = 1; i < H * W; i++) {
    int mass = i % W;
    for (int j = 0; j < (1 << (W - 1)); j++) {
      int L = 0, R = 0;
      if ((j >> mass) & 1) {
        L = j - (1 << mass);
        R = L + (1 << (mass - 1));
        if (x[i] == 0 || x[i] == 1) {
          if (mass != W - 1) {
            dp[L][1] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][1] %= mod;
          }
          if (mass == W - 1) {
            dp[L][0] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][0] %= mod;
          }
        }
        if (x[i] == 0 || x[i] == 2) {
          dp[L][0] += dp_prev[j][0];
          dp[L][0] %= mod;
          dp[R][0] += dp_prev[j][1];
          dp[R][0] %= mod;
        }
      } else {
        L = j;
        R = j + (1 << (mass - 1));
        if (x[i] == 0 || x[i] == 1) {
          if (mass != W - 1) {
            dp[L][1] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][1] %= mod;
          }
          if (mass == W - 1) {
            dp[L][0] += dp_prev[j][0] + dp_prev[j][1];
            dp[L][0] %= mod;
          }
        }
        if (x[i] == 0 || x[i] == 2) {
          dp[L][0] += dp_prev[j][0];
          dp[L][0] %= mod;
          dp[R][0] += dp_prev[j][1];
          dp[R][0] %= mod;
        }
        if (x[i] == 0 || x[i] == 3) {
          dp[L][0] += dp_prev[j][0] + dp_prev[j][1];
          dp[L][0] %= mod;
        }
      }
    }
    for (int j = 0; j < (1 << (W - 1)); j++) {
      dp_prev[j][0] = dp[j][0];
      dp_prev[j][1] = dp[j][1];
      dp[j][0] = 0;
      dp[j][1] = 0;
    }
  }
  int C = 1;
  for (int i = 0; i < H * W; i++)
    if (x[i] == 0)
      C = C * 3 % mod;
  int sum = 0;
  for (int i = 0; i < (1 << (W - 1)); i++) {
    sum += dp_prev[i][0] + dp_prev[i][1];
    sum %= mod;
  }
  // cout << (C + mod - sum) % mod << endl;
  return 0;
}