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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)


import java.io.*;
import java.util.*;
 
public class Main{
	int N, M;
	ArrayList<ArrayList<Pair>> pairs;
	long mod = 1000000007;
	int[][][] memo;
	class Pair{
		int l, r, x;
		public Pair(int l, int r, int x){
			this.l = l;
			this.r = r;
			this.x = x;
		}
	}
	
	public void solve(){
		N = nextInt();
		M = nextInt();
		pairs = new ArrayList<>(N);
		for(int i = 0; i < N; i++){
			pairs.add(new ArrayList<>());
		}
		
		for(int i = 0; i < M; i++){
			Pair p = new Pair(nextInt() - 1, nextInt() - 1, nextInt());
			pairs.get(p.r).add(p);
		}
		memo = new int[N + 2][N + 2][N + 2];
		for(int i = 0; i < memo.length; i++){
			for(int j = 0; j < memo[i].length; j++){
				for(int k = 0; k < memo[i][j].length; k++){
					memo[i][j][k] = -1;
				}
			}
		}
		out.println(dfs(-1, -1, -1));
	}
	
	public int dfs(int r, int g, int b)	{
		if(memo[r + 1][g + 1][b + 1] != -1){
			return memo[r + 1][g + 1][b + 1];
		}
		int[] arr = {r, g, b};
		Arrays.sort(arr);
		int idx = arr[2] + 1;
		
		
		if(idx > 0){
			for(Pair p : pairs.get(idx - 1)){
				if(p.x == 1){
					if(arr[1] < p.l){
						
					}else{
						return 0;
					}
				}else if(p.x == 2){
					if(arr[1] >= p.l && arr[0] < p.l){
						
					}else{
						return 0;
					}
				}else if(p.x == 3){
					if(arr[0] >= p.l){
						
					}else{
						return 0;
					}
				}
			}
		}
		
		if(idx == N) return 1;
		
		long ans = dfs(idx, g, b);
		ans += dfs(r, idx, b);
		ans += dfs(r, g, idx);
		
		return memo[r + 1][g + 1][b + 1] = (int)(ans % mod);
	}
	
	private static PrintWriter out;
	public static void main(String[] args){
		out = new PrintWriter(System.out);
		new Main().solve();
		out.flush();
	}
	
	
	
	public static int nextInt(){
		int num = 0;
		String str = next();
		boolean minus = false;
		int i = 0;
		if(str.charAt(0) == '-'){
			minus = true;
			i++;
		}
		int len = str.length();
		for(;i < len; i++){
			char c = str.charAt(i);
			if(!('0' <= c && c <= '9')) throw new RuntimeException();
			num = num * 10 + (c - '0');
		}
		return minus ? -num : num;
	}
	
	public static long nextLong(){
		long num = 0;
		String str = next();
		boolean minus = false;
		int i = 0;
		if(str.charAt(0) == '-'){
			minus = true;
			i++;
		}
		int len = str.length();
		for(;i < len; i++){
			char c = str.charAt(i);
			if(!('0' <= c && c <= '9')) throw new RuntimeException();
			num = num * 10l + (c - '0');
		}
		return minus ? -num : num;
	}
	public static String next(){
		int c;
		while(!isAlNum(c = read())){}
		StringBuilder build = new StringBuilder();
		build.append((char)c);
		while(isAlNum(c = read())){
			build.append((char)c);
		}
		return build.toString();
	}
	
	
	private static byte[] inputBuffer = new byte[1024];
	private static int bufferLength = 0;
	private static int bufferIndex = 0;
	private static int read(){
		if(bufferLength < 0) throw new RuntimeException();
		if(bufferIndex >= bufferLength){
			try{
				bufferLength = System.in.read(inputBuffer);
				bufferIndex = 0;
			}catch(IOException e){
				throw new RuntimeException(e);
			}
			if(bufferLength <= 0) return (bufferLength = -1);
		}
		return inputBuffer[bufferIndex++];
	}
	
	private static boolean isAlNum(int c){
		return '!' <= c && c <= '~';
	}
}