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
146
147
148
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set>

int c;

#define SKIP_WHITESPACE \
{   \
    while (1) { \
        c = fgetc(stdin);   \
        if (c != ' ' && c != '\n' && c != '\r') \
            break;  \
    }   \
}   \

#define READ_INT    \
({   \
    SKIP_WHITESPACE \
    int ret = c - '0';  \
    while (1) { \
        c = fgetc(stdin);   \
        if (c < '0' || c > '9') \
            break;  \
        ret = ret * 10 + c - '0';   \
    }   \
    ret; \
})   \

#define MOD 1000000007ULL

uint64_t inv(uint64_t inva) {
  return inva <= 1 ? inva : MOD - (uint64_t)(MOD / inva) * inv(MOD % inva) % MOD;
}

struct edge {
    int par;
    int next;
};

struct node {
    int count;
    int edges;
    int visited;
};

int main(int argc, char* argv[]) {
    std::ios_base::sync_with_stdio (false);
    int i, n, a, b, w;

    n = READ_INT;

    std::vector<int> ends(2 * n);
    std::set<int> active;
    std::set<int>::iterator ai, pai;
    std::vector<edge> edges(2 * n);
    int ei = 0;
    std::vector<node> nodes(n, {0, -1, -1});

    for (i = 0; i < n; ++i) {
        a = READ_INT - 1;
        b = READ_INT - 1;
        ends[a] = i;
        ends[b] = i;
    }

    for (i = 0; i < 2 * n; ++i) {
        a = ends[i];
        ai = active.find(a);
        if (ai == active.end()) {
            //insert
            ai = active.insert(a).first;
            if (ai != active.begin()) {
                pai = ai;
                pai--;
                b = *pai;

                edges[ei].next = nodes[a].edges;
                nodes[a].edges = ei;
                edges[ei].par = b;
                ei++;
            }
        } else {
            //removal
            if (ai != active.begin()) {
                pai = ai;
                pai--;
                b = *pai;
                active.erase(a);

                edges[ei].next = nodes[a].edges;
                nodes[a].edges = ei;
                edges[ei].par = b;
                ei++;
            } else {
                active.erase(a);
            }
        }
    }

    // for (i = 0; i < n; ++i) {
    //     std::cout << "node: " << i << "; count=" << nodes[i].count << "; edges={";
    //     w = nodes[i].edges;
    //     while (w != -1) {
    //         std::cout << edges[w].par << ", ";
    //         w = edges[w].next;
    //     }
    //     std::cout << "}\n";
    // }
    // std::cout << "ends: {";
    // for (i = 0; i < 2 * n; ++i) {
    //     std::cout << ends[i] << ", ";
    // }
    // std::cout << "}\n";

    std::vector<int> stack(n);
    int si = 0;
    for (i = 0; i < n; ++i) {
        //dfs from node i
        stack[0] = i;
        si = 1;

        while (si > 0) {
            si--;
            a = stack[si];
            w = nodes[a].edges;
            while (w != -1) {
                b = edges[w].par;
                if (nodes[b].visited < i) {
                    nodes[b].visited = i;
                    nodes[b].count++;
                    stack[si++] = b;
                }
                w = edges[w].next;
            }
        }
    }
    uint64_t ret = 0ULL;
    for (i = 0; i < n; ++i) {
        ret = (ret + inv(1ULL + nodes[i].count)) % MOD;
    }
    std::cout << ret;
    std::cout << "\n";

    return EXIT_SUCCESS;
}