Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md=10**9+7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
    dp[0][0][0] = 1
    ans = 0
    for r in range(n):
        for g in range(n):
            for b in range(n):
                pre = dp[r][g][b]
                if pre == 0: continue
                i = max(r, g, b) + 1
                flag=False
                for l, x in rtol[i-1]:
                    over_l = [r >= l, g >= l, b >= l]
                    if sum(over_l)!=x:
                        flag=True
                        break
                if flag:continue
                dp[i][g][b] = (pre+dp[i][g][b])%md
                dp[r][i][b] = (pre+dp[r][i][b])%md
                dp[r][g][i] = (pre+dp[r][g][i])%md
    for l, x in rtol[n]:
        for i in range(n):
            for j in range(n):
                over_l = [i >= l, j >= l]
                if sum(over_l)+1 != x:
                    dp[i][j][n]=0
                    dp[i][n][j]=0
                    dp[n][j][i]=0

    ans+=sum(dp[i][j][n] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[i][n][j] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[n][j][i] for i in range(n) for j in range(n))
    ans%=md
    print(ans)

main()


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 一番後ろの色を(赤に)固定した上で、もらうDPに書き換え
def main():
    md = 10 ** 9 + 7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for k in range(1, n + 1):
        for i in range(k - 1):
            dp[i][k - 1] = dp[k - 1][i] = sum(dp[i][:k - 1]) % md
        for l, x in rtol[k]:
            if x == 1:
                for i in range(l, k):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0
            if x == 2:
                for i in range(l, k):
                    for j in range(i + 1, k):
                        dp[i][j] = dp[j][i] = 0
                for i in range(l):
                    for j in range(i + 1):
                        dp[i][j] = dp[j][i] = 0
            if x == 3:
                for i in range(l):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0

    # p2D(dp)
    print(sum(sum(dr) for dr in dp) * 3 % md)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md=10**9+7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
    dp[0][0][0] = 1
    ans = 0
    for r in range(n):
        for g in range(n):
            for b in range(n):
                pre = dp[r][g][b]
                if pre == 0: continue
                i = max(r, g, b) + 1
                flag=False
                for l, x in rtol[i-1]:
                    over_l = [r >= l, g >= l, b >= l]
                    if sum(over_l)!=x:
                        flag=True
                        break
                if flag:continue
                dp[i][g][b] = (pre+dp[i][g][b])%md
                dp[r][i][b] = (pre+dp[r][i][b])%md
                dp[r][g][i] = (pre+dp[r][g][i])%md
    for l, x in rtol[n]:
        for i in range(n):
            for j in range(n):
                over_l = [i >= l, j >= l]
                if sum(over_l)+1 != x:
                    dp[i][j][n]=0
                    dp[i][n][j]=0
                    dp[n][j][i]=0

    ans+=sum(dp[i][j][n] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[i][n][j] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[n][j][i] for i in range(n) for j in range(n))
    ans%=md
    print(ans)

main()


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 一番後ろの色を(赤に)固定した上で、もらうDPに書き換え
def main():
    md = 10 ** 9 + 7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for k in range(1, n + 1):
        for i in range(k - 1):
            dp[i][k - 1] = dp[k - 1][i] = sum(dp[i][:k - 1]) % md
        for l, x in rtol[k]:
            if x == 1:
                for i in range(l, k):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0
            if x == 2:
                for i in range(l, k):
                    for j in range(i + 1, k):
                        dp[i][j] = dp[j][i] = 0
                for i in range(l):
                    for j in range(i + 1):
                        dp[i][j] = dp[j][i] = 0
            if x == 3:
                for i in range(l):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0

    # p2D(dp)
    print(sum(sum(dr) for dr in dp) * 3 % md)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md=10**9+7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
    dp[0][0][0] = 1
    ans = 0
    for r in range(n):
        for g in range(n):
            for b in range(n):
                pre = dp[r][g][b]
                if pre == 0: continue
                i = max(r, g, b) + 1
                dp[i][g][b] = (pre+dp[i][g][b])%md
                dp[r][i][b] = (pre+dp[r][i][b])%md
                dp[r][g][i] = (pre+dp[r][g][i])%md
                for l, x in rtol[i]:
                    over_l = [r >= l, g >= l, b >= l]
                    if over_l[1] + over_l[2] + 1 != x:
                        dp[i][g][b] = 0
                    if over_l[0] + over_l[2] + 1 != x:
                        dp[r][i][b] = 0
                    if over_l[0] + over_l[1] + 1 != x:
                        dp[r][g][i] = 0
                #if i == n:
                #    ans += dp[i][g][b] + dp[r][i][b] + dp[r][g][i]
    ans+=sum(dp[i][j][n] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[i][n][j] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[n][j][i] for i in range(n) for j in range(n))
    ans%=md
    print(ans)

main()


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 一番後ろの色を(赤に)固定した上で、もらうDPに書き換え
def main():
    md = 10 ** 9 + 7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for k in range(1, n + 1):
        for i in range(k - 1):
            dp[i][k - 1] = dp[k - 1][i] = sum(dp[i][:k - 1]) % md
        for l, x in rtol[k]:
            if x == 1:
                for i in range(l, k):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0
            if x == 2:
                for i in range(l, k):
                    for j in range(i + 1, k):
                        dp[i][j] = dp[j][i] = 0
                for i in range(l):
                    for j in range(i + 1):
                        dp[i][j] = dp[j][i] = 0
            if x == 3:
                for i in range(l):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0

    # p2D(dp)
    print(sum(sum(dr) for dr in dp) * 3 % md)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md=10**9+7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
    dp[0][0][0] = 1
    ans = 0
    for r in range(n):
        for g in range(n):
            for b in range(n):
                pre = dp[r][g][b]
                if pre == 0: continue
                i = max(r, g, b) + 1
                dp[i][g][b] = (pre+dp[i][g][b])%md
                dp[r][i][b] = (pre+dp[r][i][b])%md
                dp[r][g][i] = (pre+dp[r][g][i])%md
                for l, x in rtol[i]:
                    over_l = [r >= l, g >= l, b >= l]
                    if over_l[1] + over_l[2] + 1 != x:
                        dp[i][g][b] = 0
                    if over_l[0] + over_l[2] + 1 != x:
                        dp[r][i][b] = 0
                    if over_l[0] + over_l[1] + 1 != x:
                        dp[r][g][i] = 0
                #if i == n:
                #    ans += dp[i][g][b] + dp[r][i][b] + dp[r][g][i]
    ans+=sum(dp[i][j][n] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[i][n][j] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[n][j][i] for i in range(n) for j in range(n))
    ans%=md
    print(ans)

main()


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 一番後ろの色を(赤に)固定した上で、もらうDPに書き換え
def main():
    md = 10 ** 9 + 7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for k in range(1, n + 1):
        for i in range(k - 1):
            dp[i][k - 1] = dp[k - 1][i] = sum(dp[i][:k - 1]) % md
        for l, x in rtol[k]:
            if x == 1:
                for i in range(l, k):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0
            if x == 2:
                for i in range(l, k):
                    for j in range(i + 1, k):
                        dp[i][j] = dp[j][i] = 0
                for i in range(l):
                    for j in range(i + 1):
                        dp[i][j] = dp[j][i] = 0
            if x == 3:
                for i in range(l):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0

    # p2D(dp)
    print(sum(sum(dr) for dr in dp) * 3 % md)

main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    md=10**9+7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]
    dp[0][0][0] = 1
    ans = 0
    for r in range(n):
        for g in range(n):
            for b in range(n):
                pre = dp[r][g][b]
                if pre == 0: continue
                i = max(r, g, b) + 1
                dp[i][g][b] += pre
                dp[r][i][b] += pre
                dp[r][g][i] += pre
                for l, x in rtol[i]:
                    over_l = [r >= l, g >= l, b >= l]
                    if over_l[1] + over_l[2] + 1 != x:
                        dp[i][g][b] = 0
                    if over_l[0] + over_l[2] + 1 != x:
                        dp[r][i][b] = 0
                    if over_l[0] + over_l[1] + 1 != x:
                        dp[r][g][i] = 0
                #if i == n:
                #    ans += dp[i][g][b] + dp[r][i][b] + dp[r][g][i]
    ans+=sum(dp[i][j][n] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[i][n][j] for i in range(n) for j in range(n))
    ans%=md
    ans+=sum(dp[n][j][i] for i in range(n) for j in range(n))
    ans%=md
    print(ans)

main()


Fixed submission:
import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

# 一番後ろの色を(赤に)固定した上で、もらうDPに書き換え
def main():
    md = 10 ** 9 + 7
    n, m = MI()
    rtol = defaultdict(list)
    for _ in range(m):
        l, r, x = MI()
        rtol[r].append([l, x])
    dp = [[0] * (n + 1) for _ in range(n + 1)]
    dp[0][0] = 1
    for k in range(1, n + 1):
        for i in range(k - 1):
            dp[i][k - 1] = dp[k - 1][i] = sum(dp[i][:k - 1]) % md
        for l, x in rtol[k]:
            if x == 1:
                for i in range(l, k):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0
            if x == 2:
                for i in range(l, k):
                    for j in range(i + 1, k):
                        dp[i][j] = dp[j][i] = 0
                for i in range(l):
                    for j in range(i + 1):
                        dp[i][j] = dp[j][i] = 0
            if x == 3:
                for i in range(l):
                    for j in range(k):
                        dp[i][j] = dp[j][i] = 0

    # p2D(dp)
    print(sum(sum(dr) for dr in dp) * 3 % md)

main()