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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 500 * 1000 + 1;
ll sil[N], osil[N];
int il[N];
ull dp[N];
vector<int> ed[N];
set<pair<int, int>> curl;
pair<int, int> tab[N];
set<pair<int, int>>::iterator it;

ll QP(ll a, ll n)
{
    ll ans = 1LL;
    while(n > 0LL)
    {
        if(n % 2LL == 1LL) ans = (ans * a) % M;
        a = (a * a) % M;
        n /= 2LL;
    }
    return ans;
}

void PreSil(int n)
{
    sil[0] = 1LL;
    for(int i = 1; i <= n; ++i) sil[i] = (sil[i - 1] * (ll)i) % M;
    osil[n] = QP(sil[n], M - 2LL);
    for(int i = n - 1; i >= 0; --i) osil[i] = (osil[i + 1] * (ll)(i + 1)) % M;
}

inline ll NK(int n, int k)
{
    return (((sil[n] * osil[k]) % M) * osil[n - k]) % M;
}

ll Cnt(int n, int k)
{
    ll ans = NK(n, k + 1);
    ans *= (sil[k] * sil[n - k - 1]) % M;
    ans %= M;
    return ans;
}

void GenIN(int n)
{
    srand(clock());
    for(int i = 1; i <= n; ++i)
    {
        int a = rand() % (2 * n) + 1, b = rand() % (2 * n) + 1;
        tab[i].st = min(a, b);
        tab[i].nd = max(a, b); 
    }
}

void CntIL(int n)
{
    int s;
    for(int l = 0; l < (n + 63) / 64; ++l)
    {
        s = l * 64 + 1;
        for(int i = s; i < s + 64; ++i)
            dp[i] = ((ull)1<<(ull)(i - s));
        for(int i = s; i <= n; ++i)
        {
            il[i] += __builtin_popcountll(dp[i]);
            for(int j = 0; j < (int)ed[i].size(); ++j)
                dp[ed[i][j]] |= dp[i];
            dp[i] = 0;
        }
    }
}

void Solve()
{
    int n; ll ans = 0;
    cin >> n;
    //n = 100;
    //GenIN(n);
    PreSil(n);
    for(int i = 1; i <= n; ++i)
        cin >> tab[i].st >> tab[i].nd;
    reverse(tab + 1, tab + 1 + n);
    for(int i = 1; i <= n; ++i)
    {
        it = curl.lower_bound(make_pair(tab[i].st, 0));
        while(it != curl.end() && (*it).st <= tab[i].nd)
        {
            ed[(*it).nd].pb(i);
            curl.erase(it);
            it = curl.lower_bound(make_pair(tab[i].st, 0));
        }
        curl.insert(make_pair(tab[i].st, i));
        curl.insert(make_pair(tab[i].nd, i));
    }
    CntIL(n);
    for(int i = 1; i <= n; ++i)
        ans += Cnt(n, il[i] - 1);
    ans %= M;
    ans *= osil[n];
    cout << ans % M << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

    return 0;
}