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 java.io.*;
import java.util.*;

public class Main {
    private FastScanner in;
    private PrintWriter out;

    class Condition {
        int l, r, cnt;

        public Condition(int l, int r, int cnt) {
            this.l = l;
            this.r = r;
            this.cnt = cnt;
        }
    }

    private void solve() {
        final int mod = (int) 1e9 + 7;
        int n = in.nextInt();
        int m = in.nextInt();
        ArrayList<Condition>[] conditions = new ArrayList[n + 1];
        for (int i = 0; i < conditions.length; i++) {
            conditions[i] = new ArrayList<>();
        }
        for (int i = 0; i < m; i++) {
            int fr = in.nextInt(), to = in.nextInt(), cnt = in.nextInt();
            conditions[to].add(new Condition(fr, to, cnt));
        }
        long[][] dp = new long[n][n];
        long[][] ndp = new long[n][n];
        dp[0][0] = 1;
        for (int pos = 1; pos <= n; pos++) {
            for (int i = 0; i < n; i++) {
                Arrays.fill(ndp[i], 0);
            }
            for (int p1 = 0; p1 < n; p1++) {
                for (int p2 = 0; p2 < n; p2++) {
                    long v = dp[p1][p2];
                    if (v == 0) {
                        continue;
                    }
                    if (pos == 1) {
                        ndp[0][0] += v * 3;
                    } else {
                        if (p1 == 0 && p2 == 0) {
                            ndp[0][0] += v;
                            ndp[0][pos - 1] += v * 2;
                        } else {
                            ndp[p1][p2] += v;
                            ndp[p1][pos - 1] += v;
                            ndp[p2][pos - 1] += v;
                        }
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    while (ndp[i][j] > mod) {
                        ndp[i][j] -= mod;
                    }
                    if (ndp[i][j] != 0) {
                        for (Condition c : conditions[pos]) {
                            int realC = 1;
                            if (i >= c.l) {
                                realC++;
                            }
                            if (j >= c.l) {
                                realC++;
                            }
                            if (realC != c.cnt) {
                                ndp[i][j] = 0;
                                break;
                            }
                        }
                    }
                }
            }
            long[][] tmp = dp;
            dp = ndp;
            ndp = tmp;
        }
        long res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                res += dp[i][j];
            }
        }
        out.println(res % mod);
    }

    private void run() {
        try {
            in = new FastScanner(new File("Main.in"));
            out = new PrintWriter(new File("Main.out"));

            solve();

            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void runIO() {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.close();
    }

    private class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        FastScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        FastScanner(InputStream f) {
            br = new BufferedReader(new InputStreamReader(f));
        }

        String next() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return null;
                st = new StringTokenizer(s);
            }
            return st.nextToken();
        }

        boolean hasMoreTokens() {
            while (st == null || !st.hasMoreTokens()) {
                String s = null;
                try {
                    s = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (s == null)
                    return false;
                st = new StringTokenizer(s);
            }
            return true;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

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