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
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using vi = vector<int>;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using pull = pair<ull, ull>;
using vll = vector<ll>;
using ii = pair<int, int>;

int MOD = 1e9 + 7;
const int BSN = 30000;
using bs = bitset<BSN>;

const int N = 5e5 + 7;
ll sil[N];
ll osil[N];
bs vis[BSN];

ll modpow(ll a, ll b)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
        {
            res = res * a % MOD;
        }
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

ll binom(int n, int k)
{
    if (k < 0 || k > n)
    {
        return 0;
    }
    return sil[n] * osil[k] % MOD * osil[n - k] % MOD;

}

void prep()
{
    sil[0] = 1;
    for (int i = 1; i < N; i++)
    {
        sil[i] = 1ll * sil[i - 1] * i % MOD;
    }
    osil[N - 1] = modpow(sil[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; i--)
    {
        osil[i] = 1ll * osil[i + 1] * (i + 1) % MOD;
    }
    // assert(1ll * osil[102423] * sil[102423] % MOD == 1);
}


void solve()
{
    prep();
    int n;
    cin >> n;

    vector<ii> input(n);

    for (auto & u : input)
    {
        cin >> u.f >> u.s;
    }
    reverse(all(input));

    vector<vi> G(n);
    
    set<ii> points;

    for (int i = 0; i < sz(input); i++)
    {
        int pocz = input[i].f;
        int kon = input[i].s;

        auto it = points.lower_bound({pocz, 0});
        while (it != points.end() && it->f < kon)
        {
            auto temp = it;
            G[i].pb(it->s);
            ++it;
            points.erase(temp);
        }
        points.insert({pocz, i});
        points.insert({kon, i});
    }

    function<void(int)> dfs = [&](int start)
    {
        vis[start][start] = 1;
        for (auto u : G[start])
        {
            if (!vis[start][u])
            {
                dfs(u);
                vis[start] |= vis[u];
            }
        }
    };

    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        if (!vis[i][i])
            dfs(i);
        int ile = vis[i].count();
        ans = (ans + 1ll * binom(n, ile) * sil[ile - 1] % MOD * sil[n - ile]) % MOD;
    }
    cout << 1ll * ans * osil[n] % MOD << '\n';
}   

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

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++)
    {
        solve();
    }

    return 0;
}