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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/***************************************************************
	File name: AtCoder074E.cpp
	Author: ljfcnyali
	Create time: 2019年10月08日 星期二 08时41分45秒
***************************************************************/
#include<bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for ( int i = (a), _end_ = (b); i <= _end_; ++ i ) 
#define mem(a) memset ( (a), 0, sizeof ( a ) ) 
#define str(a) strlen ( a ) 
#define pii pair<int, int>
#define int long long
typedef long long LL;

const int maxn = 310;
const int Mod = 1e9 + 7;

int f[maxn][maxn][maxn], n, m, ans;
bool vis[maxn][maxn][maxn];

inline void Solve(int l, int r, int x)
{
    if ( x == 1 ) 
    {
        REP(i, l, r - 1) REP(j, 1, r - 1) vis[r][i][j] = vis[r][j][i] = true;
        return ;
    }
    if ( x == 2 )
    {
        REP(i, l, r - 1) REP(j, l, r - 1) vis[r][i][j] = true;
        REP(i, 0, l - 1) REP(j, 0, l - 1) vis[r][i][j] = true;
        return ;
    }
    REP(i, 0, l - 1) REP(j, 0, r) vis[r][j][i] = vis[r][i][j] = true;
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    scanf("%lld%lld", &n, &m);
    REP(i, 1, m)
    {
        int l, r, x; scanf("%lld%lld%lld", &l, &r, &x);
        Solve(l, r, x);
    }
    f[1][0][0] = 1;
    REP(i, 1, n - 1)
    {
        REP(j, 0, i - 1)
            REP(k, 0, max(0ll, j - 1))
            {
                if ( vis[i][j][k] || !f[i][j][k] ) continue ;
                f[i + 1][j][k] = (f[i + 1][j][k] + f[i][j][k]) % Mod;
                f[i + 1][i][k] = (f[i + 1][i][k] + f[i][j][k]) % Mod;
                f[i + 1][i][j] = (f[i + 1][i][j] + f[i][j][k]) % Mod;
            }
    }
    REP(i, 0, n - 1) REP(j, 0, max(0ll, i - 1)) if ( !vis[n][i][j] ) ans = (ans + f[n][i][j]) % Mod;
    printf("%lld\n", (ans * 3) % Mod);
    return 0;
}