#P15863. [LBA-OI R3 C] wywmrfz

[LBA-OI R3 C] wywmrfz

Description

Little Y has recently become obsessed with a tower defense game.

Today, she focuses on a character, Little W. Little W can block at most 2 enemies at the same time and perform several attacks. Each attack can be one of two types:

  1. Normal attack: Deals 1 damage to all blocked enemies simultaneously.
  2. Critical strike: Deals 2 damage to all blocked enemies simultaneously.

Now, Little Y gives Little W TT tasks. In each task, Little W needs to attack exactly nn times to defeat mm enemies. The ii-th enemy has health kik_i and must be defeated within the lil_i-th to rir_i-th attacks. Little Y will not make it too hard: Little W never needs to face 3 or more enemies at once.

Little Y wants to know: how many valid attack sequences satisfy all requirements? Since the answer can be huge, you only need to output it modulo 109+710^9+7.

Two attack sequences are different if there exists an i[1,n]i \in [1,n] such that one sequence uses a normal attack on the ii-th step and the other uses a critical strike.

Formal Statement

Given positive integers nn and mm triples (l,r,k)(l, r, k), a sequence is called good if and only if:

  1. All elements are in {1,2}\{1,2\}.
  2. For every i[1,m]i \in [1,m], j=liriaj=ki\sum\limits_{j=l_i}^{r_i} a_j = k_i.

You need to find the number of good sequences modulo 109+710^9+7.

Guarantee: Every position is covered by at most 2 intervals, i.e.

$$\forall i\in[1,n],\; \sum\limits_{j=1}^m \left[\,i\in[l_j,r_j]\,\right] \le 2$$

There are TT test cases in one test point.

Input Format

The first line contains an integer TT, the number of tasks.

For each test case:

  • The first line contains two integers n,mn, m.
  • The next mm lines each contain three integers l,r,kl, r, k, representing the information of an enemy.

Output Format

Output TT lines. Each line contains one integer: the answer for the ii-th test case modulo 109+710^9+7.

3
5 2
1 4 7
2 5 7
10 3
3 3 1
3 4 2
7 9 4
5000 6
1657 2689 1560
2 1789 2643
1790 3701 2878
1360 1655 429
2692 3856 1744
3 1357 2010
4
96
952912621

Hint

Data Constraints

Test Case nn \le mm \le Special Property
121\sim 2 1616 3232 None
353\sim 5 3636 7272
676\sim 7 60006000 60006000 Yes
898\sim 9 22 None
101210\sim 12 200200 400400
131513\sim 15 300300 600600
161916\sim 19 30003000 60006000
202520\sim 25 80008000 1600016000

Special Property: Every position is covered by at most 1 interval: $\forall i\in[1,n],\; \sum\limits_{j=1}^m \left[\,i\in[l_j,r_j]\,\right] \le 1$.

For 100%100\% data: 1n8000, 1m2n, 1T101\le n\le 8000,\ 1\le m\le 2n,\ 1\le T\le 10.

Hard Guarantee: $\forall i\in[1,n],\; \sum\limits_{j=1}^m \left[\,i\in[l_j,r_j]\,\right] \le 2$.