There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has $n$ different matryoshkas. Any matryoshka is a figure of volume $out_i$ with an empty space inside of volume $in_i$ (of course, $out_i > in_i$).

You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll — inside the third one and so on. Matryoshka $i$ can be nested inside matryoshka $j$ if $out_i \le in_j$. So only the last doll will take space inside your bag.

Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to $in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + \dots + (in_{i_k} - out_{i_{k-1}})$, where $i_1$, $i_2$, ..., $i_k$ are the indices of the chosen dolls in the order they are nested in each other.

Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property.

You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index $i$ such that one of the subsets contains the $i$-th doll, and another subset doesn't.

Since the answer can be large, print it modulo $10^9 + 7$.


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of matryoshkas.

The next $n$ lines contain a description of each doll: two integers $out_i$ and $in_i$ ($1 \le in_i < out_i \le 10^9$) — the outer and inners volumes of the $i$-th matryoshka.


-----Output-----

Print one integer — the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo $10^9 + 7$.


-----Example-----
Input
7
4 1
4 2
4 2
2 1
5 4
6 4
3 2

Output
6



-----Note-----

There are $6$ big enough nested subsets with minimum possible extra space in the example:   $\{1, 5\}$: we can't add any other matryoshka and keep it nested; it's extra space is $1$;  $\{1, 6\}$;  $\{2, 4, 5\}$;  $\{2, 4, 6\}$;  $\{3, 4, 5\}$;  $\{3, 4, 6\}$. 

There are no more "good" subsets because, for example, subset $\{6, 7\}$ is not big enough (we can add the $4$-th matryoshka to it) or subset $\{4, 6, 7\}$ has extra space equal to $2$.

Difficulty: HARD
Tags: Shortest paths

Sorting

Combinatorics

Dynamic programming

Data structures
Skill types: Dynamic programming

Sorting

Data structures

Starter code:

Solutions:
mod = 1000000007
eps = 10 ** (-9)

def main():
	import sys
	input = sys.stdin.buffer.readline
	N = int(input())
	A = []
	B = []
	val = set()
	val.add(0)
	a_max = -1
	for _ in range(N):
		(a, b) = map(int, input().split())
		(a, b) = (b, a)
		A.append(a)
		B.append(b)
		val.add(a)
		val.add(b)
		a_max = max(a_max, a)
	val = sorted(list(val))
	val2idx = {v: i for (i, v) in enumerate(val)}
	NN = len(val)
	adj = [[] for _ in range(NN)]
	for i in range(NN - 1):
		adj[i].append((i + 1, val[i + 1] - val[i]))
	for (a_, b_) in zip(A, B):
		a = val2idx[a_]
		b = val2idx[b_]
		adj[a].append((b, 0))
	dist = [10 ** 10] * NN
	dist[0] = 0
	for i in range(NN - 1):
		for (b, cost) in adj[i]:
			dist[b] = min(dist[b], dist[i] + cost)
	min_space = 10 ** 10
	B_set = set(B)
	for b in B_set:
		if b <= a_max:
			continue
		ib = val2idx[b]
		min_space = min(min_space, dist[ib])
	dp = [0] * NN
	dp[0] = 1
	for i in range(NN - 1):
		for (b, cost) in adj[i]:
			if dist[i] + cost == dist[b]:
				dp[b] = (dp[b] + dp[i]) % mod
	ans = 0
	for b in B_set:
		if b <= a_max:
			continue
		ib = val2idx[b]
		if dist[ib] == min_space:
			ans = (ans + dp[ib]) % mod
	print(ans)
main()


import io, sys
input = lambda f=io.StringIO(sys.stdin.buffer.read().decode()).readline: f().rstrip()
from bisect import bisect_left as lb
MOD = 10 ** 9 + 7
n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
a = sorted(((in_, out) for (out, in_) in a))
dp = [None] * n
dp_min = [None] * n
for i in range(n - 1, -1, -1):
	(in_, out) = a[i]
	j = lb(a, (out, 0))
	if j == n:
		dp[i] = (in_, 1)
	else:
		(empty, count) = dp_min[j]
		dp[i] = (empty - out + in_, count)
	dp_min[i] = list(dp[i])
	if i < n - 1:
		if dp_min[i + 1][0] < dp_min[i][0]:
			dp_min[i] = dp_min[i + 1]
		elif dp_min[i + 1][0] == dp_min[i][0]:
			dp_min[i][1] += dp_min[i + 1][1]
	dp_min[i][1] %= MOD
print(dp_min[0][1])


import sys
input = sys.stdin.readline
from bisect import bisect_right as br
P = 10 ** 9 + 7
N = int(input())
X = []
maxinn = 0
for _ in range(N):
	(a, b) = list(map(int, input().split()))
	maxinn = max(maxinn, b)
	X.append((a, b))
X = sorted(X)
OUT = [0]
VOL = [0]
CNT = [1]
for (out, inn) in X:
	i = br(OUT, inn) - 1
	vol = VOL[i] + out - inn
	if OUT[-1] != out:
		OUT.append(out)
		VOL.append(VOL[-1] if len(CNT) > 1 else 0)
		CNT.append(CNT[-1] if len(CNT) > 1 else 0)
	if VOL[-1] < vol:
		VOL[-1] = vol
		CNT[-1] = CNT[i]
	elif VOL[-1] == vol:
		CNT[-1] += CNT[i]
		CNT[-1] %= P
mi = min([OUT[i] - VOL[i] for i in range(len(CNT)) if OUT[i] > maxinn])
print(sum([CNT[i] for i in range(len(CNT)) if OUT[i] > maxinn and OUT[i] - VOL[i] == mi]) % P)


from bisect import bisect_left as lb
MOD = 10 ** 9 + 7
n = int(input())
a = [tuple(map(int, input().split())) for _ in range(n)]
a = sorted(((in_, out) for (out, in_) in a))
dp_suf = [None] * n
for i in range(n - 1, -1, -1):
	(in_, out) = a[i]
	j = lb(a, (out, 0))
	if j == n:
		(empty, count) = (in_, 1)
	else:
		(empty, count) = dp_suf[j]
		empty -= out - in_
	if i < n - 1:
		if empty > dp_suf[i + 1][0]:
			(empty, count) = dp_suf[i + 1]
		elif empty == dp_suf[i + 1][0]:
			count += dp_suf[i + 1][1]
	dp_suf[i] = (empty, count % MOD)
print(dp_suf[0][1])


from sys import stdin, stdout
mod = 10 ** 9 + 7
n = int(input())
dolls = []
for i in range(n):
	(o, i) = map(int, stdin.readline().split())
	dolls.append((o, i))
dolls.sort()
dolls = [(i, o) for (o, i) in dolls]

def bin_search(i):
	lo = -1
	hi = n - 1
	while lo + 1 < hi:
		mid = (lo + hi - 1) // 2 + 1
		top = dolls[mid][1]
		if top <= i:
			lo = mid
		else:
			hi = mid
	return hi
counts = [1] * (n + 1)
dp = [0] * (n + 1)
for k in range(n):
	(i, o) = dolls[k]
	m_prev = dp[k] + o - (dolls[k - 1][1] if k > 0 else 0)
	kk = bin_search(i)
	m_with = dp[kk] + i - (dolls[kk - 1][1] if kk > 0 else 0)
	if m_prev < m_with:
		dp[k + 1] = m_prev
		counts[k + 1] = counts[k]
	elif m_prev > m_with:
		dp[k + 1] = m_with
		counts[k + 1] = counts[kk]
	else:
		dp[k + 1] = m_prev
		counts[k + 1] = (counts[k] + counts[kk]) % mod
best = 10 ** 9 + 10
best_count = 0
maximal = max([i for (i, o) in dolls])
for i in range(bin_search(maximal), n):
	ii = bin_search(dolls[i][0])
	cur = dp[ii] + dolls[i][0] - (dolls[ii - 1][1] if ii > 0 else 0)
	if cur < best:
		best = cur
		best_count = counts[ii]
	elif cur == best:
		best_count += counts[ii]
		best_count %= mod
print(best_count)