#P9522. [JOIST 2022] 错误拼写 / Misspelling

[JOIST 2022] 错误拼写 / Misspelling

Background

JOISC2022 D1T3

Problem Description

Once upon a time, President K had a string SS of length NN, consisting only of lowercase letters. However, he forgot it.
He also has a dictionary that contains various kinds of misspellings. He once read that dictionary, and now he is sure that SS satisfies the following condition:

  • Let TiT_i (1iN)(1\le i\le N) be the string obtained by deleting the ii-th character of SS and concatenating the remaining parts. For every jj (1jM)(1\le j\le M), it holds that TAjTBjT_{A_j} \le T_{B_j}.

Here, TAjTBjT_{A_j} \le T_{B_j} means that TAjT_{A_j} is equal to TBjT_{B_j}, or TAjT_{A_j} is lexicographically smaller than TBjT_{B_j}.

Write a program that, given the information above about SS, outputs the number of possible strings SS, modulo 109+710^9+7.

Input Format

The first line contains two positive integers N,MN, M, representing the length of SS and the number of constraints.

The following MM lines: the jj-th line (1jM)(1 \le j \le M) contains two positive integers Aj,BjA_j, B_j, representing one constraint.

Output Format

Output one line containing one non-negative integer, the number of possible strings SS modulo 109+710^9+7.

3 2
1 3
3 2
5876
5 6
1 2
1 5
2 4
5 4
5 3
4 3
656981
10 9
3 6
4 6
6 7
7 9
10 8
9 8
8 5
5 2
5 1
206289833

7 6
1 3
3 4
4 6
6 5
5 7
7 2
7125651
5 4
2 4
4 3
3 5
5 1
61451

Hint

Sample Explanation #1.

For example, if S=babS=\texttt{bab}, then $T_1 = \texttt{ab}, T_2 = \texttt{bb}, T_3 = \texttt{ba}$. It satisfies T1T3T_1 \le T_3 and T3T2T_3 \le T_2. Therefore, this SS is valid.
It can be proven that there are 58765876 valid strings SS in total. Hence, the output is 58765876.

On the other hand, if S=aabS=\texttt{aab}, then $T_1 = \texttt{ab}, T_2 = \texttt{ab}, T_3 = \texttt{aa}$. It does not satisfy T1T3T_1 \le T_3. Therefore, this SS is invalid.

This sample satisfies the constraints of all subtasks.

Sample Explanation #2.

This sample satisfies the constraints of subtasks 1,2,4,51,2,4,5.

Sample Explanation #3.

The result before taking modulo is 824206295601824\,206\,295\,601, so the output is 206289833206\,289\,833.

This sample satisfies the constraints of subtasks 1,2,4,51,2,4,5.

Sample Explanation #4.

This sample satisfies the constraints of all subtasks.

Sample Explanation #5.

This sample satisfies the constraints of all subtasks.

Constraints.

For all testdata, it holds that:

  • 2N5000002 \le N \le 500\,000.
  • 1M5000001 \le M \le 500\,000.
  • 1Aj,BjN1 \le A_j,B_j \le N (1jM)(1 \le j \le M).
  • AjBjA_j\ne B_j (1jM)(1 \le j \le M).
  • (Aj,Bj)(Ak,Bk)(A_j,B_j)\ne(A_k,B_k) (1j<kM)(1 \le j < k \le M).

The detailed additional constraints and scores for subtasks are shown in the table below:

Subtask ID Additional Constraints Score
11 N10N \le 10 88
22 N200N \le 200 2020
33 There exists a permutation PP of {1,2,,N}\{1,2,\dots,N\} such that Aj=Pj,Bj=Pj+1A_j = P_j, B_j = P_{j+1} (1jM=N1)(1 \le j \le M=N-1) 2929
44 N20000N \le 20\,000 3232
55 No additional constraints 1111

Translated by ChatGPT 5