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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

const MOD: i32 = 1000000007;

fn main() {
    let (n, m): (usize, usize) = read();
    let q: Vec<(usize, usize, usize)> = readn(m);
    let mut vec: Vec<Vec<(usize, usize)>> = vec![vec![]; n+1];
    for (l, r, c) in q {
        vec[r].push((l, c));
    }

    let mut dp: Vec<Vec<Vec<i32>>> = vec![vec![vec![0; n+1]; n+1]; n+1];
    dp[0][0][0] = 1;

    for x in 0..n {
        for p in 0..n {
            for q in 0..n {
                let base = dp[x][p][q];
                if base == 0 {
                    continue
                }

                let r = x + 1;
                for (tp, tq) in vec![(p, q), (x, q), (p, x)] {
                    let mut ok = true;
                    for &(l, c) in &vec[x+1] {
                        if color(tp, tq, l, r) != c {
                            ok = false;
                            break;
                        }
                    }
                    if ok {
                        dp[x+1][tp][tq] += base;
                        if dp[x+1][tp][tq] >= MOD {
                            dp[x+1][tp][tq] -= MOD;
                        }
                    }
                }
            }
        }
    }

    let mut sum: i64 = 0;
    for p in 0..n {
        for q in 0..n {
            sum += dp[n][p][q] as i64;
        }
    }
    sum %= MOD as i64;

    println!("{}", sum);
}

fn color(a: usize, b: usize, l: usize, r: usize) -> usize {
    let mut ret = 1;
    if l <= a && a <= r {
        ret += 1;
    }
    if l <= b && b <= r {
        ret += 1;
    }
    ret
}