CF1167F.Scalar Queries

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array a1,a2,,ana_1, a_2, \dots, a_n . All aia_i are pairwise distinct.

Let's define function f(l,r)f(l, r) as follows:

  • let's define array b1,b2,,brl+1b_1, b_2, \dots, b_{r - l + 1} , where bi=al1+ib_i = a_{l - 1 + i} ;
  • sort array bb in increasing order;
  • result of the function f(l,r)f(l, r) is i=1rl+1bii\sum\limits_{i = 1}^{r - l + 1}{b_i \cdot i} .

Calculate (1lrnf(l,r))mod(109+7)\left(\sum\limits_{1 \le l \le r \le n}{f(l, r)}\right) \mod (10^9+7) , i.e. total sum of ff for all subsegments of aa modulo 109+710^9+7 .

输入格式

The first line contains one integer nn ( 1n51051 \le n \le 5 \cdot 10^5 ) — the length of array aa .

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ai1091 \le a_i \le 10^9 , aiaja_i \neq a_j for iji \neq j ) — array aa .

输出格式

Print one integer — the total sum of ff for all subsegments of aa modulo 109+710^9+7

输入输出样例

  • 输入#1

    4
    5 2 4 7
    

    输出#1

    167
    
  • 输入#2

    3
    123456789 214365879 987654321
    

    输出#2

    582491518
    

说明/提示

Description of the first example:

  • f(1,1)=51=5f(1, 1) = 5 \cdot 1 = 5 ;
  • f(1,2)=21+52=12f(1, 2) = 2 \cdot 1 + 5 \cdot 2 = 12 ;
  • f(1,3)=21+42+53=25f(1, 3) = 2 \cdot 1 + 4 \cdot 2 + 5 \cdot 3 = 25 ;
  • f(1,4)=21+42+53+74=53f(1, 4) = 2 \cdot 1 + 4 \cdot 2 + 5 \cdot 3 + 7 \cdot 4 = 53 ;
  • f(2,2)=21=2f(2, 2) = 2 \cdot 1 = 2 ;
  • f(2,3)=21+42=10f(2, 3) = 2 \cdot 1 + 4 \cdot 2 = 10 ;
  • f(2,4)=21+42+73=31f(2, 4) = 2 \cdot 1 + 4 \cdot 2 + 7 \cdot 3 = 31 ;
  • f(3,3)=41=4f(3, 3) = 4 \cdot 1 = 4 ;
  • f(3,4)=41+72=18f(3, 4) = 4 \cdot 1 + 7 \cdot 2 = 18 ;
  • f(4,4)=71=7f(4, 4) = 7 \cdot 1 = 7 ;
首页