#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 (li,hi)(l_i, h_i) and (ri,hi)(r_i, h_i). The faucet installed by the plumber is the point with coordinates (li+ri2,hi+0.5)(\frac{l_i+r_i}{2}, h_i + 0.5). The floor of the room is represented by the OXOX axis.

As soon as the faucet above shelf ii 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 ii, he will open it if and only if shelf ii 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 n!n! 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 109+710^9+7. Formally, let the answer be pq\frac{p}{q}, where q0q\neq 0 and gcd(p,q)=1\gcd(p,q)=1. You need to output pq1(mod109+7)p\cdot q^{-1}\pmod{10^9+7}, where q1q^{-1} is the unique integer in the set 1,2,,109+61,2,\ldots,10^9+6 such that qq11(mod109+7)q\cdot q^{-1}\equiv 1\pmod{10^9+7}.

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 109+710^9+7.

Input Format

The first line contains an integer n (1n500000)n\ (1\le n\le 500\,000), the number of shelves in Mati’s company.

The next nn lines each contain two integers li,ri (1li<ri2n)l_i,r_i\ (1\le l_i<r_i\le 2\cdot n) describing shelf ii. For simplicity, assume hi=ih_i=i.

You may assume that all lil_i and rir_i are pairwise distinct: the integers li,ril_i,r_i form a permutation of 11 to 2n2\cdot n.

Output Format

Output one integer: the expected number of faucets Radek needs to open, taken modulo 109+710^9+7.

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 1,2,31,2,3, he will open all 33 faucets.
  • In the order 1,3,21,3,2, he will first open faucets 11 and 33. When he is about to open faucet 22, shelf 22 has already been flooded, so he does not need to open faucet 22.
  • In the order 2,1,32,1,3, he will open all 33 faucets.
  • In the order 2,3,12,3,1, he will first open faucets 22 and 33. When he is about to open faucet 11, shelf 11 has already been flooded, so he does not need to open faucet 11.
  • In the order 3,1,23,1,2 or 3,2,13,2,1, after opening faucet 33, all shelves will be flooded, so there is no need to open the remaining two faucets.

Radek needs to open 16(3+2+3+2+1+1)=2\frac{1}{6}\cdot(3+2+3+2+1+1)=2 faucets on average.

Sample Explanation 2

In the second sample, Radek needs to open 9130\frac{91}{30} faucets on average. Since 301233333335(mod109+7)30^{-1}\equiv 233333335\pmod{10^9+7}, the output is $91\cdot 233333335\equiv 21233333485\equiv 233333338\pmod{10^9+7}$.

Translated by ChatGPT 5