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 Rust (1.15.1) (judge verdict: Accepted)

mod reader {
    use std;

    macro_rules! read_line((  ) => {
                           {
                           let mut input = String :: new (  ) ; std :: io ::
                           stdin (  ) . read_line ( & mut input ) . unwrap (
                           ) ; input } } ;);

    pub fn read_and_parse<T: std::str::FromStr>() -> T {
        read_line!().trim().parse().ok().unwrap()
    }
}
use reader::*;

fn main() {
    const MOD: u64 = 1_000_000_000 + 7;
    const LETTERS: &'static str = "ACGT";
    const PROHIBIT: [&'static str; 6] =
        ["AGC", "ACG", "GAC", "AGGC", "AGTC", "ATGC"];

    let n: usize = read_and_parse();
    let mut table = Vec::new();
    let mut codons = Vec::new();
    table.resize(61, Vec::new());

    for codon in table.iter_mut() { codon.resize(n - 2, 0); codon[0] = 1 }

    for first in LETTERS.chars() {
        for second in LETTERS.chars() {
            for third in LETTERS.chars() {
                let mut codon: String = String::new();
                codon.push(first);
                codon.push(second);
                codon.push(third);

                if !PROHIBIT[..3].contains(&codon.as_str()) {
                    codons.push(codon);
                }
            }
        }
    }

    for i in 0..(n - 3) {
        for (codon_id, codon) in codons.iter().enumerate() {
            for append in LETTERS.chars() {
                let mut four_letters = codon.clone();
                four_letters.push(append);
                let mut three_letters = codon[1..].to_string();
                three_letters.push(append);

                if PROHIBIT[..3].contains(&three_letters.as_str()) ||
                       PROHIBIT[3..].contains(&four_letters.as_str()) {
                    continue ;
                }

                let matched_index =
                    codons.binary_search(&three_letters).ok().unwrap();

                table[matched_index][i + 1] += table[codon_id][i];
                table[matched_index][i + 1] %= MOD;
            }
        }
    }

    println!("{}" , table . iter (  ) . fold (
             0 , | acc , codon | ( acc + codon . last (  ) . unwrap (  ) ) %
             MOD ));
}