Editorial
Full solution
Precompute the following prefix products and prefix sums. All arithmetic is performed modulo , and division means multiplication by a modular inverse.
Set . For a query , compute
The answer is
Preprocessing takes time, and each query takes time. The total time complexity is .
Each input probability is for an integer between and . Precompute these possible probabilities and their inverses. Then both and can be updated with one multiplication each. Add , not , when computing . All required inverses exist because every probability is nonzero.
Proof
A block of length contains ordered pairs of bulbs, allowing the same bulb to be chosen twice. Thus, the sum of squared block lengths equals the number of ordered pairs belonging to the same lit block.
For , the two positions belong to the same lit block if and only if every bulb from through is lit. By independence, this event has probability
Subtask 1
Every bulb is guaranteed to light up. A query interval contains one block of length , so its answer is . Including input processing, the running time is .