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
// Kacper Orszulak
#ifndef DEBUG
    #define NDEBUG
#else
    // #define DEBUGPRINT
#endif

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvll = vector<vll>;
#define st first
#define nd second
#define pb push_back
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define whole(x) x.begin(), x.end()
[[maybe_unused]] constexpr int INF = 1e9+7;
template<class T> bool minR(T &a, const T &b) {
	if (b < a) { a = b; return true; }
	else return false;
}
template<class T> bool maxR(T &a, const T &b) {
	if (b > a) { a = b; return true; }
	else return false;
}
constexpr ll MOD = 1e9+7;

int n;
vector<pii> shelves;

ll binpow(ll a, ll exp) {
    a %= MOD;
    if (exp == 0)
        return 1;
    if (exp == 1)
        return a;
    if (exp % 2 == 0) {
        ll b = binpow(a, exp/2);
        return b * b % MOD;
    } else {
        return binpow(a, exp-1) * a % MOD;
    }
}

inline ll modInv(ll a) {
    return binpow(a, MOD-2);
}

ll calcFraction(ll sum) {
    ll p = sum;
    ll q = 1;
    for (ll i = 1; i <= n; ++i) q *= i;
    const ll g = gcd(p, q);
    p /= g;
    q /= g;

    p %= MOD;
    q %= MOD;
    return p * modInv(q) % MOD;
}

namespace Brute {
    vvi G;

    void genG() {
        G.resize(n);
        vi shelveAt(2*n, INF);
        rep(i, n) {
            auto &[a, b] = shelves[i];
            if (a >= 0 and shelveAt[a] != INF) {
                G[i].pb(shelveAt[a]);
            }
            if (b < 2*n and shelveAt[b] != INF) {
                G[i].pb(shelveAt[b]);
            }

            for (int j = a; j <= b; ++j) {
                shelveAt[j] = i;
            }
        }
    }

    vb visited;

    void visit(int v) {
        if (visited[v])
            return;
        visited[v] = true;
        for (const int w : G[v]) {
            visit(w);
        }
    }

    void brute() {
        genG();

        vi perm(n); rep(i, n) perm[i] = i;
        ll sum = 0;

        do {
            visited.assign(n, false);
            ll curSum = 0;
            for (const int v : perm) {
#ifdef DEBUGPRINT
                cerr << v+1 << " ";
#endif
                if (not visited[v])
                    ++curSum;
                visit(v);
            }
#ifdef DEBUGPRINT
            cerr << '\n';
            cerr << curSum << '\n';
#endif
            sum += curSum;
        } while (next_permutation(whole(perm)));

        cout << calcFraction(sum) << '\n';
    }
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

    cin >> n;
    shelves.resize(n);
    for (auto &[a, b] : shelves) {
        cin >> a >> b;
        --a; --b;
    }

#ifdef BRUTE
    Brute::brute();
#else
    Brute::brute();
#endif
}