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# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        this.TextLen = inpt[0];

        int seiyakuCount = inpt[1];

        for (int i = 0; i < seiyakuCount; i++)
        {
            inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
            Seiyaku s = new Seiyaku(i, inpt[0]-1, inpt[1]-1, inpt[2]);
            if (!fromDic.ContainsKey(s.FromIdx))
            {
                fromDic.Add(s.FromIdx, new List<Seiyaku>());
            }
            fromDic[s.FromIdx].Add(s);
            if (!toDic.ContainsKey(s.ToIdx))
            {
                toDic.Add(s.ToIdx, new List<Seiyaku>());
            }
            toDic[s.ToIdx].Add(s);
            SeiyakuList.Add(s);
        }
        long ans = this.GetAns(0, new List<Seiyaku>());
        Console.WriteLine(ans);
    }

    private Dictionary<int, Dictionary<string, long>> dic = new Dictionary<int, Dictionary<string, long>>();
    private long GetAns(int idx, List<Seiyaku> targetSeiyaku)
    {
        string key = string.Join(":", targetSeiyaku.Select(a => a.ID + a.Used));
        if (!dic.ContainsKey(idx))
        {
            dic.Add(idx, new Dictionary<string, long>());
        }
        if (dic[idx].ContainsKey(key))
        {
            return dic[idx][key];
        }

        foreach (Seiyaku s in targetSeiyaku)
        {
            if (s.Used.Length > s.Count)
            {
                dic[idx][key] = 0;
                return 0;
            }
        }
        foreach (Seiyaku s in targetSeiyaku.Where(a => a.ToIdx < idx))
        {
            if (s.Used.Length != s.Count)
            {
                dic[idx][key] = 0;
                return 0;
            }
        }

        if (idx >= this.TextLen)
        {
            dic[idx][key] = 1;
            return 1;
        }

        long ans = 0;

        for (char c = 'A'; c <= 'C'; c++)
        {
            List<Seiyaku> subList = targetSeiyaku.Where(a => a.ToIdx >= idx).Select(a => a.Duplicate()).ToList();
            if (fromDic.ContainsKey(idx))
            {
                subList.AddRange(fromDic[idx].Select(a => a.Duplicate()));
            }
            subList.ForEach(a => a.SetFlag(c));
            ans += this.GetAns(idx + 1, subList);
            ans = ans % Mod;
        }
        dic[idx][key] = ans;
        return ans;
    }

    private int TextLen = 0;
    private List<Seiyaku> SeiyakuList = new List<Seiyaku>();
    private Dictionary<int, List<Seiyaku>> fromDic = new Dictionary<int, List<Seiyaku>>();
    private Dictionary<int, List<Seiyaku>> toDic = new Dictionary<int, List<Seiyaku>>();

    private const long Mod = 1000000000 + 7;


    private class Seiyaku
    {
        public int ID;
        public int FromIdx;
        public int ToIdx;
        public int Count;
        public String Used = "";

        public Seiyaku(int id, int from, int to, int cnt)
        {
            this.ID = id;
            this.FromIdx = from;
            this.ToIdx = to;
            this.Count = cnt;
        }

        public Seiyaku Duplicate()
        {
            Seiyaku ins = new Seiyaku(this.ID, this.FromIdx, this.ToIdx, this.Count);
            ins.Used = this.Used;
            return ins;
        }

        public void SetFlag(char c)
        {
            if (Used.Contains(c))
            {
                return;
            }
            string tmp = Used + c;
            this.Used = string.Join("", tmp.OrderBy(a => a));
        }
    }

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"




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

";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}