Problem:
Score : 1200 points

Problem Statement

Takahashi is an expert of Clone Jutsu, a secret art that creates copies of his body.

On a number line, there are N copies of Takahashi, numbered 1 through N.
The i-th copy is located at position X_i and starts walking with velocity V_i in the positive direction at time 0.

Kenus is a master of Transformation Jutsu, and not only can he change into a person other than himself, but he can also transform another person into someone else.

Kenus can select some of the copies of Takahashi at time 0, and transform them into copies of Aoki, another Ninja.
The walking velocity of a copy does not change when it transforms.
From then on, whenever a copy of Takahashi and a copy of Aoki are at the same coordinate, that copy of Takahashi transforms into a copy of Aoki.

Among the 2^N ways to transform some copies of Takahashi into copies of Aoki at time 0, in how many ways will all the copies of Takahashi become copies of Aoki after a sufficiently long time?
Find the count modulo 10^9+7.

Constraints

1 ≤ N ≤ 200000

1 ≤ X_i,V_i ≤ 10^9(1 ≤ i ≤ N)

X_i and V_i are integers.

All X_i are distinct.

All V_i are distinct.

Input

The input is given from Standard Input in the following format:

N
X_1 V_1
:
X_N V_N

Output

Print the number of the ways that cause all the copies of Takahashi to turn into copies of Aoki after a sufficiently long time, modulo 10^9+7.

Sample Input 1

3
2 5
6 1
3 7

Sample Output 1

6

All copies of Takahashi will turn into copies of Aoki after a sufficiently long time if Kenus transforms one of the following sets of copies of Takahashi into copies of Aoki: (2), (3), (1,2), (1,3), (2,3) and (1,2,3).

Sample Input 2

4
3 7
2 9
8 16
10 8

Sample Output 2

9

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	long MODULO = 1_000_000_000 + 7;

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] e = new int[n][3];
		for (int i = 0; i < n; ++i) {
			e[i][0] = sc.nextInt();// x
			e[i][1] = sc.nextInt();// v
			e[i][2] = -1;
		}
		Arrays.sort(e, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[1], o2[1]);
			}
		});
		for (int i = 0; i < n; ++i) {
			e[i][2] = i;
		}

		Arrays.sort(e, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[0], o2[0]);
			}
		});

		int[][] min = new int[n][2];
		int[][] max = new int[n][2];
		for (int i = 0; i < n; ++i) {
			min[i][0] = Integer.MAX_VALUE / 16;
			max[i][0] = -Integer.MAX_VALUE / 16;
			min[i][1] = -1;
			max[i][1] = -1;
		}
		for (int i = 0; i < n; ++i) {
			if (i == 0 || max[i - 1][0] < e[i][1]) {
				max[i][0] = e[i][1];
				max[i][1] = e[i][2];
			} else {
				max[i][0] = max[i - 1][0];
				max[i][1] = max[i - 1][1];
			}
		}
		for (int i = n - 1; i >= 0; --i) {
			if (i == n - 1 || min[i + 1][0] > e[i][1]) {
				min[i][0] = e[i][1];
				min[i][1] = e[i][2];
			} else {
				min[i][0] = min[i + 1][0];
				min[i][1] = min[i + 1][1];
			}
		}

		long[] f = new long[n];
		long s = 0;
		int t = 0;
		for (int i = 0; i < n; ++i) {
			while (max[t][1] < min[i][1]) {
				s = (s - f[t] + MODULO) % MODULO;
				++t;
			}
			f[i] = s;
			if (min[i][1] == min[0][1]) {
				++f[i];
			}
			s += f[i];
			s %= MODULO;
			f[i] %= MODULO;
		}
		long ans = 0;
		for (int i = 0; i < n; ++i) {
			if (max[n - 1][1] == max[i][1]) {
				ans += f[i];
			}
			while (ans >= MODULO) {
				ans -= MODULO;
			}
		}
		System.out.println(ans);
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}