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_nP​1​​,P​2​​…P​n​​.

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_yA​x​​A​y​​. 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 + 710​9​​+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_j∑​l≤i≤r​​∑​i<j≤r​​A​i​​A​j​​ module 10^9 + 710​9​​+7 for each l, rl,r, where A_1, A_2, \ldots A_nA​1​​,A​2​​,…A​n​​ is a permutation of nn.

Input

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

Second line is nn numbers from A_1A​1​​ to A_nA​n​​(1\le A_i\le 10^51≤A​i​​≤10​5​​, for each i \neq ji≠j, A_i \neq A_jA​i​​≠A​j​​).

Third line is a number Q(1\le Q\le 10^5)Q(1≤Q≤10​5​​) 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

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

前缀和

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

Eming cup Problem D. Game of numbers相关推荐

  1. SPOJ problem 42: Adding Reversed Numbers

    这是一道水题,就是让你把给出的两个数倒置后相加再倒回来,模拟一下就行. #include<cstdio> int n,m,x,y,t; int main(){for(scanf(" ...

  2. 2021牛客暑期多校训练营1 G Game of Swapping Numbers 思维 + 巧妙的转换

    传送门 文章目录 题意: 思路: 题意: 给你两个数组A,BA,BA,B,你可以选择AAA的两个位置i,j,i<ji,j,i<ji,j,i<j交换Ai,AjA_i,A_jAi​,Aj ...

  3. Codeforces Round #257 (Div. 1) D. Jzzhu and Numbers 高维前缀和 + 容斥

    传送门 文章目录 题意: 思路: 题意: 思路: 完全想不到容斥啊,看了半天也没看懂渍渍渍. 定义f[i]f[i]f[i]表示iii的超集个数,那么选择的方案就是2f[i]−12^{f[i]}-12f ...

  4. UVALive 3958 Weird Numbers (负进制数)

    Weird Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/F Description Binary number ...

  5. hdu-5867 Water problem(水题)

    题目链接: Water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. The Street View House Numbers (SVHN) Dataset

    原文: SVHN is a real-world image dataset for developing machine learning and object recognition algori ...

  7. NIPS 2018 接收论文list 完整清单

    NIPS2018 接收论文包括poster.tutorial.workshop等,目前官网公布了论文清单: https://nips.cc/Conferences/2018/Schedule Post ...

  8. 【NIPS 2018】完整论文下载链接-续

    NeurIPS2018的论文已经开放下载,本文总结了论文的pdf页面.直接点击对应论文即可访问对应的pdf下载页面. 如果希望Batch->所有论文链接 续->第一篇 [556] Mult ...

  9. 2016-2017 ACM-ICPC CHINA-Final(EC-final) 题解(10 / 12)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 2016-2017 ACM-ICPC CHINA-Final 比赛链接:https://codefor ...

最新文章

  1. 使用 Visual C# .NET 生成 Office COM 外接程序
  2. 『ACM-算法-ST算法』信息竞赛进阶指南--区间最值问题的ST算法
  3. vue3 v-model变化
  4. java 注解与反射_Java注解与反射直接上手
  5. 6-5-3:STL之stack和queue——优先级队列-priority_queue(堆)的基本使用和模拟实现以及仿函数
  6. 单体多字系统以及多体并行系统
  7. 小蒜的A+B 计蒜客 - T1283
  8. 腾讯、网易、新浪新闻网站爬虫编写记录及评论格式分析
  9. 【Flink】Flink checkpoint was declined
  10. 微课|玩转Python轻松过二级:第3章课后习题解答3
  11. mysql更改密码_Mysql更改密码、连接mysql、mysql常用命令
  12. GetUserName()第二个参数中指明缓冲区长度
  13. 二、云计算-私有云-国产-华为-FusionCloud+HCIE Cloud相关知识点+笔试题库
  14. 标准库函数begin和end的使用
  15. aso优化师是什么_做ASO优化师这么多年,没想到被它打败了!
  16. Android adb截图后保存到电脑
  17. 知乎上 40 个有趣回复,很精辟很提神
  18. CCU ETL脚本列表
  19. 奥村マヨ - 見えない翼
  20. 别让生气毁了你的生活(深度好文)

热门文章

  1. 二叉树的直径—leetcode543
  2. UDP对应的应用层协议之DNS、DHCP
  3. c++ array赋值问题
  4. 概率论-4.4 特征函数(待补充)
  5. 《汇编语言》-王爽-实验7
  6. Linux线程——线程同步
  7. 解决嵌入式开发过程中开发板与虚拟机的网络通信问题
  8. matlab7.0调节视图,新手求救啊!!!matlab7.0在win7启动后命令窗口出现大段代码!...
  9. excel多个工作表求和案例
  10. IE8“开发人员工具”使用详解上(各级菜单详解)