Problem p03088 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, (n)-1)
typedef long long ll;

#define mod 1000000007

ll madd(ll a, ll b) { return (a + b) % mod; }
ll msub(ll a, ll b) { return (a + mod - b) % mod; }
ll mmul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll mpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b == 1)
    return a % mod;
  else if (b % 2 == 0) {
    ll hlf = mpow(a, b / 2);
    return (hlf * hlf) % mod;
  } else {
    ll hlf = mpow(a, b / 2);
    return (mmul(hlf, hlf) * a) % mod;
  }
}
ll mdiv(ll a, ll b) { return mmul(a, mpow(b, mod - 2)); }

// AGC ... 021
bool isAGC(int a, int b, int c, int d) {
  if (a == 0 && b == 2 && c == 1)
    return true;
  if (b == 0 && c == 2 && d == 1)
    return true;
  return false;
}

// A 0 C 1 G 2 T 3
bool islegal(int a, int b, int c, int d) {
  if (isAGC(a, b, c, d))
    return false;
  if (isAGC(b, a, c, d))
    return false;
  if (isAGC(a, c, b, d))
    return false;
  if (isAGC(a, b, d, c))
    return false;
  return true;
}

ll ans[101][256];

int main() {
  int N;
  cin >> N;
  if (N == 3) {
    cout << "61" << endl;
    return 0;
  }
  if (N == 4) {
    cout << "230" << endl;
    return 0;
  }
  // tmpの初期化
  rep(i, 256) {
    int a, b, c, d;
    a = i / 64;
    b = (i - a * 64) / 16;
    c = (i - a * 64 - b * 16) / 4;
    d = (i - a * 64 - b * 16 - c * 4);
    if (islegal(a, b, c, d))
      ans[4][i] = 1;
    else
      ans[4][i] = 0;
  }
  FOR(j, 5, N + 1) {
    rep(i, 256) {
      int a, b, c, d;
      a = i / 64;
      b = (i - a * 64) / 16;
      c = (i - a * 64 - b * 16) / 4;
      d = (i - a * 64 - b * 16 - c * 4);
      ans[j][i] = 0;
      if (ans[4][a * 64 + b * 16 + c * 4 + d] == 1) {
        rep(k, 4) {
          ans[j][i] = madd(ans[j][i], ans[j - 1][k * 64 + a * 16 + b * 4 + c]);
        }
      }
    }
  }
  ll ans2 = 0;
  rep(i, 256) ans2 = madd(ans2, ans[N][i]);
  cout << ans2 << endl;
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) FOR(i, 0, (n)-1)
typedef long long ll;

#define mod 1000000007

ll madd(ll a, ll b) { return (a + b) % mod; }
ll msub(ll a, ll b) { return (a + mod - b) % mod; }
ll mmul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll mpow(ll a, ll b) {
  if (b == 0)
    return 1;
  else if (b == 1)
    return a % mod;
  else if (b % 2 == 0) {
    ll hlf = mpow(a, b / 2);
    return (hlf * hlf) % mod;
  } else {
    ll hlf = mpow(a, b / 2);
    return (mmul(hlf, hlf) * a) % mod;
  }
}
ll mdiv(ll a, ll b) { return mmul(a, mpow(b, mod - 2)); }

// AGC ... 021
bool isAGC(int a, int b, int c, int d) {
  if (a == 0 && b == 2 && c == 1)
    return true;
  if (b == 0 && c == 2 && d == 1)
    return true;
  return false;
}

// A 0 C 1 G 2 T 3
bool islegal(int a, int b, int c, int d) {
  if (isAGC(a, b, c, d))
    return false;
  if (isAGC(b, a, c, d))
    return false;
  if (isAGC(a, c, b, d))
    return false;
  if (isAGC(a, b, d, c))
    return false;
  return true;
}

ll ans[103][256];

int main() {
  int N;
  cin >> N;
  if (N == 3) {
    cout << "61" << endl;
    return 0;
  }
  if (N == 4) {
    cout << "230" << endl;
    return 0;
  }
  // tmpの初期化
  rep(i, 256) {
    int a, b, c, d;
    a = i / 64;
    b = (i - a * 64) / 16;
    c = (i - a * 64 - b * 16) / 4;
    d = (i - a * 64 - b * 16 - c * 4);
    if (islegal(a, b, c, d))
      ans[4][i] = 1;
    else
      ans[4][i] = 0;
  }
  FOR(j, 5, N + 1) {
    rep(i, 256) {
      int a, b, c, d;
      a = i / 64;
      b = (i - a * 64) / 16;
      c = (i - a * 64 - b * 16) / 4;
      d = (i - a * 64 - b * 16 - c * 4);
      ans[j][i] = 0;
      if (ans[4][a * 64 + b * 16 + c * 4 + d] == 1) {
        rep(k, 4) {
          ans[j][i] = madd(ans[j][i], ans[j - 1][k * 64 + a * 16 + b * 4 + c]);
        }
      }
    }
  }
  ll ans2 = 0;
  rep(i, 256) ans2 = madd(ans2, ans[N][i]);
  cout << ans2 << endl;
  return 0;
}