Lantern Sparks
View as PDFPondo has strung lanterns together with
wires. Lantern
independently chooses an
integer brightness uniformly at random from the inclusive interval
.
A wire connecting lanterns and
produces a spark if at least one of the two
brightnesses is divisible by a fixed integer
. Let
be the number of wires that
spark.
Output the expected value of .
It can be shown that the answer can be expressed as a rational number in lowest
terms with
coprime to
. Output
.
Input
The first line contains three integers ,
, and
.
Each of the next lines contains two integers
and
.
Each of the next lines contains two integers
and
, describing a wire between
lanterns
and
.
Lanterns are numbered through
. 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 .
Constraints
Example 1
Input
3 3 2
1 2
3 4
5 6
1 2
2 3
3 1
Output
250000004
Explanation
Each lantern has brightness even with probability . A wire sparks unless both
endpoints are odd, which happens with probability
, so each wire sparks with
probability
. The three wires contribute
in expectation, and
.
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
Input
2 1 3
1 3
1 2
1 2
Output
333333336
Explanation
Lantern fails to be divisible by
with probability
, and lantern
never
chooses a multiple of
. The single wire therefore sparks with probability
.
Example 3
Input
1 0 1
1 1
Output
0
Explanation
There are no wires, so the expected number of sparks is .
Comments