qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n=int(input())
mod=10**9+7
l=['A','C','G','T']
NG=['AGC','GAC','ACG']
NG2=['AAGC','ACGC','AGGC','ATGC','AGAC','AGCC','AGTC']
L=[]
for i1 in range(4):
  for i2 in range(4):
    for i3 in range(4):
      for i4 in range(4):
        L.append(l[i1]+l[i2]+l[i3]+l[i4])
dp=[[0 for i in range(256)] for j in range(n+1)]
for i in range(256):
  if L[i][1:4] in NG or L[i][0:3] in NG or L[i] in NG2:
    dp[4][i]=0
  else:
    dp[4][i]=1
for j in range(5,n+1):
  for i in range(256):
    if L[i][1:4] in NG or L[i][0:3] in NG or L[i] in NG2:
      dp[j][i]=0
    else:
      for i2 in range(256):
        if L[i2][1:4]==L[i][0:3]:
          dp[j][i]+=dp[j-1][i2]
    dp[j][i]%=mod
print(sum(dp[n])%mod)

Fixed code:
import sys
n=int(input())
if n==3:
  print(61)
  sys.exit()
mod=10**9+7
l=['A','C','G','T']
NG=['AGC','GAC','ACG']
NG2=['AAGC','ACGC','AGGC','ATGC','AGAC','AGCC','AGTC']
L=[]
for i1 in range(4):
  for i2 in range(4):
    for i3 in range(4):
      for i4 in range(4):
        L.append(l[i1]+l[i2]+l[i3]+l[i4])
dp=[[0 for i in range(256)] for j in range(n+1)]
for i in range(256):
  if L[i][1:4] in NG or L[i][0:3] in NG or L[i] in NG2:
    dp[4][i]=0
  else:
    dp[4][i]=1
for j in range(5,n+1):
  for i in range(256):
    if L[i][1:4] in NG or L[i][0:3] in NG or L[i] in NG2:
      dp[j][i]=0
    else:
      for i2 in range(256):
        if L[i2][1:4]==L[i][0:3]:
          dp[j][i]+=dp[j-1][i2]
    dp[j][i]%=mod
print(sum(dp[n])%mod)
Buggy code:
n=int(input())
mod=10**9+7

dp=[[[[0]*5 for _ in range(5)] for _ in range(5)] for _ in range(n+1)]
dp[0][0][0][0]=1

for x in range(n):
    for i in range(5):
        for j in range(5):
            for k in range(5):
                for l in range(5):
                    if i==1 and j==2 and l==3:
                        continue
                    if i==1 and k==2 and l==3:
                        continue
                    if j==1 and k==2 and l==3:
                        continue
                    if j==2 and k==1 and l==3:
                        continue
                    if j==1 and k==3 and l==2:
                        continue
                    dp[x+1][j][k][l]=(dp[x+1][j][k][l]+dp[x][i][j][k])%mod
ans=0
for i in range(1,5):
    for j in range(1,5):
        for k in range(1,5):
            ans =(ans +dp[n][i][j][k])%mod

print(ans)

Fixed code:
n = int(input())
dp = [[[[0] * 5 for _ in range(5)] for _ in range(5)] for _ in range(n + 1)]
mod = 10 ** 9 + 7

dp[0][0][0][0] = 1
for i in range(n):
    for j in range(5):
        for k in range(5):
            for l in range(5):
                for s in range(1, 5):
                    if l == 3 and k == 1 and s == 2:
                        continue
                    if l == 2 and k == 1 and s == 3:
                        continue
                    if l == 1 and k == 3 and s == 2:
                        continue
                    if k == 3 and j == 1 and s == 2:
                        continue
                    if l == 3 and j == 1 and s == 2:
                        continue
                    dp[i + 1][s][l][k] += dp[i][l][k][j]
                    dp[i + 1][s][l][k] %= mod
ans = 0
for j in range(1, 5):
    for k in range(1, 5):
        for l in range(1, 5):
            ans += dp[n][j][k][l]
print(ans % mod)
Buggy code:
N, MOD = int(input()), 10 ** 9 + 7
memo = [{} for i in range(N+1)]
def ok(last4):
    for i in range(4):
        t = list(last4)
        if i >= 1:
            t[i-1], t[i] = t[i], t[i-1]
        if ''.join(t).count('AGC') >= 1:
            return False
    return True
for i in range(N+1):
    if i == 0:
        memo[0]["****"] = 1
    elif i == 1:
        memo[1]["***A"] = 1
        memo[1]["***C"] = 1
        memo[1]["***G"] = 1
        memo[1]["***T"] = 1
    else:
        #print("aa")
        for c in 'ACGT':
            for j in memo[i-1]:
                #print(j)
                if ok(j[1:] + c):
                    if j[1:] + c not in memo[i]:               
                        memo[i][j[1:] + c] = memo[i-1][j] % MOD
                    else:
                        memo[i][j[1:] + c] += memo[i-1][j] % MOD

s = 0
for i in memo[N].values():
    s += i
print(s)

Fixed code:
N, MOD = int(input()), 10 ** 9 + 7
memo = [{} for i in range(N+1)]
def ok(last4):
    for i in range(4):
        t = list(last4)
        if i >= 1:
            t[i-1], t[i] = t[i], t[i-1]
        if ''.join(t).count('AGC') >= 1:
            return False
    return True
for i in range(N+1):
    if i == 0:
        memo[0]["****"] = 1
    elif i == 1:
        memo[1]["***A"] = 1
        memo[1]["***C"] = 1
        memo[1]["***G"] = 1
        memo[1]["***T"] = 1
    else:
        #print("aa")
        for c in 'ACGT':
            for j in memo[i-1]:
                #print(j)
                if ok(j[1:] + c):
                    if j[1:] + c not in memo[i]:               
                        memo[i][j[1:] + c] = memo[i-1][j] % MOD
                    else:
                        memo[i][j[1:] + c] += memo[i-1][j] % MOD

s = 0
for i in memo[N].values():
    s += i % MOD
print(s % MOD)
Buggy code:
N = int(input())
MOD = 10**9+7
memo = [{} for _ in range(N+1)]

def ok(last4):
    for i in range(4):
        t = list(last4)
        if i >= 1:
            t[i-1],t[i] = t[i],t[i-1]
            if ''.join(t).count('AGC') >= 1:
                return False
        return True

def dfs(cur,last3):
    if last3 in memo[cur]:
        return memo[cur][last3]
    if cur == N:
        return 1
    ret = 0
    for c in 'AGCT':
        if ok(last3+c):
            ret = (ret + dfs(cur+1,last3[1:]+c)) % MOD
    memo[cur][last3] = ret
    return ret

print(dfs(0,'TTT'))

Fixed code:
N = int(input())
MOD = 10 ** 9 + 7
memo = [{} for i in range(N + 1)]

def ok(last4):
	for i in range(4):
		t = list(last4)
		if i >= 1:
			tmp = t[i]
			t[i] = t[i - 1]
			t[i - 1] = tmp
		if ''.join(t).count('AGC') >= 1:
			return False
	return True
			
def AGC(n, last3):
	if last3 in memo[n]:
		return memo[n][last3]
	if n == N:
		return 1
	ret = 0
	for c in 'AGCT':
		if ok(last3 + c):
			ret = (ret + AGC(n + 1, last3[1:] + c)) % MOD
		memo[n][last3] = ret
	return ret
	
print(AGC(0, 'TTT'))
Buggy code:
n = int(input())
mod = 10 ** 9 + 7

memo = [{} for i in range(N+1)]

def ok(last4):
  for i in range(4):
    t = list(last4)
    if i >= 1:
      t[i-1],t[i] = t[i], t[i-1]
    if ''.join(t).count('AGC') >= 1:
      return False
  return True

def dfs(cur, last3):
  if last3 in memo[cur]:
    return memo[cur][last3]
  if cur == n:
    return 1
  
  ret = 0
  for c in 'ACGT':
    if ok(last3 + c):
      ret = (ret + dfs(cur + 1, last3[1:]+c)) % mod
  
  memo[cur][last3] = ret
  return ret

print(dfs(0,'TTT'))


     

Fixed code:
n = int(input())
mod = 10 ** 9 + 7
memo = [{} for i in range(n + 1)]

def check(last4):
    for i in range(4):
        t = list(last4)
        if i >= 1:
            t[i - 1], t[i] = t[i], t[i - 1]
        if ''.join(t).count('AGC') >= 1:
            return 0
    return 1

def dfs(cur, last3):
    if last3 in memo[cur]:
        return memo[cur][last3]
    if cur == n:
        return 1
    ret = 0
    for c in 'ACGT':
        if check(last3 + c):
            ret = (ret + dfs(cur + 1, last3[1:] + c)) % mod
    memo[cur][last3] = ret
    return ret

print(dfs(0, 'TTT'))
Buggy code:
N = int(input())
mod = 10**9+7

def check(s):
    for i in range(4):
        l=list(s)
        if i>0:
            l[i-1],l[i]=l[i],l[i-1]
        if "".join(l).count("AGC"):
            return False
    else:
        return True

memo=[dict() for _ in range(N)]
def dfp(i,seq):
    if i==N:
        return 1
    if seq in memo[i]:
        return memo[i][seq]
    ret=0
    for s in ["A","G","C","T"]:
        if check(seq+s):
            ret=(ret+dfp(i+1,seq[1:]+s))%mod
    memo[i][seq] = ret
    return ret

dfp(0,"TTT")

Fixed code:
N = int(input())
mod = 10**9+7

def check(s):
    for i in range(4):
        l=list(s)
        if i>0:
            l[i-1],l[i]=l[i],l[i-1]
        if "".join(l).count("AGC"):
            return False
    else:
        return True

memo=[dict() for _ in range(N)]
def dfp(i,seq):
    if i==N:
        return 1
    if seq in memo[i]:
        return memo[i][seq]
    ret=0
    for s in ["A","G","C","T"]:
        if check(seq+s):
            ret=(ret+dfp(i+1,seq[1:]+s))%mod
    memo[i][seq] = ret
    return ret

print(dfp(0,"TTT"))
Buggy code:
ngs4 = ["ATGC", "AGTC","ACGC", "AGCC","AGGC", "AGGC"]
ngs3 = ["AGC", "ACG", "GAC"]
def is_valid(b):
    s = b[-4:]
    if s in ngs4:
        return False
    s = s[-3:]
    if s in ngs3:
        return False
    return True

N = int(input())
memo = [{} for i in range(N+1)]
acgt = ["A", "C", "G", "T"]
MOD = 10**9 + 7
def dp(i, last3):
    if last3 in memo[i]:
        return memo[i][last3]

    if i == N:
        return 1
    
    ret = 0
    for c in acgt:
        candi = last3 + c
        if is_valid(candi):
            ret = (ret + dp(i+1, last3[1:] +c)) % MOD
    memo[i][last3] = ret
    return ret

def sub():
    print(dp(0, "TTT"))


Fixed code:
ngs4 = ["ATGC", "AGTC","ACGC", "AGCC","AGGC", "AGGC"]
ngs3 = ["AGC", "ACG", "GAC"]
def is_valid(b):
    s = b[-4:]
    if s in ngs4:
        return False
    s = s[-3:]
    if s in ngs3:
        return False
    return True

N = int(input())
memo = [{} for i in range(N+1)]
acgt = ["A", "C", "G", "T"]
MOD = 10**9 + 7
def dp(i, last3):
    if last3 in memo[i]:
        return memo[i][last3]

    if i == N:
        return 1
    
    ret = 0
    for c in acgt:
        candi = last3 + c
        if is_valid(candi):
            ret = (ret + dp(i+1, last3[1:] +c)) % MOD
    memo[i][last3] = ret
    return ret

def sub():
    print(dp(0, "TTT"))

sub()
Buggy code:
import itertools

s = 'ACGT'
s = [a+b+c for a,b,c in itertools.product(s,s,s)]
n = {s_:1 for s_ in s}
n['AGC'] = 0
n['GAC'] = 0
n['ACG'] = 0

N = int(input())

for i in range(N-3):
    count = {s_:0 for s_ in s}
    for _ in s:
        for a in 'ACGT':
            x = _+a
            if 'GAC' in x or 'ACG' in x or ('A' == x[0] and 'GC' == x[2:]) or ('AG' == x[:2] and 'C' == x[3]):
                pass
            else:
                count[x[1:]] += n[_]
    count['AGC'] = 0
    count['GAC'] = 0
    count['ACG'] = 0
    n = count
print(sum([value for value in count.values()])%(10**9+7))

Fixed code:
import itertools

s = 'ACGT'
s = [a+b+c for a,b,c in itertools.product(s,s,s)]
n = {s_:1 for s_ in s}
n['AGC'] = 0
n['GAC'] = 0
n['ACG'] = 0

N = int(input())
if N == 3:
    count = n
else:
    for i in range(N-3):
        count = {s_:0 for s_ in s}
        for _ in s:
            for a in 'ACGT':
                x = _+a
                if 'GAC' in x or 'ACG' in x or ('A' == x[0] and 'GC' == x[2:]) or ('AG' == x[:2] and 'C' == x[3]):
                    pass
                else:
                    count[x[1:]] += n[_]
        count['AGC'] = 0
        count['GAC'] = 0
        count['ACG'] = 0
        n = count
print(sum([value for value in count.values()])%(10**9+7))
Buggy code:
from collections import defaultdict

N = int(input())
MOD = 10**9+7

ng = set()
for t in "AGCT":
    for n in ["?AGC", "AGC?", "A?GC", "AG?C", "GAC?", "?GAC", "ACG?", "?ACG"]:
        ng.add(n.replace('?', t))

dp = {"???": 1}
for i in range(1, N+1):
    ans = 0
    _dp = defaultdict(int)
    for k, v in dp.items():
        for t in "AGCT":
            s = k+t
            if s not in ng:
                _dp[s[1:]] += v
                ans = (ans+v) % MOD
    dp = _dp
    if i == N:
        print(ans)


Fixed code:
from collections import defaultdict

N = int(input())
MOD = 10**9+7

nglist = [
    "?AGC", "AGC?",
    "A?GC", "AG?C",
    "GAC?", "?GAC",
    "ACG?", "?ACG"
]
ng = {}
for t in "AGCT":
    for n in nglist:
        ng[n.replace('?', t)] = 1

dp = {"TTT": 1}

for i in range(N):
    ans = 0
    _dp = defaultdict(int)
    for k, v in dp.items():
        for t in "AGCT":
            s = k+t
            if s not in ng:
                _dp[s[1:]] += v
                ans = (ans+v) % MOD
    dp = _dp
    if i == (N-1):
        print(ans)
Buggy code:
#!/usr/bin/env python3
import sys
import itertools

MOD = 1000000007  # type: int


def ok(s: str):
    ng = 'AGC'
    c = s.count(ng)
    for i in range(1, 4):
        s2 = s[:i-1] + s[i] + s[i-1] + s[i+1:]
        c += s2.count(ng)
    return c == 0
    

def solve(N: int):
    K = [''.join(t) for t in itertools.product('ACGT', repeat=3)]
    C = [{k: 0 for k in K} for i in range(N+2)] # C[-1 ~ N]
    C[0]['TTT'] = 1
    
    for i in range(1, N+1):
        for last3 in C[i-1].keys():
            for c in 'ACGT':
                if ok(last3 + c):
                    C[i][last3[1:] + c] = (C[i][last3[1:] + c] + C[i-1][last3]) % MOD
    print(sum([C[N][k] for k in K]))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()


Fixed code:
#!/usr/bin/env python3
import sys
import itertools

MOD = 1000000007  # type: int


def ok(s: str):
    ng = 'AGC'
    c = s.count(ng)
    for i in range(1, 4):
        s2 = s[:i-1] + s[i] + s[i-1] + s[i+1:]
        c += s2.count(ng)
    return c == 0
    

def solve(N: int):
    K = [''.join(t) for t in itertools.product('ACGT', repeat=3)]
    C = [{k: 0 for k in K} for i in range(N+2)] # C[-1 ~ N]
    C[0]['TTT'] = 1
    
    for i in range(1, N+1):
        for last3 in C[i-1].keys():
            for c in 'ACGT':
                if ok(last3 + c):
                    C[i][last3[1:] + c] = (C[i][last3[1:] + c] + C[i-1][last3]) % MOD
    print(sum([C[N][k] for k in K]) % MOD)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()
Buggy code:
N = int(input())
memo = [{} for _ in range(N + 1)]

def ok(last4):
  for i in range(4):
    s = list(last4)
    if i >= 1:
      s[i - 1]. s[i] = s[i], s[i - 1]
    if "".join(s).count("AGC") >= 1:
      return False
    return True

def dfs(last3, n):
  if last3 in memo[n]:
    return memo[n][last3]
  if n == N:
    return 1
  ret = 0
  for c in "AGCT":
    if ok(last3 + c):
      ret += dfs(last3[1:] + c, n + 1)
  memo[n][last3] = ret
  return ret

print(dfs("TTT", 0) % (10 ** 9 + 7))

Fixed code:
N = int(input())
MOD = 10**9+7
memo = [{} for _ in range(N+1)]

def ok(last4):
    for i in range(4):
        t = list(last4)
        if i >= 1:
            t[i-1],t[i] = t[i],t[i-1]
        if ''.join(t).count('AGC') >= 1:
            return False
    return True

def dfs(cur,last3):
    if last3 in memo[cur]:
        return memo[cur][last3]
    if cur == N:
        return 1
    ret = 0
    for c in 'AGCT':
        if ok(last3+c):
            ret = (ret + dfs(cur+1,last3[1:]+c)) % MOD
    memo[cur][last3] = ret
    return ret

print(dfs(0,'TTT'))
Buggy code:
N = int(input())
ng = ["agc", "aagc", "acgc", "aggc", "atgc", "agac", "agcc", "aggc", "agtc", "acg", "gac"]
x = {"a": 1, "g": 1, "c": 1, "t": 1}

for _ in range(1,N):
    y = dict()
    for let,num in x.items():
        for a in ["a", "g", "c", "t"]:
            if let + a in ng or (let + a)[-3:] in ng:
                continue
            else:
                if let[-3:] + a in y.keys():
                    y[let[-3:] + a] += num
                else:
                    y[let[-3:]+a] = num
    x = y

print(sum(x.values()) % (10**9+7))

Fixed code:
N = int(input())
ng = ["agc", "aagc", "acgc", "aggc", "atgc", "agac", "agcc", "aggc", "agtc", "acg", "gac"]

x = {"a": 1, "g": 1, "c": 1, "t": 1}

for _ in range(1,N):
    y = dict()
    for let,num in x.items():
        for a in ["a", "c", "g", "t"]:
            if let[-3:] + a in ng or (let[-3:] + a)[-3:] in ng:
                continue
            else:
                if let[-3:] + a in y.keys():
                    y[let[-3:] + a] += num
                else:
                    y[let[-3:]+a] = num
    x = y

print(sum(x.values()) % (10**9+7))
Buggy code:
N=int(input())
MOD=10**9+7

dp={b:1 for b in "ACGT"}

for _ in range(N-1):
  dp_new={}
  for k in dp:
    for b in "ACGT":
      new=k[-3:]+b
      if new in ("AGC","GAC","ACG","AGGC","ATGC","ACGC","AGAC","AGTC","AGGC"):
        continue
      else:
        dp_new.setdefault(new,0)
        dp_new[new]+=dp[k]
  dp=dp_new

print(sum(dp.values())%MOD)

Fixed code:
N=int(input())
MOD=10**9+7

dp={b:1 for b in "ACGT"}

for _ in range(N-1):
  dp_new={}
  for k in dp:
    for b in "ACGT":
      new=k[-3:]+b
      if new[-3:] in ("AGC","GAC","ACG") or new in ("AGGC","ATGC","ACGC","AGTC","AGGC"):
        continue
      else:
        dp_new.setdefault(new,0)
        dp_new[new]+=dp[k]
  dp=dp_new

print(sum(dp.values())%MOD)
Buggy code:
# -*- coding: utf-8 -*-

N = int(input())

dp = {b: 1 for b in "ACGT"}
for _ in range(N - 1):
    dp_new = {}
    for k in dp:
        for b in "ACGT"
           if k[-2:] + b in ("AGC", "ACG", "GAC") or k[-3:4] + b in ("ACGC", "AGGC", "ATGC", "AGCC", "AGGC", "AGTC"):
                continue
            else:
                dp_new[k[-3:] + b] += dp[k]
    dp = dp_new

mod = 10 ** 9 + 7
print(sum(dp.values()) % mod)


Fixed code:
# -*- coding: utf-8 -*-

N = int(input())

dp = {b: 1 for b in "ACGT"}
for _ in range(N - 1):
    dp_new = {}
    for k in dp:
        for b in "ACGT":
            n = k[-3:] + b
            if n[-3:] in ("AGC", "ACG", "GAC") or n in ("ACGC", "AGGC", "ATGC", "AGCC", "AGGC", "AGTC"):
                continue
            else:
                if n in dp_new:
                    dp_new[n] += dp[k]
                else:
                    dp_new[n] = dp[k]
    dp = dp_new

print(sum(dp.values()) % (10 ** 9 + 7))
Buggy code:
n=int(input())
m=[{}for i in range(n+1)]
w=10**9+7
def f(s):
    for i in range(4):
        t=list(s)
        if i>=1:
            t[i-1],t[i]=t[i],t[i-1]
        if "".join(t).count("AGC")>0:
            return False
    return True
def a(p,q):
    if q in m[p]:
        return m[p][q]
    if p==n:
        return 1
    r=0
    for i in "AGCT":
        if f(q+i):
            r=(r+a(p+1,q[1:]+i))%w
    m[p][q]=r
    return r
print(a(0,"WHY"))

print(m)

Fixed code:
n=int(input())
m=[{}for i in range(n+1)]
w=10**9+7
def f(s):
    for i in range(4):
        t=list(s)
        if i>=1:
            t[i-1],t[i]=t[i],t[i-1]
        if "".join(t).count("AGC")>0:
            return False
    return True
def a(p,q):
    if q in m[p]:
        return m[p][q]
    if p==n:
        return 1
    r=0
    for i in "AGCT":
        if f(q+i):
            r=(r+a(p+1,q[1:]+i))%w
    m[p][q]=r
    return r
print(a(0,"WHY"))
Buggy code:
n = int(input())
memo = [{} for i in range(N+1)]
m = 10**9+7

def ppp(l):
    for i in range(4):
        t = list(l)
        if i >= 1:
            t[i-1],t[i] = t[i],t[i-1]
        if "".join(t).count("AGC") >= 1:
            return False
        return True
def dfs(c,l):
    if l in memo[c]:
        return memo[c][l]
    if c == n:
        return 1
    ret = 0
    for b in "AGCT":
        if ppp(l+b):
            ret = (ret + dfs(c + 1,l[1:] + c)) % m
    memo[c][l] = ret
    return ret
print(dfs(0,"TTT"))

Fixed code:
n = int(input())
memo = [{} for i in range(n+1)]
m = 10**9+7

def ppp(l):
    for i in range(4):
        t = list(l)
        if i >= 1:
            t[i-1],t[i] = t[i],t[i-1]
        if "".join(t).count("AGC") >= 1:
            return False
    return True
def dfs(c,l):
    if l in memo[c]:
        return memo[c][l]
    if c == n:
        return 1
    ret = 0
    for b in "AGCT":
        if ppp(l+b):
            ret = (ret + dfs(c + 1,l[1:] + b)) % m
    memo[c][l] = ret
    return ret
print(dfs(0,"TTT"))
Buggy code:
n = int(input())
mem = [{} for i in range(n + 1)]

first3s = []
for c in 'ACGT':
	for c2 in 'ACGT':
		for c3 in 'ACGT':
			first3s.append(c + c2 + c3)

ans = 0
for three in first3s:
  mem[3][three] = 0 if three in ['AGC', 'ACG', 'GAC'] else 1
  ans += mem[3][three]

def check4(four):
  if 'AGC' in four:
    return False
  for i in range(3):
    if 'AGC' in four[:i] + four[i+1] + four[i] + four[i+2:]:
      return False
  return True

if n > 3:
  ans = 0
  for i in range(4, n + 1):
    for three in first3s:
      for c in 'ACGT':
        four = c + three
        if check4(four):
          ans = (ans + mem[i - 1][three]) % (10**9 + 7)
          mem[i][(c + three)[:3]] = (mem[i].get((c + three)[:3], 0) + mem[i - 1][three]) % (10**9 + 7)
print(ans)

Fixed code:
n = int(input())
mem = [{} for i in range(n + 1)]

first3s = []
for c in 'ACGT':
	for c2 in 'ACGT':
		for c3 in 'ACGT':
			first3s.append(c + c2 + c3)

ans = 0
for three in first3s:
  mem[3][three] = 0 if three in ['AGC', 'ACG', 'GAC'] else 1
  ans += mem[3][three]

def check4(four):
  if 'AGC' in four:
    return False
  for i in range(3):
    if 'AGC' in four[:i] + four[i+1] + four[i] + four[i+2:]:
      return False
  return True

if n > 3:
  for i in range(4, n + 1):
    ans = 0
    for three in first3s:
      for c in 'ACGT':
        four = c + three
        if check4(four):
          ans = (ans + mem[i - 1][three]) % (10**9 + 7)
          mem[i][(c + three)[:3]] = (mem[i].get((c + three)[:3], 0) + mem[i - 1][three]) % (10**9 + 7)
print(ans)
Buggy code:
N = int(input())
MOD = 10**9 + 7

dp = [[[[0] * 4 for ___ in range(4)]for __ in range(4)]for _ in range(N + 1)]
for i in range(4):
    for j in range(4):
        for k in range(4):
            dp[0][i][j][k] = 0
            dp[1][i][j][k] = 1
            dp[2][i][j][k] = 1
            if i == 2 and j == 1 and k == 0:
                continue
            elif i == 1 and j == 0 and k == 2:
                continue
            elif i == 2 and j == 0 and k == 1:
                continue
            dp[3][i][j][k] = 1


for n in range(3, N):
    for i in range(4):
        for j in range(4):
            for k in range(4):
                for c in range(4):
                    if c == 1 and i == 2 and j == 0:
                        continue
                    elif c == 2 and i == 1 and j == 0:
                        continue
                    elif c == 2 and i == 0 and j == 1:
                        continue
                    elif c == 2 and i == 1 and k == 0:
                        continue
                    elif c == 2 and j == 1 and k == 0:
                        continue
                    dp[n + 1][c][i][j] += dp[n][i][j][k]
                    dp[n + 1][c][i][j] %= MOD

ans = 0
for i in range(4):
    for j in range(4):
        for k in range(4):
            ans += dp[N][i][j][k]
print(ans % MOD)


Fixed code:
N = int(input())
MOD = 10**9 + 7

dp = [[[[0] * 4 for ___ in range(4)]for __ in range(4)]for _ in range(N + 1)]
for i in range(4):
    for j in range(4):
        for k in range(4):
            dp[0][i][j][k] = 0
            dp[1][i][j][k] = 1
            dp[2][i][j][k] = 1
            if i == 2 and j == 1 and k == 0:
                continue
            elif i == 1 and j == 2 and k == 0:
                continue
            elif i == 2 and j == 0 and k == 1:
                continue
            dp[3][i][j][k] = 1


for n in range(3, N):
    for i in range(4):
        for j in range(4):
            for k in range(4):
                for c in range(4):
                    if c == 1 and i == 2 and j == 0:
                        continue
                    elif c == 2 and i == 1 and j == 0:
                        continue
                    elif c == 2 and i == 0 and j == 1:
                        continue
                    elif c == 2 and i == 1 and k == 0:
                        continue
                    elif c == 2 and j == 1 and k == 0:
                        continue
                    dp[n + 1][c][i][j] += dp[n][i][j][k]
                    dp[n + 1][c][i][j] %= MOD

ans = 0
for i in range(4):
    for j in range(4):
        for k in range(4):
            ans += dp[N][i][j][k]
print(ans % MOD)