#P10027. 梦境世界
梦境世界
Background
Doremi loves traveling.
Problem Description
There is a grid of length and width . Its top-left corner is position , and its bottom-right corner is position . Doremi wants to walk from the top-left corner to the bottom-right corner. Each time, she can move one cell to the right or one cell downward, but she cannot go out of the grid boundary. Besides, there are forbidden cells that Doremi cannot step on.
Doremi has magic potions. Drinking a potion can undo the last not-yet-undone walking move, but the move sequence will not delete its last element. Of course, she cannot use a potion at position , and a potion will not undo the effect of the previous potion. For example, after moving from to , if she drinks a potion at , then the move sequence is . If she walks from to and then to , and drinks two potions consecutively, then the move sequence is .
Doremi considers a trip to be a walking route that finally reaches . She wants to find the number of essentially different trips, modulo the given . She considers two trips to be different if and only if the recorded move sequences are different.
Input Format
The first line contains five integers , with meanings as described above.
The next lines each contain two integers , meaning that the cell labeled in the grid cannot be passed through.
Output Format
Output one integer in one line, representing the answer modulo .
2 2 2 998244353 1
1 2
7
5 5 0 114514 0
70
5 5 3 998244353 3
3 4
2 5
4 4
13782
Hint
Sample Explanation 1
The seven routes are as follows:

Constraints
This problem uses bundled tests.
- Subtask 0 (10 pts): , .
- Subtask 1 (15 pts): , .
- Subtask 2 (15 pts): , , .
- Subtask 3 (25 pts): , .
- Subtask 4 (35 pts): No special restrictions.
For all testdata, it is guaranteed that , , , , , , and all are distinct.
Translated by ChatGPT 5