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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>

constexpr int MOD = 1e9 + 7;

struct modint {
	int n;
	modint(int n = 0) : n(n) {}
};

modint operator+(modint a, modint b) { return modint((a.n += b.n) >= MOD ? a.n - MOD : a.n); }
modint operator-(modint a, modint b) { return modint((a.n -= b.n) < 0 ? a.n + MOD : a.n); }
modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % MOD); }
modint &operator+=(modint &a, modint b) { return a = a + b; }
modint &operator-=(modint &a, modint b) { return a = a - b; }
modint &operator*=(modint &a, modint b) { return a = a * b; }

int main() {
	// dp[i][i][j]

	int n, m;
	std::cin >> n >> m;

	static modint dp[310][310][310];

	std::vector<std::vector<int>> query(n + 1);
	std::vector<int> l(m), r(m), x(m);
	for (int i = 0; i < m; i++) {
		std::cin >> l[i] >> r[i] >> x[i];
		query[r[i]].push_back(i);
	}

	dp[0][0][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int u = 0; u < n; u++) {
			for (int v = 0; v < n; v++) {
				int us[3] = { u, v, u };
				int vs[3] = { v, i - 1, i - 1 };
				int ws[3] = { i, i ,i };
				bool ok[3] = { true, true, true };

				for (int id : query[i]) {
					for (int j = 0; j < 3; j++) {
						int cnt[3] = {};
						if (us[j] >= l[id]) cnt[j]++;
						if (vs[j] >= l[id]) cnt[j]++;
						if (ws[j] >= l[id]) cnt[j]++;
						if (x[id] != cnt[j]) {
							ok[j] = false;
						}
					}
				}

				for (int j = 0; j < 3; j++) {
					if (ok[j]) {
						dp[i][us[j]][vs[j]] += dp[i - 1][u][v];
					}
				}
			}
		}
	}

	modint ans = 0;
	for (int i = 0; i < n + 1; i++) {
		for (int j = 0; j < n + 1; j++) {
			ans += dp[n][i][j];
		}
	}
	std::cout << ans.n << std::endl;
}