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;

class Procon
{
    public Procon() { }
    static void Main(string[] agrs)
    {
        new Procon().Do();
    }

    Scanner cin;

    int N;

    Dictionary<string, long>[] last4str;

    string[] Alphabet = new string[4] { "A", "G", "C", "T" };

    public bool Check(string key)
    {
        if (key.Contains("AGC"))
        {
            return true;
        }

        for (int i = 1; i < key.Length; i++)
        {
            char[] tmpCharArray = key.ToCharArray();
            char tmp = tmpCharArray[i];
            tmpCharArray[i] = tmpCharArray[i - 1];
            tmpCharArray[i - 1] = tmp;
            string tmpKey = new string(tmpCharArray);
            if (tmpKey.Contains("AGC"))
            {
                return true;
            }
        }
        return false;
    }

    public long Count(long i)
    {
        last4str[i] = new Dictionary<string, long>();
        if (i == 0)
        {
            last4str[i]["A"] = 1;
            last4str[i]["G"] = 1;
            last4str[i]["C"] = 1;
            last4str[i]["T"] = 1;
        }
        else
        {
            foreach (string k in last4str[i - 1].Keys)
            {
                foreach (string a in Alphabet)
                {
                    var newKey = k + a;
                    if (Check(newKey))
                    {
                        continue;
                    }
                    newKey = newKey.Substring(Math.Max(newKey.Length - 3, 0));
                    if (last4str[i].ContainsKey(newKey))
                    {
                        last4str[i][newKey] = (last4str[i][newKey] + last4str[i - 1][k]) % 1000000007;
                    }
                    else
                    {
                        last4str[i].Add(newKey, last4str[i - 1][k]);
                    }
                }
            }
        }

        long ans = 0;
        foreach (long v in last4str[i].Values)
        {
            ans = (ans + v) % 1000000007;
        }

        return ans;
    }

    public void Do()
    {
        cin = new Scanner();
        N = cin.nextInt();

        last4str = new Dictionary<string, long>[N];
        long ans = 0;
        for (int i = 0; i < N; i++)
        {
            ans = Count(i);
        }
        Console.WriteLine(ans);
    }
}

class Scanner
{
    string[] s;
    int i;
    int max_i;

    public Scanner()
    {
        s = new string[0];
        max_i = 0;
        i = 0;
    }

    public string nextString()
    {
        if (i >= s.Length)
        {
            s = Console.ReadLine().Split(' ');
            max_i = s.Length;
            i = 0;
            if (max_i == 0)
            {
                return "";
            }
            return s[i++];
        }
        else
        {
            return s[i++];
        }
    }

    public int nextInt()
    {
        return int.Parse(nextString());
    }

    public long nextLong()
    {
        return long.Parse(nextString());
    }

    public double nextDouble()
    {
        return double.Parse(nextString());
    }

    public string[] arrayString()
    {
        return Console.ReadLine().Split(' ');
    }

    public int[] arrayInt()
    {
        string[] str = Console.ReadLine().Split(' ');
        int[] array = new int[str.Length];
        for (int i = 0; i < str.Length; i++)
        {
            array[i] = int.Parse(str[i]);
        }
        return array;
    }

    public long[] arrayLong()
    {
        string[] str = Console.ReadLine().Split(' ');
        long[] array = new long[str.Length];
        for (int i = 0; i < str.Length; i++)
        {
            array[i] = long.Parse(str[i]);
        }
        return array;
    }

    public double[] arrayDouble()
    {
        string[] str = Console.ReadLine().Split(' ');
        double[] array = new double[str.Length];
        for (int i = 0; i < str.Length; i++)
        {
            array[i] = double.Parse(str[i]);
        }
        return array;
    }
}