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
// PA 2024 5B Kraniki
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;

#define QWE 1000000007

struct Shelf
{
	int L, R;
	bool flag;
	vector<int> edges;
	
	Shelf(int L, int R) {
		this->L = L;
		this->R = R;
		flag = false;
	}
};

int n, a, b;
vector<Shelf> shelves;

long long getInv(long long a, long long p) {
	long long result;
	
	if (p == 1) {
		return a;
	}
	
	if (p % 2) {
		result = getInv(a, p - 1);
		result = (result * a) % QWE;
	} else {
		result = getInv(a, p / 2);
		result = (result * result) % QWE;
	}
	
	return result;
} 

void paint(int a) {
	shelves[a].flag = true;
	for (unsigned i = 0; i < shelves[a].edges.size(); ++i) {
		if (shelves[shelves[a].edges[i]].flag == false) {
			paint(shelves[a].edges[i]);
		}
	}
}
	
long long getResultForPerm(vector<int> *perm) {
	for (int i = 0; i < n; ++i) {
		shelves[i].flag = false;
	}
	
	long long result = 0;
	for (int i = 0; i < n; ++i) {
		if (shelves[(*perm)[i]].flag == false) {
			++result;
			paint((*perm)[i]);
		}
	}
	
	return result;
}

void processAllPermutations(vector<int> *availShelves, vector<int> *perm, long long &result, long long &count)
{
	vector<int> newAvailShelves;
	
	if (availShelves->size() == 0) {
		result = (result + getResultForPerm(perm)) % QWE;
		count = (count + 1) % QWE;
		return;
	}
	
	for (unsigned i = 0; i < availShelves->size(); ++i) {
		newAvailShelves = (*availShelves);
		newAvailShelves.erase(find(newAvailShelves.begin(), newAvailShelves.end(), (*availShelves)[i]));
		perm->push_back((*availShelves)[i]);
		processAllPermutations(&newAvailShelves, perm, result, count);
		perm->pop_back();
	}		
}

int main()
{
	ios_base::sync_with_stdio(0);
	vector<int> availShelves;
	vector<int> perm;
	long long result;
	long long count;
	
	cin >> n;
	
	for (int i = 0; i < n; ++i) {
		cin >> a;
		cin >> b;
		shelves.push_back(Shelf(a, b));
		availShelves.push_back(i);
		for (int j = i - 1; j >= 0; --j) {
			if (shelves[i].L > shelves[j].L && shelves[i].L < shelves[j].R) {
				shelves[i].edges.push_back(j);
				break;
			}
		}
		for (int j = i - 1; j >= 0; --j) {
			if (shelves[i].R > shelves[j].L && shelves[i].R < shelves[j].R) {
				shelves[i].edges.push_back(j);
				break;
			}
		}
	}
	
	result = 0;
	count = 0;
	processAllPermutations(&availShelves, &perm, result, count);
	cout << (result * getInv(count, QWE - 2)) % QWE << endl;
	
	return 0;
}