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(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_imports)]

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use std::cmp::*;

struct Scanner<I: Iterator<Item = char>> {
    iter: std::iter::Peekable<I>,
}

macro_rules! exit {
    () => {{
        exit!(0)
    }};
    ($code:expr) => {{
        if cfg!(local) {
            writeln!(std::io::stderr(), "===== Terminated =====")
                .expect("failed printing to stderr");
        }
        std::process::exit($code);
    }}
}

impl<I: Iterator<Item = char>> Scanner<I> {
    pub fn new(iter: I) -> Scanner<I> {
        Scanner {
            iter: iter.peekable(),
        }
    }

    pub fn safe_get_token(&mut self) -> Option<String> {
        let token = self.iter
            .by_ref()
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        if token.is_empty() {
            None
        } else {
            Some(token)
        }
    }

    pub fn token(&mut self) -> String {
        self.safe_get_token().unwrap_or_else(|| exit!())
    }

    pub fn get<T: FromStr>(&mut self) -> T {
        self.token().parse::<T>().unwrap_or_else(|_| exit!())
    }

    pub fn vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
        (0..len).map(|_| self.get()).collect()
    }

    pub fn mat<T: FromStr>(&mut self, row: usize, col: usize) -> Vec<Vec<T>> {
        (0..row).map(|_| self.vec(col)).collect()
    }

    pub fn char(&mut self) -> char {
        self.iter.next().unwrap_or_else(|| exit!())
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }

    pub fn line(&mut self) -> String {
        if self.peek().is_some() {
            self.iter
                .by_ref()
                .take_while(|&c| !(c == '\n' || c == '\r'))
                .collect::<String>()
        } else {
            exit!();
        }
    }

    pub fn peek(&mut self) -> Option<&char> {
        self.iter.peek()
    }
}

trait Joinable {
    fn join(self, sep: &str) -> String;
}

impl<U: ToString, T: Iterator<Item = U>> Joinable for T {
    fn join(self, sep: &str) -> String {
        self.map(|x| x.to_string()).collect::<Vec<_>>().join(sep)
    }
}

use std::ops::*;

const MOD: usize = 1000000007;
#[derive(Copy, Clone)]
struct ModInt {
    n: usize,
}

impl ModInt {
    fn pow(self, power: usize) -> ModInt {
        if power == 0 {
            return mod_int(1);
        }
        if power % 2 == 0 {
            let t = self.pow(power/2);
            return t * t;
        }
        self * self.pow(power-1)
    }
    fn inv(self) -> ModInt {
        self.pow(MOD - 2)
    }
}

fn mod_int(n: usize) -> ModInt {
    ModInt { n: n % MOD }
}

impl Add for ModInt {
    type Output = ModInt;

    fn add(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        tmp.n += rhs.n;
        if tmp.n >= MOD {
            tmp.n -= MOD;
        }
        tmp
    }
}

impl AddAssign for ModInt {
    fn add_assign(&mut self, rhs: ModInt) {
        *self = *self + rhs;
    }
}

impl Sub for ModInt {
    type Output = ModInt;

    fn sub(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        if tmp.n < rhs.n {
            tmp.n += MOD;
        }
        tmp.n -= rhs.n;
        tmp
    }
}

impl SubAssign for ModInt {
    fn sub_assign(&mut self, rhs: ModInt) {
        *self = *self - rhs;
    }
}

impl Mul for ModInt {
    type Output = ModInt;

    fn mul(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        tmp.n *= rhs.n;
        tmp.n %= MOD;
        tmp
    }
}

impl MulAssign for ModInt {
    fn mul_assign(&mut self, rhs: ModInt) {
        *self = *self * rhs;
    }
}

impl Div for ModInt {
    type Output = ModInt;

    fn div(self, rhs: ModInt) -> ModInt {
        let mut tmp = self;
        tmp.n *= rhs.inv().n;
        tmp.n %= MOD;
        tmp
    }
}

impl DivAssign for ModInt {
    fn div_assign(&mut self, rhs: ModInt) {
        *self = *self / rhs;
    }
}

fn main() {
    let cin = stdin();
    let cin = cin.lock();
    let mut sc = Scanner::new(cin.bytes().map(|c| c.unwrap() as char));
    let N: usize = sc.get();
    let mut dp = vec![vec![vec![vec![0; 4]; 4]; 4]; N+1];
    dp[0][3][3][3] = 1;
    for i in 0..N {
        for a in 0..4 {
            for b in 0..4 {
                for c in 0..4 {
                    for n in 0..4 {
                        if b == 0 && a == 2 && n == 1 { continue; }
                        if b == 2 && a == 0 && n == 1 { continue; }
                        if b == 0 && a == 1 && n == 2 { continue; }
                        if c == 0 && a == 2 && n == 1 { continue; }
                        if c == 0 && b == 2 && n == 1 { continue; }
                        dp[i+1][n][a][b] += dp[i][a][b][c];
                        dp[i+1][n][a][b] %= MOD;
                    }
                }
            }
        }
    }
    let mut ans = 0;
    for a in 0..4 {
        for b in 0..4 {
            for c in 0..4 {
                ans += dp[N][a][b][c];
                ans %= MOD
            }
        }
    }
    println!("{}", ans);
}