#P10365. [PA 2024] Kraniki
[PA 2024] Kraniki
Background
PA 2024 5B2
Problem Description
This problem is translated from PA 2024 Round 5 Kraniki.
Radek, the boss of Radek and Friends, is trying to flood all shelves of the competing company Mati and Company. To carry out the perfect sabotage, he asked his friend, the plumber Janusz, to install a small faucet above each shelf.
For simplicity, the shelves in Mati and Company can be represented as line segments on the plane. Each shelf is the segment between the points and . The faucet installed by the plumber is the point with coordinates . The floor of the room is represented by the axis.
As soon as the faucet above shelf is opened, that shelf will be flooded. Then the water will naturally start dripping vertically downward from both ends of the shelf, and it may flood more shelves, or drip down to the floor.

In the second sample, a visualization of the water flow after opening one faucet.
Radek will inspect the faucets in some fixed order. When he sees faucet , he will open it if and only if shelf has not been flooded yet.
Radek has not decided the order in which he will inspect the faucets. He will choose one uniformly at random among the possible orders. Radek wants to know the expected number of faucets he will open.
Your task is to answer Radek’s question and output the value modulo . Formally, let the answer be , where and . You need to output , where is the unique integer in the set such that .
It can be proven that for all testdata satisfying the constraints of the problem, the result is a rational number, and its denominator is not divisible by .
Input Format
The first line contains an integer , the number of shelves in Mati’s company.
The next lines each contain two integers describing shelf . For simplicity, assume .
You may assume that all and are pairwise distinct: the integers form a permutation of to .
Output Format
Output one integer: the expected number of faucets Radek needs to open, taken modulo .
3
4 6
1 3
2 5
2
5
2 9
3 4
1 8
6 10
5 7
233333338
Hint
Sample Explanation 1
Let us consider all orders in which Radek may inspect the faucets in the first sample:
- In the order , he will open all faucets.
- In the order , he will first open faucets and . When he is about to open faucet , shelf has already been flooded, so he does not need to open faucet .
- In the order , he will open all faucets.
- In the order , he will first open faucets and . When he is about to open faucet , shelf has already been flooded, so he does not need to open faucet .
- In the order or , after opening faucet , all shelves will be flooded, so there is no need to open the remaining two faucets.
Radek needs to open faucets on average.
Sample Explanation 2
In the second sample, Radek needs to open faucets on average. Since , the output is $91\cdot 233333335\equiv 21233333485\equiv 233333338\pmod{10^9+7}$.
Translated by ChatGPT 5