Problem:
Score : 800 points

Problem Statement

There are N squares arranged in a row.
The squares are numbered 1, 2, ..., N, from left to right.

Snuke is painting each square in red, green or blue.
According to his aesthetic sense, the following M conditions must all be satisfied.
The i-th condition is:

There are exactly x_i different colors among squares l_i, l_i + 1, ..., r_i.

In how many ways can the squares be painted to satisfy all the conditions?
Find the count modulo 10^9+7.

Constraints

1 ≤ N ≤ 300

1 ≤ M ≤ 300

1 ≤ l_i ≤ r_i ≤ N

1 ≤ x_i ≤ 3

Input

Input is given from Standard Input in the following format:

N M
l_1 r_1 x_1
l_2 r_2 x_2
:
l_M r_M x_M

Output

Print the number of ways to paint the squares to satisfy all the conditions, modulo 10^9+7.

Sample Input 1

3 1
1 3 3

Sample Output 1

6

The six ways are:

RGB

RBG

GRB

GBR

BRG

BGR

where R, G and B correspond to red, green and blue squares, respectively.

Sample Input 2

4 2
1 3 1
2 4 2

Sample Output 2

6

The six ways are:

RRRG

RRRB

GGGR

GGGB

BBBR

BBBG

Sample Input 3

1 3
1 1 1
1 1 2
1 1 3

Sample Output 3

0

There are zero ways.

Sample Input 4

8 10
2 6 2
5 5 1
3 5 2
4 7 3
4 4 1
2 3 1
7 7 1
1 5 2
1 7 3
3 4 2

Sample Output 4

108

Solution in PyPy3 (2.4.0) (judge verdict: Time Limit Exceeded)

import sys
readline = sys.stdin.readline

def viol(c2, c3, l, x):
    res = False
    if x == 1:
        if not c2 < l:
            res = True
    elif x == 2:
        if not c3 < l <= c2:
            res = True
    else:
        if not l <= c3:
            res = True
    return res
MOD = 10**9+7
N, M = map(int, readline().split())
limit = [[] for _ in range(N+3)]

for _ in range(M):
    l, r, x = map(int, readline().split())
    limit[r+2].append((l+2, x))


dp = [[[0]*(N+3) for _ in range(N+3)] for _ in range(N+3)]
dp[0][1][2] = 1

for i in range(2, N+2):
    
    r = i
    for g in range(r):
        for b in range(g):
            d = dp[r][g][b]
            dp[r+1][g][b] = (dp[r+1][g][b] + d)%MOD
            dp[r][r+1][b] = (dp[r][r+1][b] + d)%MOD
            dp[r][g][r+1] = (dp[r][g][r+1] + d)%MOD
    for b in range(r):
        for g in range(b):
            d = dp[r][g][b]
            dp[r+1][g][b] = (dp[r+1][g][b] + d)%MOD
            dp[r][r+1][b] = (dp[r][r+1][b] + d)%MOD
            dp[r][g][r+1] = (dp[r][g][r+1] + d)%MOD
    
    g = i
    for r in range(g):
        for b in range(r):
            d = dp[r][g][b]
            dp[g+1][g][b] = (dp[g+1][g][b] + d)%MOD
            dp[r][g+1][b] = (dp[r][g+1][b] + d)%MOD
            dp[r][g][g+1] = (dp[r][g][g+1] + d)%MOD
    for b in range(g):
        for r in range(b):
            d = dp[r][g][b]
            dp[g+1][g][b] = (dp[g+1][g][b] + d)%MOD
            dp[r][g+1][b] = (dp[r][g+1][b] + d)%MOD
            dp[r][g][g+1] = (dp[r][g][g+1] + d)%MOD
    
    b = i
    for r in range(b):
        for g in range(r):
            d = dp[r][g][b]
            dp[b+1][g][b] = (dp[b+1][g][b] + d)%MOD
            dp[r][b+1][b] = (dp[r][b+1][b] + d)%MOD
            dp[r][g][b+1] = (dp[r][g][b+1] + d)%MOD
    for g in range(b):
        for r in range(g):
            d = dp[r][g][b]
            dp[b+1][g][b] = (dp[b+1][g][b] + d)%MOD
            dp[r][b+1][b] = (dp[r][b+1][b] + d)%MOD
            dp[r][g][b+1] = (dp[r][g][b+1] + d)%MOD
    
    for l, x in limit[i+1]:
        r = i+1
        for g in range(r):
            for b in range(g):
                if viol(g, b, l, x):
                    dp[r][g][b] = 0
        for b in range(r):
            for g in range(b):
                if viol(b, g, l, x):
                    dp[r][g][b] = 0
        g = i+1
        for r in range(g):
            for b in range(r):
                if viol(r, b, l, x):
                    dp[r][g][b] = 0
        for b in range(g):
            for r in range(b):
                if viol(b, r, l, x):
                    dp[r][g][b] = 0
        b = i+1
        for r in range(b):
            for g in range(r):
                if viol(r, g, l, x):
                    dp[r][g][b] = 0
        for g in range(b):
            for r in range(g):
                if viol(g, r, l, x):
                    dp[r][g][b] = 0
res = 0
for r in range(N+3):
    for g in range(N+3):
        for b in range(N+3):
            if max(r, g, b) == 2+N:
                res = (res + dp[r][g][b])%MOD
print(res)