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::*;

#[allow(dead_code)]
fn read<T: std::str::FromStr>() -> T {
    use std::io::Read;
    let stdin = std::io::stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

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


#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read_line::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn check(s: String) -> bool {
    if s.contains("AGC") {
        return false;
    }
    for i in 0..s.len()-1 {
        let mut c: Vec<char> = s.chars().collect();
        let c0 = c[i];
        let c1 = c[i+1];
        c[i] = c1;
        c[i+1] = c0;
        let tmp_s = c.iter().fold("".to_string(), |acc, x| format!("{}{}", acc, x));
        if tmp_s.contains("AGC") {
            return false;
        }
    }
    true
}

fn dfs(d: i32, N: i32, last_s: String, hm: &mut HashMap<(i32, String), i32>) -> i32 {
    if d == N {
        return 1
    }
    if hm.contains_key(&(d, last_s.clone())) {
        return hm.get(&(d, last_s.clone())).unwrap().clone();
    }
    let mut tmp_cnt = 0;
    let chars = ["A".to_string(), "G".to_string(), "C".to_string(), "T".to_string()];
    for c in chars.iter() {
        let s: String = format!("{}{}", last_s.clone(), c.clone());
        if check(s) {
            let mut next_s = last_s.clone();
            next_s.remove(0);
            let next_s: String = format!("{}{}", next_s, c.clone());
            tmp_cnt = (tmp_cnt + dfs(d+1, N, next_s, hm)) % 1000000007;
        }
    }
    hm.insert((d, last_s), tmp_cnt);
    tmp_cnt
}

#[allow(non_snake_case)]
fn main() {
    let N: u32 = read();

    println!("{}", dfs(0i32, N as i32, "TTT".to_string(), &mut HashMap::new()));
}