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::io::*;
use std::str::FromStr;
use std::cmp::{max};
use std::cell::RefCell;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char) 
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n: usize = read();
    let mut dp = Vec::with_capacity(n + 1);
    for i in 0..(n + 1) {
        dp.push(Vec::with_capacity(64));
        for _ in 0..64 {
            dp[i].push(0);
        }
    }
    println!("{}", rec(n, 63, &mut dp));
}

fn rec(n: usize, i: usize, dp: &mut Vec<Vec<u64>>) -> u64 {
    if dp[n][i] != 0 {
        return dp[n][i];
    } else if n == 0 {
        dp[n][i] = 1;
        return dp[n][i];
    }
    let mut ret = 0;
    for c in 0..4 {
        if ng((i << 2) + c) {
            continue;
        }
        let new_i = ((i << 2) + c) & 63;
        ret = add_mod(rec(n - 1, new_i, dp), ret);
    }
    dp[n][i] = ret;
    ret
}

// A C G T
// CGA
// ACG
// AG?C
fn ng(n: usize)  -> bool {
    let up = n >> 2;
    let down = n & 63;
    let pos1 = n & 243;
    let pos2 = n & 207;
    up == 6 || down == 6 || up == 33 || down == 33 || up == 9 || down == 9 || pos1 == 33 || pos2 == 9
}

const MOD: u64 = 1000000007;

fn add_mod(a: u64, b: u64) -> u64 {
    (a + b) % MOD
}

fn sub_mod(a: u64, b: u64) -> u64 {
    (a + MOD - b) % MOD
}

fn mul_mod(a: u64, b: u64) -> u64 {
    (a * b) % MOD
}

fn pow_mod(base: u64, exp: u64) -> u64 {
    if exp == 0 {
        1
    } else if exp == 1 {
        base
    } else {
        let prev = pow_mod(base, exp / 2);
        let tmp = if exp % 2 == 0 { 1 } else { base };
        mul_mod(mul_mod(prev, prev), tmp)
    }
}

fn div_mod(a: u64, b: u64) -> u64 {
    mul_mod(a, pow_mod(b, MOD - 2))
}

fn combination_mod(n: u64, k: u64) -> u64 {
    let mut ret = 1;
    for i in 0..k {
        let mul = if i < n { n - i } else { 0 };
        ret = div_mod(mul_mod(ret, mul), i + 1);
    }
    ret
}