QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#10968. Lazy Sort

Statistics

Farmer John has $N$ cows ($2 \leq N \leq 5\cdot 10^6$) and is attempting to get them to sort a non-negative integer array $A$ of length $N$ by relying on their laziness. He has a lot of heavy boxes so he lines the cows up one behind another, where cow $i+1$ is behind cow $i$, and gives $a_i$ boxes to cow $i$ ($0\le a_i$).

Cows are inherently lazy so they always look to pass their work off to someone else. From cow $1$ to $N-1$ in order, each cow looks to the cow behind them. If cow $i$ has strictly more boxes than cow $i+1$, cow $i$ thinks this is "unfair" and gives one of its boxes to cow $i+1$. This process repeats until every cow is satisfied.

Farmer John will then note the number of boxes $b_i$ that each cow $i$ is holding and create an array $B$ out of these values. If $B = sorted(A)$, then Farmer John will be happy. Unfortunately, Farmer John forgot all but $Q$ values ($2 \leq Q \leq \min(N, 100)$) in $A$. Luckily, those values include the number of boxes he was going to give to the first and last cow. Each value that FJ remembers is given in the form $c_i \; v_i$ representing that $a_{c_i}=v_i$ ($1 \leq c_i \leq N$, $1\le v_i\le 10^9$). Determine the number of different ways the missing values can be filled in so that he will be happy mod $10^9+7$.

Input Format

The first line contains two space-separated integers $N$ and $Q$ representing the number of cows and queries respectively.

The next $Q$ lines contain two space separated integers $c_i \; v_i$ representing that cow $c_i$ initially holds $v_i$ boxes. It is guaranteed that $c_1 = 1$, $c_Q = N$, and $c_i < c_{i+1}$ (the order of the cows is strictly increasing).

Output Format

Print the number of different ways modulo $10^9+7$ that values $a_i$ can be assigned such that Farmer John will be happy after the cows perform the lazy sort. It is guaranteed that there will be at least one valid assignment.

Sample Data

Sample Input

3 2
1 3
3 2

Sample Output

2

Sample Input 2

6 3
1 1
3 3
6 5

Sample Output 2

89

Constraints

  • Inputs 3-4: $N,v_i\le 100$
  • Inputs 5-6: $N\le 100$ and $v_i \leq 10^6$
  • Inputs 7-9: $N \leq 2\cdot 10^5$ and $v_i \le 10^6$
  • Inputs 10-12: $N \leq 2\cdot 10^5$
  • Inputs 13-15: No additional constraints.
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.