QOJ.ac

QOJ

実行時間制限: 4.0 s メモリ制限: 512 MB 満点: 100

#17612. Z1

統計

Ivan is designing a template problem for a high school informatics competition. In this problem, a sequence of numbers $S = a_1, a_2, \dots, a_n$ is given, where $0 \le a_j < H$, along with $m$ queries of the form $x_j, y_j$ where $1 \le x_j \le y_j \le n$. The answer to the $j$-th query is the maximum of the numbers in the sequence $S$ at positions between $x_j$ and $y_j$ inclusive: $z_j = \max(a_{x_j}, a_{x_j+1}, \dots, a_{y_j})$.

Ivan created an excellent test case for this problem, but he lost the original sequence $S$. You are given the length of the original sequence $n$, the constraint on the size of the sequence elements $H$, and $m$ queries $x_j, y_j$ along with their corresponding answers $z_j$. A sequence $S$ of length $n$ is good if it consists of numbers between $0$ and $H - 1$ inclusive, and every $z_j$ is the correct answer for the corresponding query $x_j, y_j$. Determine the number of good sequences $S$ modulo $10^9 + 7$.

Input

The first line contains the natural numbers $n$, $m$, and $H$ — the length of the sequence, the number of queries, and the constraint on the size of the sequence elements. In the $j$-th of the following $m$ lines, there are three integers $x_j$, $y_j$, and $z_j$ ($1 \le x_j \le y_j \le n$, $0 \le z_j < H$) — the $j$-th query and its answer.

Output

Print a single number — the required number of good sequences modulo $10^9 + 7$.

Subtasks

In all subtasks, $1 \le m, H \le 10^6$.

Subtask Points Constraints
1 20 $n \le 100$
2 30 $n \le 1\,000$
3 50 $n \le 1\,000\,000$

Examples

Input 1

3 2 3
1 2 1
2 2 0

Output 1

3

Input 2

7 10 3
1 3 1
3 4 1
3 6 2
4 5 2
1 1 1
1 2 1
3 3 0
1 1 1
3 3 0
3 6 2

Output 2

18

Note

Explanation of the first example: Due to the second query, the element $a_2$ must be $0$, and therefore, due to the first query, the element $a_1$ must be $1$. The element $a_3$ can be any non-negative integer less than $H = 3$. Thus, all good sequences $S$ are $(1, 0, 0)$, $(1, 0, 1)$, and $(1, 0, 2)$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.