qyliang/CodeNanoFix huggingface dataset:
Buggy code:
MOD = 10 ** 9 + 7
N = int(input())
dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N + 1)]
dp[0][3][3][3] = 1
for len in range(N):
    for i in range(4):
        for j in range(4):
            for k in range(4):
                if dp[len][i][j][k] == 0: continue

                for l in range(4):
                    if l == 2 and i == 1 and j == 0: continue
                    if l == 2 and i == 0 and j == 1: continue
                    if l == 1 and i == 2 and j == 0: continue
                    if l == 2 and i == 1 and k == 0: continue
                    if l == 2 and j == 1 and k == 0: continue
                    dp[len + 1][l][i][j] += dp[len][i][j][k]
                    dp[len + 1][l][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)

Fixed code:
MOD = 10 ** 9 + 7
N = int(input())
dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N + 1)]
dp[0][3][3][3] = 1
for len in range(N):
    for i in range(4):
        for j in range(4):
            for k in range(4):
                if dp[len][i][j][k] == 0: continue

                for l in range(4):
                    if l == 2 and i == 1 and j == 0: continue
                    if l == 2 and i == 0 and j == 1: continue
                    if l == 1 and i == 2 and j == 0: continue
                    if l == 2 and i == 1 and k == 0: continue
                    if l == 2 and j == 1 and k == 0: continue
                    dp[len + 1][l][i][j] += dp[len][i][j][k]
                    dp[len + 1][l][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]
            ans %= MOD
print(ans)
Buggy code:
import sys
input = sys.stdin.readline

n = int(input())

dp = [[[[0]*4 for _ in range(4)] for _ in range(4)] for _ in range(n + 1)]
c = [0, 1, 2, 3]
import itertools
pers = itertools.product(c, repeat=3)
for p in pers:
    if p in [(0, 1, 2), (0, 2, 1), (1, 0, 2)]:
        continue
    dp[3][p[0]][p[1]][p[2]] = 1
for i in range(4, n + 1):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                if (j, k, l) in [(0, 1, 2), (0, 2, 1), (1, 0, 2)]:
                    continue
                elif (j == 0 and l == 1) or (j == 0 and k == 1):
                    dp[i][j][k][l] = sum([dp[i - 1][k][l][x] for x in range(4) if x != 2])
                else:
                    dp[i][j][k][l] = sum([dp[i - 1][k][l][x] for x in range(4)])
ans = 0
for j in range(4):
    for k in range(4):
        for l in range(4):
            ans += dp[-1][j][k][l]
print(ans)

Fixed code:
import sys
input = sys.stdin.readline

n = int(input())

dp = [[[[0]*4 for _ in range(4)] for _ in range(4)] for _ in range(n + 1)]
c = [0, 1, 2, 3]
import itertools
pers = itertools.product(c, repeat=3)
for p in pers:
    if p in [(0, 1, 2), (0, 2, 1), (1, 0, 2)]:
        continue
    dp[3][p[0]][p[1]][p[2]] = 1
for i in range(4, n + 1):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                if (j, k, l) in [(0, 1, 2), (0, 2, 1), (1, 0, 2)]:
                    continue
                elif (j == 0 and l == 1) or (j == 0 and k == 1):
                    dp[i][j][k][l] = sum([dp[i - 1][k][l][x] for x in range(4) if x != 2])
                else:
                    dp[i][j][k][l] = sum([dp[i - 1][k][l][x] for x in range(4)])
ans = 0
for j in range(4):
    for k in range(4):
        for l in range(4):
            ans += dp[-1][j][k][l]
print(ans % (10**9 + 7))
Buggy code:
n = int(input())
mod = 10**9 + 7
acgt = ["A", "C", "G", "T"]
dp = {acgt[i]+acgt[j]+acgt[k]: 1 for i in range(4) for j in range(4) for k in range(4)}
dp["AGC"] = 0
dp["ACG"] = 0
dp["GAC"] = 0

for i in range(n-3):
    next_dp = {key: 0 for key in dp.keys()}
    for key, val in dp.items():
        for item in acgt:
            next_key = key + item
            if next_key[1:] == "AGC" or next_key[1:] == "ACG" or next_key[1:] == "GAC":
                continue
            elif next_key[0] + next_key[2:] == "AGC" or next_key[:1] + next_key[3] == "AGC":
                continue
            else:
                dp[next_key[1:]] += dp[key]
    dp = next_dp
    for key in dp.keys():
        dp[key] %= mod
print(sum(dp.values())%mod)

Fixed code:
n = int(input())
mod = 10**9 + 7
acgt = ["A", "C", "G", "T"]
dp = {acgt[i]+acgt[j]+acgt[k]: 1 for i in range(4) for j in range(4) for k in range(4)}
dp["AGC"] = 0
dp["ACG"] = 0
dp["GAC"] = 0

for i in range(n-3):
    next_dp = {key: 0 for key in dp.keys()}
    for key, val in dp.items():
        for item in acgt:
            next_key = key + item
            if next_key[1:] == "AGC" or next_key[1:] == "ACG" or next_key[1:] == "GAC":
                continue
            elif next_key[0] + next_key[2:] == "AGC" or next_key[:2] + next_key[3] == "AGC":
                continue
            else:
                next_dp[next_key[1:]] = (next_dp[next_key[1:]] + val) % mod
    dp = next_dp
print(sum(dp.values()) % mod)
Buggy code:
n = int(input())
# {0:"A", 1:"C", 2:"G", 3:"T"}
dp = [[[[0] * 4 for _ in range(4)] for _ in range(4)] for _ in range(n + 1)]
dp[0][3][3][3] = 1
mod = 10 ** 9 + 7

for i in range(n):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    if m == 1 and l == 0 and k == 2:
                        continue
                    if m == 2 and l == 1 and k == 0:
                        continue
                    if m == 1 and l == 2 and k == 0:
                        continue
                    if m == 2 and k == 1 and j == 0:
                        continue
                    if m == 1 and l == 2 and k == 0:
                        continue
                    
                    dp[i+1][k][l][m] += dp[i][j][k][l]
                    dp[i+1][k][l][m] %= mod
                    

ans = 0
for j in range(4):
    for k in range(4):
        for l in range(4):
            ans += dp[n][j][k][l]
            ans %= mod
print(ans)

Fixed code:
n = int(input())
# {0:"A", 1:"C", 2:"G", 3:"T"}
dp = [[[[0] * 4 for _ in range(4)] for _ in range(4)] for _ in range(n + 1)]
dp[0][3][3][3] = 1
mod = 10 ** 9 + 7

for i in range(n):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    if m == 1 and l == 0 and k == 2: continue
                    if m == 2 and l == 1 and k == 0: continue
                    if m == 1 and l == 2 and k == 0: continue
                    if m == 1 and k == 2 and j == 0: continue
                    if m == 1 and l == 2 and j == 0: continue
                    
                    dp[i+1][k][l][m] += dp[i][j][k][l]
                    
ans = 0
for j in range(4):
    for k in range(4):
        for l in range(4):
            ans += dp[n][j][k][l]

print(ans % mod)
Buggy code:
import sys
input = lambda: sys.stdin.readline().rstrip()

N = int(input())

dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N+1)]

dp[0][3][3][3] = 1

MOD = 10 ** 9 + 7
for l in range(N):
    for i in range(4):
        for j in range(4):
            for k in range(4):
                if dp[l][i][j][k] == 0:
                    continue
                
                for n in range(4):
                    if j == 0 and k == 2 and n == 1: continue
                    if j == 0 and k == 1 and n == 2: continue
                    if j == 1 and k == 0 and n == 2: continue

                    if i == 0 and j == 1 and n == 2: continue
                    if i == 0 and k == 1 and n == 2: continue 

                    dp[l+1][j][k][n] += dp[l][i][j][k]
                    dp[l+1][j][k][n] %= 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)

Fixed code:
import sys
input = lambda: sys.stdin.readline().rstrip()

N = int(input())

dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N+1)]

dp[0][3][3][3] = 1

MOD = 10 ** 9 + 7
for l in range(N):
    for i in range(4):
        for j in range(4):
            for k in range(4):
                if dp[l][i][j][k] == 0:
                    continue
                
                for n in range(4):
                    if j == 0 and k == 2 and n == 1: continue
                    if j == 0 and k == 1 and n == 2: continue
                    if j == 1 and k == 0 and n == 2: continue

                    if i == 0 and j == 1 and n == 2: continue
                    if i == 0 and k == 1 and n == 2: continue 

                    dp[l+1][j][k][n] += dp[l][i][j][k]
                    dp[l+1][j][k][n] %= MOD
ans = 0
for i in range(4):
    for j in range(4):
        for k in range(4):
            ans += dp[N][i][j][k]
            ans %= MOD
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

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

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:
            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 i in 'ACGT':
        if ok(last3 + i):
            ret = (ret + dfs(cur+1,last3[1:]+i)) % mod
    memo[cur][last3] = ret
    return ret
print(dfs(0,'   '))
Buggy code:
n = int(input())
dp = [[0] * 4 for i in range(n + 2)]
for i in range(4):
    dp[2][i] = 1
mod = 10**9 + 7
for i in range(3, n + 2):
    for j in range(4):
        for k in range(4):
            dp[i][j] += dp[i - 1][k]
    dp[i][2] -= dp[i - 2][0]
    dp[i][2] -= dp[i - 2][1]
    dp[i][1] -= dp[i - 2][0] + dp[i - 3][1]
    dp[i][2] -= 3 * dp[i - 3][0]
print(sum(dp[-1]) % mod)


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

dp[0][0] = dp[0][1] = dp[0][2] = dp[0][3] = 1
dp[1][0] = dp[1][1] = dp[1][2] = dp[1][3] = 4

for i in range(2,n):
    for j in range(4):
        dp[i][j] = sum(dp[i-1])%mod
    dp[i][1] -= dp[i-2][0]
    dp[i][2] -= (dp[i-2][0] + dp[i-2][1])
    if i >= 3:
        dp[i][2] -= 3*dp[i-3][0]
        dp[i][1] += dp[i-3][1]
        

print(sum(dp[n-1])%mod)
Buggy code:
n=int(input())
mod=10**9+7
if n==3:
    print(61)
    exit()
zt=['A','G','C','T']
for i in range(3):
    tmp=[]
    for j in zt:
        for k in list('AGCT'):
            tmp.append(j+k)
    zt=tmp
ng=set([])
for i in list('AGCT'):
    ng.add('AGC'+i)
    ng.add('AG'+i+'C')
    ng.add('A'+i+'GC')
    ng.add(i+'AGC')
    ng.add('ACG'+i)
    ng.add('GAC'+i)
    ng.add(i+'ACG')
    ng.add(i+'GAC')
z=set([])
for j in zt:
    if not j in ng:
        z.add(j)
zn=len(z)
dp=[[0 for j in range(zn)] for i in range(n-3)]
for i in range(zn):
   # if not z[i] in ng:
    dp[0][i]=1
for i in range(n-4):
    for j in range(zn):
        for k in range(zn):
            #if not z[k] in ng:
            if z[j][1:]==z[k][:3]:
                dp[i+1][k]+=dp[i][j]
                dp[i+1][k]%=mod
print(sum(dp[-1])%mod)


Fixed code:
n=int(input())
mod=10**9+7
if n==3:
    print(61)
    exit()
zt=['A','G','C','T']
for i in range(3):
    tmp=[]
    for j in zt:
        for k in list('AGCT'):
            tmp.append(j+k)
    zt=tmp
ng=set([])
for i in list('AGCT'):
    ng.add('AGC'+i)
    ng.add('AG'+i+'C')
    ng.add('A'+i+'GC')
    ng.add(i+'AGC')
    ng.add('ACG'+i)
    ng.add('GAC'+i)
    ng.add(i+'ACG')
    ng.add(i+'GAC')
z=[]
for j in zt:
    if not j in ng:
        z.append(j)
zn=len(z)
dp=[[0 for j in range(zn)] for i in range(n-3)]
for i in range(zn):
   # if not z[i] in ng:
    dp[0][i]=1
for i in range(n-4):
    for j in range(zn):
        for k in range(zn):
            #if not z[k] in ng:
            if z[j][1:]==z[k][:3]:
                dp[i+1][k]+=dp[i][j]
                dp[i+1][k]%=mod
print(sum(dp[-1])%mod)
Buggy code:
mod = 1000000007
n = int(input())
end_a = [0,0,0,16]
end_g = [0,0,0,15]
end_ag = [0,0,0,4]
end_ac = [0,0,0,3]
end_ga = [0,0,0,4]
end_aga = [0,0,0,1]
end_agt = [0,0,0,1]
end_agg = [0,0,0,1]
ans = [0,0,0,61]
for i in range(3, n+1):
    end_a.append(ans[i]%mod)
    end_g.append((ans[i] - end_ac[i])%mod)
    end_ag.append(end_a[i]%mod)
    end_ac.append((end_a[i] - end_ga[i] - end_aga[i])%mod)
    end_ga.append(end_g[i]%mod)
    end_aga.append(end_ag[i]%mod)
    end_agt.append(end_ag[i]%mod)
    end_agg.append(end_ag[i]%mod)
    taboo = (end_ag[i] + end_ac[i] + end_ga[i] + end_aga[i] + end_agt[i] + end_agg[i])%mod
    ans.append((taboo*3 + (ans[i]-taboo)*4)%mod)
print(ans[n]%mod)

Fixed code:
mod = 1000000007
n = int(input())
end_a = [0,0,0,16]
end_g = [0,0,0,15]
end_ag = [0,0,0,4]
end_ac = [0,0,0,3]
end_ga = [0,0,0,4]
end_at = [0,0,0,4]
end_aga = [0,0,0,1]
end_agt = [0,0,0,1]
end_agg = [0,0,0,1]
end_atg = [0,0,0,1]
ans = [0,0,0,61]
for i in range(3, n+1):
    end_a.append(ans[i]%mod)
    end_g.append((ans[i] - end_ac[i])%mod)
    end_ag.append(end_a[i]%mod)
    end_ac.append((end_a[i] - end_ga[i])%mod)
    end_ga.append(end_g[i]%mod)
    end_at.append(end_a[i]%mod)
    end_agt.append(end_ag[i]%mod)
    end_agg.append(end_ag[i]%mod)
    end_atg.append(end_at[i]%mod)
    taboo = (end_ag[i] + end_ac[i] + end_ga[i] + end_agt[i] + end_agg[i] + end_atg[i])%mod
    ans.append((taboo*3 + (ans[i]-taboo)*4)%mod)
print(ans[n]%mod)
Buggy code:
# We Like AGC

N = int(input())
dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N+1)]

for c1 in range(4):
	for c2 in range(4):
		for c3 in range(4):
			dp[3][c1][c2][c3] = 1

dp[3][0][1][2] = 0
dp[3][0][2][1] = 0
dp[3][1][0][2] = 0

mod = 1e+9+7
for l in range(4,N+1):
	for c1 in range(4):
		for c2 in range(4):
			for c3 in range(4):
				for a in range(4):
					if a==2 and c2 == 0 and c3 == 1:
						continue
					if a==1 and c2 == 0 and c3 == 2:
						continue
					if a==2 and c2 == 1 and c3 == 0:
						continue
					if a==2 and c1 == 0 and c2 == 1:
						continue
					if a==2 and c1 == 0 and c3 == 1:
						continue
					dp[l][c2][c3][a] += dp[l-1][c1][c2][c3]

cnt = 0
for c1 in range(4):
	for c2 in range(4):
		for c3 in range(4):
			cnt += dp[N][c1][c2][c3]
cnt = int(cnt%mod)
print(cnt)

Fixed code:
# We Like AGC

N = int(input())
MOD = 1e+9+7
dp = [[[[0 for _ in range(4)] for _ in range(4)] for _ in range(4)] for _ in range(N+1)]
for c1 in range(4):
	for c2 in range(4):
		for c3 in range(4):
			dp[3][c1][c2][c3] = 1

dp[3][0][1][2] = 0
dp[3][0][2][1] = 0
dp[3][1][0][2] = 0

for n in range(4, N+1):
	for c1 in range(4):
		for c2 in range(4):
			for c3 in range(4):
				for a in range(4):
				
					if a==2 and c2 == 0 and c3 == 1:
						continue
					if a==2 and c2 == 1 and c3 == 0:
						continue
					if a==2 and c1 == 0 and c3 == 1:
						continue
					if a==2 and c1 == 0 and c2 == 1:
						continue
					if a==1 and c2 == 0 and c3 == 2:
						continue
				
					dp[n][c2][c3][a] += dp[n-1][c1][c2][c3]
					dp[n][c2][c3][a]  = int(dp[n][c2][c3][a]%MOD)
cnt = 0
mod = 1e+9+7
for c1 in range(4):
	for c2 in range(4):
		for c3 in range(4):
			cnt += dp[N][c1][c2][c3]
cnt = int(cnt%mod)
print(cnt)
Buggy code:
MOD = 10**9 + 7
from itertools import product
N = int(input())
# XAGC, XGAC, AXGC, XACG: prohibited
A=0; C=1; G=2; T=3
cur = [[[1]*4 for j in range(4)] for k in range(4)]
for i in range(4):
    cur[A][G][C] = cur[G][A][C] = cur[A][C][G] = 1

for i in range(3,N):
    prev = cur
    cur = [[[0]*4 for j in range(4)] for k in range(4)]
    for i, j, k in product(range(4),repeat=3):
        if (j==A and k==G) or (j==G and k==A) or (i==A and k==G):
            for l in [A,G,T]:
                cur[j][k][l] += prev[i][j][k]
        elif j==A and k==C:
            for l in [A,C,T]:
                cur[j][k][l] += prev[i][j][k]
        else:
            for l in range(4):
                cur[j][k][l] += prev[i][j][k]
    for i, j, k in product(range(4),repeat=3):
        cur[i][j][k] %= MOD

ans = sum(sum(sum(cur[i][j]) for j in range(4)) for i in range(4))
print(ans)

Fixed code:
from itertools import product
MOD = 10**9 + 7
N = int(input())
# XAGC, XGAC, AXGC, AGXC, XACG: prohibited
A=0; C=1; G=2; T=3
cur = [[[1]*4 for j in range(4)] for k in range(4)]
for i in range(4):
    cur[A][G][C] = cur[G][A][C] = cur[A][C][G] = 0

for _ in range(3,N):
    prev = cur
    cur = [[[0]*4 for j in range(4)] for k in range(4)]
    for i, j, k in product(range(4),repeat=3):
        if (j==A and k==G) or (j==G and k==A) or (i==A and k==G) or (i==A and j==G):
            for l in [A,G,T]:
                cur[j][k][l] += prev[i][j][k]
        elif j==A and k==C:
            for l in [A,C,T]:
                cur[j][k][l] += prev[i][j][k]
        else:
            for l in range(4):
                cur[j][k][l] += prev[i][j][k]
    for i, j, k in product(range(4),repeat=3):
        cur[i][j][k] %= MOD

ans = sum(sum(sum(cur[i][j]) for j in range(4)) for i in range(4)) % MOD
print(ans)
Buggy code:
N = int(input())

C = 10**9+7
n = 5
L = [0 for i in range(N+1)]
X = [0 for i in range(N+1)]
Y = [0 for i in range(N+1)]
Z = [0 for i in range(N+1)]
W = [0 for i in range(N+1)]
G = [0 for i in range(N+1)]
L[3] = 61
L[4] = 230
X[3] = 3
X[4] = 12
Y[3] = 4
Y[4] = 16
Z[3] = 4
Z[4] = 15
W[3] = 3
W[4] = 12
W[5] = 48
G[3] = 15
G[4] = 58
while n <= N:
    L[n] = 4*L[n-1] - (X[n-1]+Y[n-1]+Z[n-1]+W[n-1])
    G[n] = L[n-1]-X[n-1]
    X[n] = L[n-2]-G[n-2]
    Y[n] = L[n-2]
    Z[n] = L[n-2]-X[n-2]
    if n > 5:
        W[n] = L[n-3]*3
    n += 1

print(L[N]%C)

Fixed code:
N = int(input())

C = 10**9+7
n = 5
num = max(N+1, 6)
L = [0 for i in range(num)]
X = [0 for i in range(num)]
Y = [0 for i in range(num)]
Z = [0 for i in range(num)]
W = [0 for i in range(num)]
G = [0 for i in range(num)]
L[3] = 61
L[4] = 230
X[3] = 3
X[4] = 12
Y[3] = 4
Y[4] = 16
Z[3] = 4
Z[4] = 15
W[3] = 3
W[4] = 12
W[5] = 48
G[3] = 15
G[4] = 58
while n <= N:
    L[n] = 4*L[n-1] - (X[n-1]+Y[n-1]+Z[n-1]+W[n-1])
    G[n] = L[n-1]-X[n-1]
    X[n] = L[n-2]-G[n-2]
    Y[n] = L[n-2]
    Z[n] = L[n-2]-X[n-2]
    if n > 5:
        W[n] = L[n-3]*3
    n += 1

print(L[N]%C)
Buggy code:
import re

def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

N = int(input())

acgt = ["A", "C", "G", "T"]
changed = ["GAC", "ACG", "A.GC", "AG.C"]
bad = "AGC"


# initialize
dp = {}
for a in acgt:
    for b in acgt:
        for c in acgt:
            now = a+b+c
            
            if re.match(bad, now):
                dp[now] = 0
            elif any(map(lambda ch: re.match(ch, now), changed)):
                dp[now] = 0
            else:
                dp[now] = 1

N -= 3

MOD = int(1e9) + 7
# main
while N > 0:
    dpNow = {}
    for a in acgt:
        for b in acgt:
            for c in acgt:
                abc = (a + b + c)[:min(N, 3)]
                dpNow[abc] = 0
                for key, value in dp.items():
                    now = key + abc

                    if re.match(bad, now):
                        ()
                    elif any(map(lambda ch: re.match(ch, now), changed)):
                        ()
                    else:
                        dpNow[abc] += value
                        dpNow[abc] %= MOD
                        
    dp = dpNow
    N -= 3

ans = 0
for v in dp.values():
    ans += v
    ans %= MOD

print(ans)


Fixed code:
import re

def makelist(n, m):
    return [[0 for i in range(m)] for j in range(n)]

N = int(input())

acgt = ["A", "C", "G", "T"]
changed = [".*GAC.*", ".*ACG.*", ".*A.GC.*", ".*AG.C.*"]
bad = ".*AGC"


# initialize
dp = {}
for a in acgt:
    for b in acgt:
        for c in acgt:
            now = a+b+c
            
            if re.match(bad, now):
                dp[now] = 0
            elif any(map(lambda ch: re.match(ch, now), changed)):
                dp[now] = 0
            else:
                dp[now] = 1

N -= 3

MOD = int(1e9) + 7
# main
while N > 0:
    dpNow = {}
    for a in acgt:
        for b in acgt:
            for c in acgt:
                abc = (a + b + c)[:min(N, 3)]
                dpNow[abc] = 0
                for key, value in dp.items():
                    now = key + abc

                    if re.match(bad, now):
                        ()
                    elif any(map(lambda ch: re.match(ch, now), changed)):
                        ()
                    else:
                        dpNow[abc] += value
                        dpNow[abc] %= MOD
                        
    dp = dpNow
    N -= 3

ans = 0
for v in dp.values():
    ans += v
    ans %= MOD

print(ans)
Buggy code:
import copy

def ok(len_4):    
    for i in range(3):
        alpha = list(copy.deepcopy(len_4))
        alpha[i], alpha[i + 1] = alpha[i + 1], alpha[i]
        if "AGC" in "".join(alpha):
            return False
    if "AGC"in len_4:
        return False
    return True


def check(len_3, n):
    if n == N:
        return 1
    if len_3 in memo[n]:
        return memo[n][len_3]
    ret = 0
    for alpha in "AGCT":
        t = len_3 + alpha
        if ok(t):
            temp = check(t[1:], n + 1)
            memo[n + 1][t[1:]] = temp % mod
            ret += temp % mod
    return ret % mod


mod = (10 ** 9) + 7
N = int(input())
memo = [{} for _ in range(N + 1)]
check("TTT", 0)

Fixed code:
import copy

def ok(len_4):    
    for i in range(3):
        alpha = list(copy.deepcopy(len_4))
        alpha[i], alpha[i + 1] = alpha[i + 1], alpha[i]
        if "AGC" in "".join(alpha):
            return False
    if "AGC"in len_4:
        return False
    return True


def check(len_3, n):
    if n == N:
        return 1
    if len_3 in memo[n]:
        return memo[n][len_3]
    ret = 0
    for alpha in "AGCT":
        t = len_3 + alpha
        if ok(t):
            temp = check(t[1:], n + 1)
            memo[n + 1][t[1:]] = temp % mod
            ret += temp % mod
    return ret % mod


mod = (10 ** 9) + 7
N = int(input())
memo = [{} for _ in range(N + 1)]
ans = check("TTT", 0)
print(ans)
Buggy code:
N = int(input())
mod = 10**9+7
dp = [[0]*4 for _ in range(N)]

dp[0] = [1,1,1,1]
dp[1] = [4,4,4,4]
#AGC, ACG, GAC, A?GC,AG?C -> AGGC, ATGC,AGTC

for i in range(2,N):
  if i == 2:
    dp[i][0] = dp[i-1][0]*4
    dp[i][1] = dp[i-1][1]*4-2
    dp[i][2] = dp[i-1][2]*4-1
    dp[i][3] = dp[i-1][3]*4
  else:
    dp[i][0] = sum(dp[i-1])
    dp[i][1] = sum(dp[i-1])-dp[i-2][0]-dp[i-2][2]-dp[i-3][0]*3
    dp[i][2] = sum(dp[i-1])-dp[i-2][0] #+dp[i-3][2]
    dp[i][3] = sum(dp[i-1])
    
print(sum(dp[N-1])%mod)

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

dp[0] = [1,1,1,1]
dp[1] = [4,4,4,4]
#AGC, ACG, GAC, A?GC,AG?C -> AGGC, ATGC,AGTC

for i in range(2,N):
  if i == 2:
    dp[i][0] = dp[i-1][0]*4
    dp[i][1] = dp[i-1][1]*4-2
    dp[i][2] = dp[i-1][2]*4-1
    dp[i][3] = dp[i-1][3]*4
  else:
    dp[i][0] = sum(dp[i-1])
    dp[i][1] = sum(dp[i-1])-dp[i-2][0]-dp[i-2][2]-dp[i-3][0]*3
    dp[i][2] = sum(dp[i-1])-dp[i-2][0]+dp[i-3][2]
    dp[i][3] = sum(dp[i-1])
    
print(sum(dp[N-1])%mod)