Eming cup Problem D. Game of numbers

Time Limit: 0.5 seconds

Memory Limit: 512 mebibytes

Mingming loves playing with numbers, and today, someone gives him a permutation of nn : P_1, P_2\ldots P_nP1​​,P2​​Pn​​.

Nannan is Mingming's friend, then he asked Mingming: if he select a number xx between [l, r][l,r], and them select another number yy in range (x, r](x,r], then calculate A_xA_yAx​​Ay​​. It is clear that there are many valid selections. Nannan wants to know the sum of them. As the number can be very large, you only have to output the answer module 10^9 + 7109​​+7

To make Mingming enjoy the game more, Nannan gives him QQ pairs of l, rl,r. Of course, Mingming can solve this problem. But now, he wants to check if you can solve the problem.

More exactly, your task is to calculate \sum_{l\le i\le r}\sum_{i< j\le r} A_iA_jlir​​i<jr​​Ai​​Aj​​ module 10^9 + 7109​​+7 for each l, rl,r, where A_1, A_2, \ldots A_nA1​​,A2​​,An​​ is a permutation of nn.

Input

First line is a number nn(3\le n\le 10^53n105​​), which means the length of sequence a.

Second line is nn numbers from A_1A1​​ to A_nAn​​(1\le A_i\le 10^51Ai​​105​​, for each i \neq jij, A_i \neq A_jAi​​Aj​​).

Third line is a number Q(1\le Q\le 10^5)Q(1Q105​​) means how many pairs of ll and rr will be chosen.

In the next QQ lines, the ii-th one contians two integers ll and rr (l< rl<r), which is the ii-th query.

Output

Output QQ lines of answers for each pairs query.

Examples

Sample Input
5
3 2 4 1 5
2
1 2
2 4
Sample Output
6
14

Note

times× 500000004 \equiv≡ 1 (mod 10^9+7109​​+7)

前缀和

 

转载于:https://www.cnblogs.com/sakumia/p/6507119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值