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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

static class CodeJam
{
    static bool Safe(string s)
    {
        string s3 = s.Substring(Math.Max(0, s.Length - 3));

        if (s3 == "AGC") { return false; }
        if (s3 == "GAC") { return false; }
        if (s3 == "ACG") { return false; }

        if (s.Length == 4)
        {
            if (s[0] == 'A' && s[1] == 'G' && s[3] == 'C') { return false; }
            if (s[0] == 'A' && s[2] == 'G' && s[3] == 'C') { return false; }
        }

        return true;
    }

    static long Solve(int N)
    {
        var C = new[] { 'A', 'C', 'G', 'T' };
        var dp1 = new Dictionary<string, long>();
        foreach (char c1 in C)
        {
            foreach (char c2 in C)
            {
                dp1[c1.ToString() + c2.ToString()] = 1;
            }
        }
        for (int i = 2; i < N; ++i)
        {
            var dp2 = new Dictionary<string, long>();
            foreach (var key1 in dp1.Keys)
            {
                foreach (char c in C)
                {
                    string key2 = key1 + c.ToString();
                    if (Safe(key2))
                    {
                        string key = key2.Substring(Math.Max(key2.Length - 3, 0));
                        if (!dp2.ContainsKey(key)) { dp2[key] = 0; }
                        dp2[key] = (dp2[key] + dp1[key1]) % 1000000007;
                    }
                }
            }
            dp1 = dp2;
        }
        long sum = 0;
        foreach (long value in dp1.Values) { sum = (sum + value) % 1000000007; }
        return sum;
    }

    static void Main(string[] args)
    {
        int N = int.Parse(Console.ReadLine());
        Console.WriteLine(Solve(N));
    }
}