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 Ruby (2.7.1) (judge verdict: Runtime Error)

#!/usr/bin/env ruby

require 'set'

class Hash
  def push(key, value)
    self[key] = [] if self[key] == nil
    self[key].push(value)
  end
end

class Array
  def each_cons_with_ends(num)
    array = []
    each_cons(num).with_index do |*x, i|
      prev = i > 0 ? self[0..i-1] : []
      following = self[(i+num)..]
      yield(*x, prev, following) if block_given?
      array << [*x, prev, following]
    end
    array.to_enum
  end

  def lower_bound(value)
    left = -1;
    right = self.length;
    while left + 1 < right
      mid = left + (right - left) / 2;
      if self[mid] >= value
        right = mid
      else
        left = mid
      end
    end
    right
  end

  def unique
    res = [self.first]
    each_cons(2) do |a, b|
      if a != b
        res.push(b)
      end
    end
    res
  end
end

def get_ints
  gets.chomp.split.map(&:to_i)
end

def get_ints_minus_one
  get_ints.map { |x| x - 1 }
end

def get_int
  gets.chomp.to_i
end

def exponential_with_mod(n, k, mod)
  return 1 if k == 0
  return n if k == 1

  if k % 2 == 1
    (n * (exponential_with_mod(n, k/2, mod) ** 2 % mod)) % mod
  else
    (exponential_with_mod(n, k/2, mod) ** 2) % mod
  end
end

# mod m での aの逆元
# mが素数のときのみ有効
def modinv(a, m)
  b = m; u = 1; v = 0

  while b != 0
    t = a / b
    a -= t * b
    c = a; a = b; b = c
    u -= t * v
    c = u; u = v; v = c
  end
  u %= m
  u += m if u < 0
  u
end

def nCk(n, k, mod)
  ue = 1
  shita = 1
  k.times do |a|
    ue = ue * (n - a) % mod
    shita = shita * (k - a) % mod
  end
  ue * modinv(shita, mod) % mod
end

N = get_int

MOD = 10 ** 9 + 7

@memo = Array.new(N+1) { {} }

def ok(last4)
  4.times do |i|
    return false if last4.include?('ACG')
    chars = last4.split('')
    return false if chars.each_cons_with_ends(2).any? do |a, b, prev, fol|
      (prev + [b, a] + fol).join.include?('ACG')
    end
  end
  return true
end

def dfs(cur, last3)
  return @memo[cur][last3] if @memo[cur][last3]
  return 1 if cur == N

  ret = 0
  %w[A C G T].each do |s|
    ret = (ret + dfs(cur+1, last3[1..] + s)) % MOD if ok(last3 + s)
  end
  @memo[cur][last3] = ret
  ret
end

puts dfs(0, 'TTT')