Problem:
Score : 400 points

Problem Statement

You are given an integer N. Find the number of strings of length N that satisfy the following conditions, modulo 10^9+7:

The string does not contain characters other than A, C, G and T.

The string does not contain AGC as a substring.

The condition above cannot be violated by swapping two adjacent characters once.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of strings of length N that satisfy the following conditions, modulo 10^9+7.

Sample Input 1

3

Sample Output 1

61

There are 4^3 = 64 strings of length 3 that do not contain characters other than A, C, G and T. Among them, only AGC, ACG and GAC violate the condition, so the answer is 64 - 3 = 61.

Sample Input 2

4

Sample Output 2

230

Sample Input 3

100

Sample Output 3

388130742

Be sure to print the number of strings modulo 10^9+7.

Solution in Ruby (2.3.3) (judge verdict: Accepted)

n = gets.to_i
# dp[桁][二つ前][ひとつ前]
# 選ばない=0, a=1, g=2, c=3, t=4
# a=1,g=2にc=3が続いてはダメ
# g=2,a=1にc=3が続いてはダメ
# a=1,c=3にg=2が続いてはダメ
# a=1,g=2,なんとか,にc=3が続いてはダメ
# a=1,なんとか,g=2,にc=3が続いてはダメ
# あとは自由

MOD = 10**9+7
dp = Array.new(n + 1) { Array.new(1000, 0) }
dp[0][0] = 1 # [桁][直近3回の状態を3桁の10進数で]

def each_state
  5.times do |i|
    5.times do |j|
      5.times do |k|
        yield i * 10 + j, j * 10 + k, i * 100 + j * 10 + k
      end
    end
  end
end

n.times do |i|
  each_state do |s1, s2, s3|
    1.upto(4) do |v|
      next if s2 == 12 && v == 3
      next if s2 == 21 && v == 3
      next if s2 == 13 && v == 2
      next if s1 == 12 && v == 3
      next if s3 == 112 && v == 3
      next if s3 == 122 && v == 3
      next if s3 == 132 && v == 3
      next if s3 == 142 && v == 3
      ns = s2 * 10 + v
      dp[i + 1][ns] += dp[i][s3]
      dp[i + 1][ns] %= MOD
    end
  end
end

ans = dp[n].inject(0, :+)
puts ans % MOD