Lantern Sparks
Pondo has strung n lanterns together with m wires. Lantern i independently chooses an integer brightness uniformly at random from the inclusive interval [li,ri].
A wire connecting lanterns u and v produces a spark if at least one of the two brightnesses is divisible by a fixed integer p. Let X be the number of wires that spark.
Output the expected value of X.
It can be shown that the answer can be expressed as a rational number P/Q in lowest terms with Q coprime to 109+7. Output P⋅Q−1mod(109+7).
Input
The first line contains three integers n, m, and p.
Each of the next n lines contains two integers li and ri.
Each of the next m lines contains two integers u and v, describing a wire between lanterns u and v.
Lanterns are numbered 1 through n. The wires form an undirected graph. There are no self-loops, but multiple wires between the same pair of lanterns are allowed; each wire is counted separately.
Output
Print a single integer: the expected number of sparks modulo 109+7.
Constraints
- 1≤n≤105
- 0≤m≤105
- 1≤p≤109
- 1≤li≤ri≤109
- 1≤u,v≤n
- u=v
Example 1
3 3 2
1 2
3 4
5 6
1 2
2 3
3 1
250000004
Explanation
Each lantern has brightness even with probability 1/2. A wire sparks unless both endpoints are odd, which happens with probability 1/4, so each wire sparks with probability 3/4. The three wires contribute 9/4 in expectation, and 9⋅4−1≡250000004(mod109+7).
The three spark events are dependent because they share lanterns, but the expectation of the sum is still the sum of the expectations.
Example 2
2 1 3
1 3
1 2
1 2
333333336
Explanation
Lantern 1 fails to be divisible by 3 with probability 2/3, and lantern 2 never chooses a multiple of 3. The single wire therefore sparks with probability 1/3.
Example 3
1 0 1
1 1
0
Explanation
There are no wires, so the expected number of sparks is 0.
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.