Lazy Sort

시간 제한2초메모리 제한2048 MB

요약
최대 100개의 위치가 주어진 배열에서, 상자를 뒤로 넘기는 게으른 과정이 정렬된 배열을 만들도록 나머지 값을 채우는 경우의 수를 센다.
난이도

어려움10점 중 9점

유형
동적 계획법, 조합론, 수학, 그리디
정답자
아직 제출이 없습니다

문제

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

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

Farmer John will then note the number of boxes b_ib\_i that each cow ii is holding and create an array BB out of these values. If B=sorted(A)B = sorted(A), then Farmer John will be happy. Unfortunately, Farmer John forgot all but QQ values (2≤Q≤min⁡(N,100)2 \leq Q \leq \min(N, 100)) in AA. 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_iv_ic\_i v\_i representing that a_c_i=v_ia\_{c\_i}=v\_i (1≤c_i≤N1 \leq c\_i \leq N, 1≤v_i≤1091\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 109+710^9+7.

입력

The first line contains two space-separated integers NN and QQ representing the number of cows and queries respectively.

The next QQ lines contain two space separated integers c_iv_ic\_i v\_i representing that cow c_ic\_i initially holds v_iv\_i boxes. It is guaranteed that c_1=1c\_1 = 1, c_Q=Nc\_Q = N, and c_i<c_i+1c\_i < c\_{i+1} (the order of the cows is strictly increasing).

출력

Print the number of different ways modulo 109+710^9+7 that values a_ia\_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.

예제2

  1. 예제 1

    입력
    3 2
    1 3
    3 2
    
    예상 출력
    2
    
  2. 예제 2

    입력
    6 3
    1 1
    3 3
    6 5
    
    예상 출력
    89