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: Accepted)

#include<bits/stdc++.h>
#define pi 3.1415926535897932384626
using namespace std;
      
struct P{
  int x,y;
  bool operator < (const P &a )const{
    //if(y!=a.y)
    return y>a.y;
  };
};

int a,b,c,d,i,k,n,m,e,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[301][301][301];
int l[1];
int j[1];

long long x,y,z,mod=1000000007;
string r;
//char r[2];

P u[1];
queue<int> q;
stack<int> s;
//set<int> s;
map<int,int> p;

vector<P> v[333];


bool as(P a,P b)
{
  if(a.x!=b.x)
  return a.x<b.x;
  return a.y>b.y;
}

int f(int n,int m,int k)
{
  if(o[n][m][k]>=0) return o[n][m][k];
  for(int h=0;h<v[k].size();h++)
  {
    if((v[k][h].y==1&&m>=v[k][h].x)||(v[k][h].y==2&&(n>=v[k][h].x||m<v[k][h].x))||(v[k][h].y==3&&n<v[k][h].x))
    return o[n][m][k]=0;
  }
  if(k==a)
  {
    return o[n][m][k]=1;
  }
  int p=f(n,m,k+1);
  p+=f(n,k,k+1),p%=mod;
  p+=f(m,k,k+1),p%=mod;
  return o[n][m][k]=p;
}

int main()
{
  memset(o,-1,sizeof(o));
  scanf("%d %d",&a,&b);
  for(int t=1;t<=b;t++)
  {
    scanf("%d %d %d",&n,&m,&k);
    v[m].push_back({n,k});
  }
  x=(long long)f(0,0,1)*3%mod;
  printf("%lld",x);
}
//7