1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <cstdio>
#include <cstdint>
#include <vector>
#include <stack>

using namespace std;

const int64_t MOD = 1000000007;

int64_t ext_euclid(int64_t a, int64_t b, int64_t &x, int64_t &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    int64_t x1, y1;
	int64_t gcd = ext_euclid(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return gcd;
}

int64_t inverse(int64_t a) {
    int64_t x, y;
    ext_euclid(a, MOD, x, y);
    return x;
}

const int TREE_SIZE = 1 << 20;

typedef struct {
	int start;
	int end;
	int value;
} node_t;

inline int parent(int n) {
	return n >> 1;
}

inline int lson(int n) {
	return n << 1;
}

inline int rson(int n) {
	return (n << 1) + 1;
}

void set_value(vector<node_t>& tree, int start, int end, int value) {
	stack<int> st;
	st.push(1);
	while (!st.empty()) {
		int i = st.top();
		st.pop();
		int node_start = tree[i].start;
		int node_end = tree[i].end;
		if ((start <= node_start) && (end >= node_end)) {
			tree[i].value = value;
			continue;
		}
		if ((start > node_end) || (end < node_start)) {
			continue;
		}
		if (tree[i].value != -1) {
			tree[lson(i)].value = tree[i].value;
			tree[rson(i)].value = tree[i].value;
		}
		tree[i].value = -1;
		st.push(lson(i));
		st.push(rson(i));
	}
}

int get_value(vector<node_t>& tree, int n) {
	int idx = 1;
	while (true) {
		if (tree[idx].value != -1) {
			return tree[idx].value;
		}
		if (tree[idx].start == tree[idx].end) {
			return -1;
		}
		if ((n << 1) < tree[idx].start + tree[idx].end) {
			idx = lson(idx);
		}
		else {
			idx = rson(idx);
		}
	}
	return -1;
}

int main () {
	vector<node_t> tree(TREE_SIZE << 1);
	int segment_length = TREE_SIZE;
	int next_start = 0;
	for (int i = 1; i < TREE_SIZE << 1; ++i) {
		tree[i].start = next_start;
		tree[i].end = next_start + segment_length - 1;
		tree[i].value = -1;
		next_start += segment_length;
		if (next_start >= TREE_SIZE) {
			next_start = 0;
			segment_length >>= 1;
		}
	}
	int n;
	scanf("%d", &n);
	vector<vector<int> > graph(n, vector<int>());
	for (int i = 0; i < n; ++i) {
		int a, b;
		scanf("%d %d", &a, &b);
		int left_fall = get_value(tree, a);
		if (left_fall != -1) {
			graph[left_fall].push_back(i);
		}
		int right_fall = get_value(tree, b);
		if ((right_fall != -1) && (right_fall != left_fall)) {
			graph[right_fall].push_back(i);
		}
		set_value(tree, a, b, i);
	}
	int64_t res = 0;
	for (int i = 0; i < n; ++i) {
		vector<bool> visited(n, false);
		stack<int> st;
		st.push(i);
		int k = 0;
		while (!st.empty()) {
			int j = st.top();
			st.pop();
			if (visited[j]) {
				continue;
			}
			++k;
			visited[j] = true;
			for (vector<int>::iterator it = graph[j].begin(); it != graph[j].end(); it++) {
				st.push(*it);
			}
		}
		res = (res + inverse(k)) % MOD;
	}
	while (res < 0) {
		res += MOD;
	}
	printf("%lld\n", res);
	return 0;
}