Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
q=[[] for i in range(N+1)]
for i in range(M):
    l,r,x=map(int,input().split())
    q[r].append((l,x))
mod=10**9+7

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

i=N
for j in range(N+1):
    for k in range(N+1):
        check=7
        for l,x in q[i]:
            if x==1:
                if i-1>=l>j:
                    check&=1
                elif i==l:
                    check&=7
                else:
                    check=0
            elif x==2:
                if i==l:
                    check=0
                elif i-1>=l>j:
                    check&=6
                elif j>=l>k:
                    check&=3
                else:
                    check=0
            else:
                if i==l:
                    check=0
                elif i-1>=l>j:
                    check=0
                elif j>=l>k:
                    check&=4
        dp[j][k]=bin(check).count("1")

ndp=[[0 for j in range(N+1)] for k in range(N+1)]
s=[(j,k) for j in range(N+1) for k in range(N+1)]
for i in range(N-1,0,-1):
    for j,k in s:
        check=7
        for l,x in q[i]:
            if i==l:
                check=(x==1)*check
            elif i-1>=l>j:
                check=(x==1)*(check%2)+(x==2)*(check&6)
            elif j>=l>k:
                check=(x==2)*(check&3)+(x==3)*(check&4)
            else:
                check*=(x==3)
            
        ndp[j][k]=(dp[j][k]*((check//1)%2)+dp[i-1][k]*((check//2)%2)+dp[i-1][j]*((check//4)%2))%mod
    dp=ndp

print(dp[0][0])

Fixed submission:
N,M=map(int,input().split())
q=[[] for i in range(N+1)]
for i in range(M):
    l,r,x=map(int,input().split())
    q[r].append((l,x))
mod=10**9+7

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

i=N
for j in range(N+1):
    for k in range(N+1):
        check=7
        for l,x in q[i]:
            if i==l:
                check=(x==1)*check
            elif i-1>=l>j:
                check=(x==1)*(check%2)+(x==2)*(check&6)
            elif j>=l>k:
                check=(x==2)*(check&3)+(x==3)*(check&4)
            else:
                check*=(x==3)
        dp[j][k]=bin(check).count("1")

ndp=[[0 for j in range(N+1)] for k in range(N+1)]
for i in range(N-1,0,-1):
    for j in range(i):
        for k in range(j+1):
            check=7
            for l,x in q[i]:
                if i==l:
                    check=(x==1)*check
                elif i-1>=l>j:
                    check=(x==1)*(check%2)+(x==2)*(check&6)
                elif j>=l>k:
                    check=(x==2)*(check&3)+(x==3)*(check&4)
                else:
                    check*=(x==3)
            ndp[j][k]=(dp[j][k]*((check//1)%2)+dp[i-1][k]*((check//2)%2)+dp[i-1][j]*((check//4)%2))%mod

    dp=ndp

print(dp[0][0])

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
q=[[] for i in range(N+1)]
for i in range(M):
    l,r,x=map(int,input().split())
    q[r].append((l,x))
mod=10**9+7

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

i=N
for j in range(N+1):
    for k in range(N+1):
        check=7
        for l,x in q[i]:
            if x==1:
                if i-1>=l>j:
                    check&=1
                elif i==l:
                    check&=7
                else:
                    check=0
            elif x==2:
                if i==l:
                    check=0
                elif i-1>=l>j:
                    check&=6
                elif j>=l>k:
                    check&=3
                else:
                    check=0
            else:
                if i==l:
                    check=0
                elif i-1>=l>j:
                    check=0
                elif j>=l>k:
                    check&=4
        dp[j][k]=bin(check).count("1")

ndp=[[0 for j in range(N+1)] for k in range(N+1)]
for i in range(N-1,0,-1):
    for j in range(N+1):
        for k in range(N+1):
            t=0
            check=7
            for l,x in q[i]:
                if x==1:
                    if i-1>=l>j:
                        check&=1
                    elif i==l:
                        check&=7
                    else:
                        check=0
                elif x==2:
                    if i==l:
                        check=0
                    elif i-1>=l>j:
                        check&=6
                    elif j>=l>k:
                        check&=3
                    else:
                        check=0
                else:
                    if i==l:
                        check=0
                    elif i-1>=l>j:
                        check=0
                    elif j>=l>k:
                        check&=4
            t=dp[j][k]*((check//1)%2)+dp[i-1][k]*((check//2)%2)+dp[i-1][j]*((check//4)%2)
            t%=mod
            ndp[j][k]=t
    dp=ndp

print(dp[0][0])

Fixed submission:
N,M=map(int,input().split())
q=[[] for i in range(N+1)]
for i in range(M):
    l,r,x=map(int,input().split())
    q[r].append((l,x))
mod=10**9+7

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

i=N
for j in range(N+1):
    for k in range(N+1):
        check=7
        for l,x in q[i]:
            if i==l:
                check=(x==1)*check
            elif i-1>=l>j:
                check=(x==1)*(check%2)+(x==2)*(check&6)
            elif j>=l>k:
                check=(x==2)*(check&3)+(x==3)*(check&4)
            else:
                check*=(x==3)
        dp[j][k]=bin(check).count("1")

ndp=[[0 for j in range(N+1)] for k in range(N+1)]
for i in range(N-1,0,-1):
    for j in range(i):
        for k in range(j+1):
            check=7
            for l,x in q[i]:
                if i==l:
                    check=(x==1)*check
                elif i-1>=l>j:
                    check=(x==1)*(check%2)+(x==2)*(check&6)
                elif j>=l>k:
                    check=(x==2)*(check&3)+(x==3)*(check&4)
                else:
                    check*=(x==3)
            ndp[j][k]=(dp[j][k]*((check//1)%2)+dp[i-1][k]*((check//2)%2)+dp[i-1][j]*((check//4)%2))%mod

    dp=ndp

print(dp[0][0])

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
q=[[] for i in range(N+1)]
for i in range(M):
    l,r,x=map(int,input().split())
    q[r].append((l,x))

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

i=N
for j in range(N+1):
    for k in range(N+1):
        check=7
        for l,x in q[i]:
            if x==1:
                if i-1>=l>j:
                    check&=1
                elif i==l:
                    check&=7
                else:
                    check=0
            elif x==2:
                if i==l:
                    check=0
                elif i-1>=l>j:
                    check&=6
                elif j>=l>k:
                    check&=3
                else:
                    check=0
            else:
                if i==l:
                    check=0
                elif i-1>=l>j:
                    check=0
                elif j>=l>k:
                    check&=4
        dp[i][j][k]=bin(check).count("1")

for i in range(N-1,-1,-1):
    for j in range(N+1):
        for k in range(N+1):
            t=0
            check=7
            for l,x in q[i]:
                if x==1:
                    if i-1>=l>j:
                        check&=1
                    elif i==l:
                        check&=7
                    else:
                        check=0
                elif x==2:
                    if i==l:
                        check=0
                    elif i-1>=l>j:
                        check&=6
                    elif j>=l>k:
                        check&=3
                    else:
                        check=0
                else:
                    if i==l:
                        check=0
                    elif i-1>=l>j:
                        check=0
                    elif j>=l>k:
                        check&=4
            if check &1==1:
                t+=dp[i+1][j][k]
            if check>>1 &1==1:
                t+=dp[i+1][i-1][k]
            if check>>2 &1==1:
                t+=dp[i+1][i-1][j]
            dp[i][j][k]=t

print(dp[1][0][0])

Fixed submission:
N,M=map(int,input().split())
q=[[] for i in range(N+1)]
for i in range(M):
    l,r,x=map(int,input().split())
    q[r].append((l,x))
mod=10**9+7

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

i=N
for j in range(N+1):
    for k in range(N+1):
        check=7
        for l,x in q[i]:
            if i==l:
                check=(x==1)*check
            elif i-1>=l>j:
                check=(x==1)*(check%2)+(x==2)*(check&6)
            elif j>=l>k:
                check=(x==2)*(check&3)+(x==3)*(check&4)
            else:
                check*=(x==3)
        dp[j][k]=bin(check).count("1")

ndp=[[0 for j in range(N+1)] for k in range(N+1)]
for i in range(N-1,0,-1):
    for j in range(i):
        for k in range(j+1):
            check=7
            for l,x in q[i]:
                if i==l:
                    check=(x==1)*check
                elif i-1>=l>j:
                    check=(x==1)*(check%2)+(x==2)*(check&6)
                elif j>=l>k:
                    check=(x==2)*(check&3)+(x==3)*(check&4)
                else:
                    check*=(x==3)
            ndp[j][k]=(dp[j][k]*((check//1)%2)+dp[i-1][k]*((check//2)%2)+dp[i-1][j]*((check//4)%2))%mod

    dp=ndp

print(dp[0][0])