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)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn is_disallowed(i: usize, j: usize, k: usize, l: usize) -> bool {
    // disallowed:
    // AGC: 0 2 1
    // GAC: 2 0 1
    // ACG: 0 1 2
    // AXGC: 0 X 2 1
    // AGXC: 0 2 X 1

    // AGC: 0 2 1
    if i == 0 && j == 2 && k == 1 {
        return false;
    }
    if j == 0 && k == 2 && l == 1 {
        return false;
    }

    // GAC: 2 0 1
    if i == 2 && j == 0 && k == 1 {
        return false;
    }
    if j == 2 && k == 0 && l == 1 {
        return false;
    }
    // ACG: 0 1 2
    if i == 0 && j == 1 && k == 2 {
        return false;
    }
    if j == 0 && k == 1 && l == 2 {
        return false;
    }

    // AXGC: 0 X 2 1
    // AGXC: 0 2 X 1
    if i == 0 && k == 2 && l == 1 {
        return false;
    }
    if i == 0 && j == 2 && l == 1 {
        return false;
    }
    true
}

fn main() {
    let n = read::<usize>();
    let big_int: u64 = 1000_000_000 + 7;
    if n == 3 {
        println!("61");
        return;
    }

    let mut dp = vec![vec![vec![vec![vec![0u64; 4]; 4]; 4]; 4]; n];
    // 0: A, 1: C, 2: G, 3: T
    for i in 0..4 {
        for j in 0..4 {
            for k in 0..4 {
                for l in 0..4 {
                    if !is_disallowed(i, j, k, l) {
                        continue;
                    }
                    dp[0][i][j][k][l] = 1;
                }
            }
        }
    }

    for ni in 1..n - 3 {
        for i in 0..4 {
            for j in 0..4 {
                for k in 0..4 {
                    for l in 0..4 {
                        if !is_disallowed(i, j, k, l) {
                            continue;
                        }
                        dp[ni][i][j][k][l] += dp[ni - 1][0][i][j][k];
                        dp[ni][i][j][k][l] %= big_int;
                        dp[ni][i][j][k][l] += dp[ni - 1][1][i][j][k];
                        dp[ni][i][j][k][l] %= big_int;
                        dp[ni][i][j][k][l] += dp[ni - 1][2][i][j][k];
                        dp[ni][i][j][k][l] %= big_int;
                        dp[ni][i][j][k][l] += dp[ni - 1][3][i][j][k];
                        dp[ni][i][j][k][l] %= big_int;
                    }
                }
            }
        }
    }
    let mut ans = 0;
    for i in 0..4 {
        for j in 0..4 {
            for k in 0..4 {
                for l in 0..4 {
                    ans += dp[n - 4][i][j][k][l];
                    ans %= big_int;
                }
            }
        }
    }
    println!("{}", ans);
}