#P5498. [LnOI2019] 脸滚键盘

[LnOI2019] 脸滚键盘

Description

The giraffe Abbi likes to roll its face on the keyboard. Each time it does so, it multiplies the values in some sub-interval.

A sub-interval is a contiguous interval within an interval.

The weight of a sub-interval is defined as the product of the weights of all its elements.

The expected weight of an interval is defined as the expected value of the weight of a sub-interval chosen uniformly at random from all sub-intervals of that interval.

Given nn numbers, where the ii-th number is the weight aia_i.

There are qq queries. For each query l  rl \ \ r, ask for the expected weight of the specified interval.

Input Format

The first line contains two integers nn and qq.

The second line contains nn integers, the ii-th integer is the initial value aia_i of the sequence.

The next qq lines each contain two integers l  rl \ \ r, representing the query interval.

Output Format

For each query, output the expected weight of the specified interval.

Since the expected weight can be very large, output the expected weight modulo 100000007100000007.

Do not ask what to do if it is not divisible; see above.

If it still does not work, please refer to https://www.luogu.org/problem/P2613.

5 3
6 12 6 3 27
1 1
4 5
1 3
6
37
100

Hint

Time and memory limits: 1 s / 512 MB.

For 30%30\% of the testdata, 1n,q1001 \leq n, q \leq 100.

For 100%100\% of the testdata, 1n,q1061 \leq n, q \leq 10^6, 1ai1071 \leq a_i \leq 10^7.

Sample explanation: For the interval [1,1][1,1], there is only one sub-interval [1,1][1,1] with weight 66. The probability of choosing each sub-interval is 11\frac{1}{1}, so the expected weight is 6.

For the interval [4,5][4,5], there are three sub-intervals [4,4][4,4], [4,5][4,5], and [5,5][5,5], with weights 33, 8181, and 2727 respectively. The probability of choosing each sub-interval is 13\frac{1}{3}, so the total expected weight is 3737.

For the interval [1,3][1,3], there are six sub-intervals [1,1][1,1], [1,2][1,2], [1,3][1,3], [2,2][2,2], [2,3][2,3], and [3,3][3,3], with weights 66, 7272, 432432, 1212, 7272, and 66 respectively. The probability of choosing each sub-interval is 16\frac{1}{6}, so the total expected weight is 100100.

It is recommended to use fast input.

Translated by ChatGPT 5