Problem p03088 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
constexpr Int mod = 1e9 + 7;
string str{"AGCT"};
#define rep(i, n) for (int i = 0; i < n; ++i)
int main() {
  Int N;
  cin >> N;
  Int dp[101][5][5] = {0};
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < str.size(); ++j) {
      for (int k = 0; k < str.size(); ++k) {
        char prev = str[k];
        char lst = str[j];
        dp[i][j][k] %= mod;
        if (i == 0 and (j == 4 or k == 4))
          continue;
        Int now = (i ? dp[i][j][k] : 1);
        if (now == 0)
          continue;
        if (lst == 'A' and prev == 'C') {

          rep(l, 4) if (str[l] != 'G') dp[i + 1][k][l] += now;

        } else if (lst == 'A' and prev == 'G') {

          rep(l, 4) rep(m, 4) {
            if (str[m] == 'C' or str[l] == 'C')
              continue;
            dp[i + 2][m][l] += now;
          }
          dp[i + 1][k][4] += now;

        } else if (lst == 'G' and prev == 'A') {

          rep(l, 4) if (str[l] != 'C') dp[i + 1][k][l] += now;

        } else if (lst == 'A' and prev == 'T') {

          rep(l, 4) rep(m, 4) {
            if (str[l] == 'G' and str[m] == 'C')
              continue;
            dp[i + 2][l][m] += now;
          }
          dp[i + 1][3][4] += now;

        } else {
          rep(l, 4) dp[i + 1][k][l] += now;
        }
      }
    }
  }
  Int ans = 0;
  for (int j = 0; j < 4; ++j)
    for (int k = 0; k < 4; ++k) {
      ans += dp[N - 2][j][k];
      ans %= mod;
    }

  ans += dp[N - 2][1][4] * 3;
  ans += dp[N - 2][3][4] * 4;
  ans %= mod;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Int = long long;
constexpr Int mod = 1e9 + 7;
string str{"AGCT"};
#define rep(i, n) for (int i = 0; i < n; ++i)
int main() {
  Int N;
  cin >> N;
  Int dp[106][5][5] = {0};
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < str.size(); ++j) {
      for (int k = 0; k < str.size(); ++k) {
        char prev = str[k];
        char lst = str[j];
        dp[i][j][k] %= mod;
        if (i == 0 and (j == 4 or k == 4))
          continue;
        Int now = (i ? dp[i][j][k] : 1);
        if (now == 0)
          continue;
        if (lst == 'A' and prev == 'C') {

          rep(l, 4) if (str[l] != 'G') dp[i + 1][k][l] += now;

        } else if (lst == 'A' and prev == 'G') {

          rep(l, 4) rep(m, 4) {
            if (str[m] == 'C' or str[l] == 'C')
              continue;
            dp[i + 2][m][l] += now;
          }
          dp[i + 1][k][4] += now;

        } else if (lst == 'G' and prev == 'A') {

          rep(l, 4) if (str[l] != 'C') dp[i + 1][k][l] += now;

        } else if (lst == 'A' and prev == 'T') {

          rep(l, 4) rep(m, 4) {
            if (str[l] == 'G' and str[m] == 'C')
              continue;
            dp[i + 2][l][m] += now;
          }
          dp[i + 1][3][4] += now;

        } else {
          rep(l, 4) dp[i + 1][k][l] += now;
        }
      }
    }
  }
  Int ans = 0;
  for (int j = 0; j < 4; ++j)
    for (int k = 0; k < 4; ++k) {
      ans += dp[N - 2][j][k];
      ans %= mod;
    }

  ans += dp[N - 2][1][4] * 3;
  ans += dp[N - 2][3][4] * 4;
  ans %= mod;
  cout << ans << endl;
}