Mike wants to prepare in IMO but he doesn ' t know geometry, so he teacher gave him an interesting geometry problem. Let's DEFINEF ([L, r]) = R-l + 1 To is the number of the integer points in the Segment[l, R] with L≤r (say that). You are given the INTEGERSN and K andn closed intervals [Li, RI] Onox axis and you had to find:
In other words, you should find the sum of the number of the integer points in the intersection of Anyk of the segments.
As the answer may very large, output it modulo 1000000007 (109 + 7).
Mike can ' t solve this problem so he needs your help. You'll help him, won ' t? Input
The first line contains II integers n andk (1≤k≤n≤200)-the number of segments and the number of segments in I Ntersection groups respectively.
Then n lines follow, the i-th line contains the integers li, ri (-109≤li≤ri≤109), describingi-th segment bounds. Output
Print One integer number-the answer to Mike's problem modulo 1000000007 (109 + 7) in the-line. Examples Input
3 2
1 2
1 3
2 3
Output
5
Input
3 3
1 3
1 3
1 3
Output
3
Input
3 1
1 2
2 3
3 4
Output
6
Note
In the first example:
;
;
.
The answer is 2 + 1 + 2 = 5.
Test instructions: Give you the closed interval on the axis of N, and then choose the K-interval to find the intersection, ask you all the length of the intersection and how much.
Analysis: Discretization directly after a paragraph to seek its contribution to the answer.
#include <cstdio> #include <queue> #include <vector> #include <cstdio> #include <ut ility> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f
3F3F #define MOD 1000000007 using namespace std;
int tot,n,k,l[200007],r[200007],lin[400007],rout[400007];
Long Long F[200007],g[400007],ans;
Long long X,long long y) {long long ans = 1;
while (y) {if (Y & 1) ans = (ans * x)% MOD;
x = x*x% MOD;
y>>=1;
} return ans;
} int main () {Cin.sync_with_stdio (false);
cin>>n>>k;
F[k] = 1ll;
for (int i = k+1;i <= n;i++) f[i] = ((f[i-1]*i)% MoD) *ksm (i-k,mod-2)% MoD;
for (int i = 1;i <= n;i++) {cin>>l[i]>>r[i];
G[++tot] = L[i];
G[++tot] = ++r[i];
} sort (G+1,g+1+tot);
for (int i = 1;i <= n;i++) {lin[lower_bound (g+1,g+1+tot,l[i])-g]++;
Rout[lower_bound (G+1,g+1+tot,r[i])-g]++;
} int now = 0; for (int i = 1;i < tot;i++) {now + = Lin[i]-rout[i];
Ans = (ans + (g[i+1]-g[i]) *f[now])% MOD;
} cout<<ans<<endl; }