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
#include <iostream>
#include <unordered_set>
#include <vector>

using namespace std;

const long long MOD = 1'000'000'000 + 7;

vector<int> factorial, inverse;

int ExpMod(int x, int n, int p) { // x ^ n % p
    long long res = 1;
    while(n > 0) {
        if(n & 1) res = ((long long)x * res) % p;
        x = ((long long)x * (long long)x) % p;
        n /= 2;
    }
    return res % MOD;
}

int binom(int n, int k) {
    return ((((long long)factorial[n] * inverse[k]) % MOD) * (long long)inverse[n-k]) % MOD;
}

struct Tree {
    vector<int> value;
    vector<int> to_set;

	Tree(int size)
		: value(size, -1), to_set(size, -1) { };

	long long get_value(int pos) {
		return value[pos + value.size() / 2];
	}

	int get(int pos, int curr_b, int curr_e, int pos_to_get) {
		if (curr_b == curr_e) {
			if (to_set[pos] != -1)
				value[pos] = to_set[pos];
			return value[pos];
		}
		if (to_set[pos] != -1) {
			to_set[pos << 1] = to_set[pos];
			to_set[(pos << 1) + 1] = to_set[pos];
		}
		if (pos_to_get <= (curr_e + curr_b) / 2)
			return get(pos << 1, curr_b, (curr_e + curr_b) / 2, pos_to_get);
		return get((pos << 1) + 1, (curr_e + curr_b) / 2 + 1, curr_e, pos_to_get);
	}


	void set_value(int pos, int value_to_set, int curr_b, int curr_e, int range_b, int range_e) {
		if(curr_b == curr_e) {
			if (to_set[pos] != -1)
				value[pos] = to_set[pos];
			to_set[pos] = -1;
			if (curr_b >= range_b && curr_e <= range_e)
				value[pos] = value_to_set;
			return;
		}
		if (to_set[pos] != -1) {
			to_set[pos << 1] = to_set[pos];
			to_set[(pos << 1) + 1] = to_set[pos];
			to_set[pos] = -1;
		}

        if (curr_b >= range_b && curr_e <= range_e) {
			to_set[pos] = value_to_set;
			return;
		}
		else if (curr_e < range_b || curr_b > range_e)
			return;

		set_value(pos << 1, value_to_set, curr_b, (curr_e + curr_b) / 2, range_b, range_e);
		set_value((pos << 1) + 1, value_to_set, (curr_e + curr_b) / 2 + 1 , curr_e, range_b, range_e);
	}
};

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);


	int n;
	cin >> n;

	vector<pair<int, int>> shelf(n);
	int pow = 1;
	while (pow < 2 * n) {
		pow *= 2;
	}
	Tree t(pow * 2);
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		if (b - a > 1)
			t.set_value(1, i, 1, pow, a + 1, b - 1);

	    shelf[i].first = t.get(1, 1, pow, a);
	    shelf[i].second = t.get(1, 1, pow, b);
	}

	vector<unordered_set<int>> paths(n);
	vector<long long> counts(n);

	for (int i = n - 1; i >= 0; i--) {
		if (shelf[i].first != -1) {
			paths[shelf[i].first].insert(i);
			for (auto it : paths[i])
				paths[shelf[i].first].insert(it);
		}
		if (shelf[i].second != -1) {
			paths[shelf[i].second].insert(i);
			for (auto it : paths[i])
				paths[shelf[i].second].insert(it);
		}
		counts[i] = paths[i].size();
	}

    factorial.resize(2 * n + 1, 1);
    inverse.resize(2 * n + 1, 1);
    for (int i = 1; i < 2 * n + 1; i++) {
        factorial[i] = (((long long)factorial[i - 1] * i)) % MOD;
        inverse[i] = ExpMod(factorial[i], MOD - 2, MOD);
    }

	long long res = 0;
	for (int i = 0; i < n; i++) {
		int k = counts[i];
		long long a = binom(n, k + 1) - binom(k, k + 1);
		a = (a * factorial[n - k - 1]) % MOD;
		a = (a * factorial[k]) % MOD;
		res = (res + a) % MOD;
	}

	cout << (res * ExpMod(factorial[n], MOD - 2, MOD)) % MOD << endl;

    return 0;
}