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

#include <iostream>
#define M 100000007
using namespace std;

int r, c;
int dp[2][10][3][100000]; // dp[r][c][左からn本][上からn本]
int g[20][20];
char z;
int po3[21];

int main() {
  cin >> r >> c;

  po3[0] = 1;
  for (int i = 0; i < c; i++)
    po3[i + 1] = po3[i] * 3;

  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      cin >> z;
      if (z == '.')
        g[i][j] = 0;
      else
        g[i][j] = z - '0';
    }

  for (int i = 0; i < c; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < po3[c]; k++)
        dp[0][i][j][k] = 0;
  dp[0][0][0][0] = 1;

  for (int i = 0; i < r; i++) {
    int id = i & 1, nxt = 1 - id;

    for (int j = 0; j < c; j++)
      for (int k = 0; k < 3; k++)
        for (int l = 0; l < po3[c]; l++)
          dp[nxt][j][k][l] = 0;

    for (int j = 0; j < c - 1; j++) {
      if (!g[i][j]) {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < po3[c]; l++) {
            int m = (l / po3[j]) % 3;
            if (m && k)
              continue;
            dp[id][j + 1][k][l] += dp[id][j][k][l];
            dp[id][j + 1][k][l] %= M;
          }
        }
      } else {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < po3[c]; l++) {
            int m = (l / po3[j]) % 3;
            int n = g[i][j] - m - k;

            if (n < 0 || n > 4)
              continue;
            for (int x = 0; x < 3; x++) {
              if (n - x < 0 || n - x > 2)
                continue;
              int w = l - m * po3[j] + (n - x) * po3[j];
              dp[id][j + 1][x][w] += dp[id][j][k][l];
              dp[id][j + 1][x][w] %= M;
            }
          }
        }
      }
    }

    if (!g[i][c - 1]) {
      for (int l = 0; l < po3[c]; l++) {
        dp[nxt][0][0][l] += dp[id][c - 1][0][l];
        dp[nxt][0][0][l] %= M;
      }
    } else {
      for (int k = 0; k < 3; k++) {
        for (int l = 0; l < po3[c]; l++) {
          int m = (l / po3[c - 1]) % 3;
          int n = g[i][c - 1] - m - k;
          if (n < 0 || n > 2)
            continue;
          int w = l - m * po3[c - 1] + n * po3[c - 1];
          dp[nxt][0][0][w] += dp[id][c - 1][k][l];
          dp[nxt][0][0][w] %= M;
        }
      }
    }
  }

  cout << dp[r & 1][0][0][0] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#define M 100000007
using namespace std;

int r, c;
int dp[2][10][3][100000]; // dp[r][c][左からn本][上からn本]
int g[10][10];
char z;
int po3[21];

int main() {
  cin >> r >> c;

  po3[0] = 1;
  for (int i = 0; i < 20; i++)
    po3[i + 1] = po3[i] * 3;

  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      cin >> z;
      if (z == '.')
        g[i][j] = 0;
      else
        g[i][j] = z - '0';
    }

  for (int i = 0; i < c; i++)
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < po3[c + 3]; k++)
        dp[0][i][j][k] = 0;
  dp[0][0][0][0] = 1;

  for (int i = 0; i < r; i++) {
    int id = i & 1, nxt = 1 - id;

    for (int j = 0; j < c; j++)
      for (int k = 0; k < 3; k++)
        for (int l = 0; l < po3[c + 3]; l++)
          dp[nxt][j][k][l] = 0;

    for (int j = 0; j < c; j++) {
      if (!g[i][j]) {
        if (j == c - 1) {
          for (int l = 0; l < po3[c + 3]; l++) {
            int w = ((l / po3[c - 1]) % 3 + l * 3) % po3[c];
            dp[nxt][0][0][w] += dp[id][j][0][l];
            dp[nxt][0][0][w] %= M;
          }
        } else {
          for (int k = 0; k < 3; k++) {
            for (int l = 0; l < po3[c + 3]; l++) {
              int m = (l / po3[c - 1]) % 3;
              if (m && k)
                continue;
              int w = (m + l * 3) % po3[c];
              dp[id][j + 1][k][w] += dp[id][j][k][l];
              dp[id][j + 1][k][w] %= M;
            }
          }
        }
      } else {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < po3[c + 3]; l++) {
            int m = (l / po3[c - 1]) % 3;
            int n = g[i][j] - m - k;

            if (n < 0 || n > 4)
              continue;
            if (j == c - 1) {
              int w = (n + l * 3) % po3[c];
              if (n > 2)
                continue;
              dp[nxt][0][0][w] += dp[id][j][k][l];
              dp[nxt][0][0][w] %= M;
            } else {
              for (int x = 0; x < 3; x++) {
                if (n - x > 2)
                  continue;
                int w = (n - x + l * 3) % po3[c];
                dp[id][j + 1][x][w] += dp[id][j][k][l];
                dp[id][j + 1][x][w] %= M;
              }
            }
          }
        }
      }
    }
  }

  cout << dp[r & 1][0][0][0] << endl;
}