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)

use std::collections::HashMap;


const MOD: u64 = 1000000007;

fn main() {
    let n: usize = read();

    println!("{:?}", solve(n));
}


fn solve(n: usize) -> u64 {
    let mut g = G::new(n);
    
    g.dfs(0, "TTT".to_string())
}


#[derive(Debug)]
struct G {
    n: usize,
    memo: Vec<HashMap<String, u64>>,
}

impl G {
    fn new(n: usize) -> Self {
        G {
            n: n,
            memo: vec![HashMap::new(); n + 1],
        }
    }


    fn dfs(&mut self, cur: usize, last3: String) -> u64 {
        if let Some(ret) = self.memo[cur].get(&last3) {
            return *ret
        }

        if cur == self.n {
            return 1
        }

        let mut ret = 0;
        for c in "AGCT".chars() {
            let last4 = {
                let mut tmp = last3.clone();
                tmp.push(c);
                tmp
            };

            if Self::check(&last4) {
                ret = (ret + self.dfs(cur + 1, splt_off(&last4, 1))) % MOD;
            }
        }
        self.memo[cur].insert(last3, ret);
        ret
    }
    
    fn check(last4: &String) -> bool {
        let mut ret = true;
        for i in 0..4 {
            unsafe {
                let mut t = last4.clone();
                let mut t = t.as_mut_vec();

                if i >= 1 {
                    // (t[i - 1], t[i]) = (t[i], t[i - 1]);
                    let tmp = t[i];
                    t[i] = t[i - 1];
                    t[i - 1] = tmp;
                }

                if is_sub_string(&String::from_utf8_unchecked(t.to_vec()), &"AGC".to_string()) {
                    ret = false;
                    break;
                }
            }
        }

        ret
    }
}


fn is_sub_string(s: &String, key: &String) -> bool {
    s.matches(key).collect::<Vec<&str>>().len() >= 1
}

fn splt_off(s: &String, mid: usize) -> String {
    let (_, last) = s.split_at(mid);
    last.to_string()
}


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()
}