Kraniki
Time limit4sMemory limit1024 MB
Given n shelves at heights 1 to n, each with endpoints l_i and r_i, find the expected number of taps opened over a random order, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Intervals, Stack, Probability
- Solved
- No attempts yet
Statement
Radek, the boss of the firm Radek and Friends, wants to flood every document shelf at the rival company Mati and Partners. For a perfect sabotage, he asked his friend, the plumber Janusz, to install small water taps above each shelf.
The shelves are segments in the plane. Shelf is the segment between points and . The tap above shelf is placed at . The floor is the axis.
When the tap above shelf is opened, that shelf is flooded. Water then drips straight down from the ends of the flooded shelves. It may flood shelves below, or drip onto the floor, which has a natural drainage system.

Radek considers the taps in a fixed order. When he considers tap , he opens it only if shelf is not flooded yet.
Radek has not chosen the order yet. He picks one of the orders uniformly at random. Find the expected number of taps Radek has to open.
Input
The first line contains one natural number (), the number of shelves at Mati and Partners.
Each of the next lines describes one shelf. Line contains two natural numbers and (). Here .
All numbers and are pairwise distinct. Together they form a permutation of to .
Output
Print one number: the expected number of taps Radek has to open, modulo . Formally, let the answer be with and . Print , where is the unique integer from to such that . The denominator of the reduced answer is proven not to be divisible by .
Hint
In the first test, the average is . For order 1, 3, 2, Radek opens taps 1 and 3. After tap 3 is opened, shelf 2 is also flooded, so he skips tap 2. For orders 3, 1, 2 and 3, 2, 1, opening tap 3 floods every shelf, so no other tap is opened.