Problem: 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.

Examples

Input

3 1
1 3 3


Output

6


Input

4 2
1 3 1
2 4 2


Output

6


Input

1 3
1 1 1
1 1 2
1 1 3


Output

0


Input

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


Output

108

Solution (in JAVA):

Solution:
import static java.lang.Math.*;
import static java.util.Arrays.*;
import java.util.*;
import java.io.*;

public class Main {
	
	final long MOD = (long)1e9+7;

	int[][][] dp = new int[301][301][301];
	boolean[][][] invalid = new boolean[301][301][301];

	void solve() {
		int N = sc.nextInt();
		int M = sc.nextInt();
		
		int[] L = new int[M];
		int[] R = new int[M];
		int[] X = new int[M];
		for (int i = 0; i < M; i++) {
			L[i] = sc.nextInt();
			R[i] = sc.nextInt();
			X[i] = sc.nextInt();
		}

		for (int m = 0; m < M; m++) {
			if (X[m] == 1) {
				for (int i = 0; i < R[m]; i++)
					for (int j = 0; j < R[m]; j++) {
						if (i >= L[m] || j >= L[m]) {
							invalid[R[m]][i][j] = true;
							invalid[i][R[m]][j] = true;
							invalid[i][j][R[m]] = true;
						}
					}
			}
			if (X[m] == 2) {
				for (int i = 0; i < L[m]; i++) {
					for (int j = 0; j < L[m]; j++) {
						invalid[R[m]][i][j] = true;
						invalid[i][R[m]][j] = true;
						invalid[i][j][R[m]] = true;
					}
				}
				for (int i = L[m]; i < R[m]; i++) {
					for (int j = L[m]; j < R[m]; j++) {
						invalid[R[m]][i][j] = true;
						invalid[i][R[m]][j] = true;
						invalid[i][j][R[m]] = true;
					}
				}
			}
			if (X[m] == 3) {
				for (int i = 0; i < R[m]; i++)
					for (int j = 0; j < R[m]; j++) {
						if (i < L[m] || j < L[m]) {
							invalid[R[m]][i][j] = true;
							invalid[i][R[m]][j] = true;
							invalid[i][j][R[m]] = true;
						}
					}
			}
		}
		
		dp[0][0][0] = 1;
		
		for (int r = 0; r <= N; r++) {
			for (int g = 0; g <= N; g++) {
				for (int b = 0; b <= N; b++) {
					if (invalid[r][g][b]) dp[r][g][b] = 0;
					dp[r][g][b] %= MOD;
					int n = max(max(r, g), b) + 1;
					if (n > N) continue;
					dp[n][g][b] += dp[r][g][b];
					dp[r][n][b] += dp[r][g][b];
					dp[r][g][n] += dp[r][g][b];
					if (dp[n][g][b] >= MOD) dp[n][g][b] -= MOD;
					if (dp[r][n][b] >= MOD) dp[r][n][b] -= MOD;
					if (dp[r][g][n] >= MOD) dp[r][g][n] -= MOD;
				}
			}
		}
		
		
		long ans = 0;
		for (int i = 0; i < N; i++) 
			for (int j = 0; j < N; j++) {
				ans += dp[N][i][j];
				ans += dp[i][N][j];
				ans += dp[i][j][N];
				ans %= MOD;
			}
		out.println(ans);
	}
	
	static void tr(Object... os) { System.err.println(deepToString(os)); }
	static void tr(int[][] as) { for (int[] a : as) tr(a); }

	void print(int[] a) {
		out.print(a[0]);
		for (int i = 1; i < a.length; i++) out.print(" " + a[i]);
		out.println();
	}

	public static void main(String[] args) throws Exception {
		new Main().run();
	}

	MyScanner sc = null;
	PrintWriter out = null;
	public void run() throws Exception {
		sc = new MyScanner(System.in);
		out = new PrintWriter(System.out);
		for (;sc.hasNext();) {
			solve();
			out.flush();
		}
		out.close();
	}

	class MyScanner {
		String line;
		BufferedReader reader;
		StringTokenizer tokenizer;

		public MyScanner(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			tokenizer = null;
		}
		public void eat() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					line = reader.readLine();
					if (line == null) {
						tokenizer = null;
						return;
					}
					tokenizer = new StringTokenizer(line);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}
		public String next() {
			eat();
			return tokenizer.nextToken();
		}
		public String nextLine() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		public boolean hasNext() {
			eat();
			return (tokenizer != null && tokenizer.hasMoreElements());
		}
		public int nextInt() {
			return Integer.parseInt(next());
		}
		public long nextLong() {
			return Long.parseLong(next());
		}
		public double nextDouble() {
			return Double.parseDouble(next());
		}
		public int[] nextIntArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++) a[i] = nextInt();
			return a;
		}
	}
}