Under construction

Still being built. Things may change

Eraser

Time limit1sMemory limit256 MB

Summary
Given a sequence, add the products of every triple of entries at distinct positions with three different values, modulo 1,000,000,007.
Level

Medium6 of 10

Topics
Combinatorics, Math, Array
Solved
No attempts yet

Problem

Seunghyun is worried that someone will ruin his erasers, so he decided to make his own.

Seunghyun picks a sequence of natural numbers a1,a2,…,ana_1, a_2, \dots, a_n. He then chooses three elements aia_i, aja_j, aka_k at three different positions of the sequence and builds a rectangular eraser whose width, depth and height are those three values. He calls it the i−j−ki-j-k eraser. Seunghyun likes erasers that are tall and thin, so ai<aj<aka_i < a_j < a_k must hold.

Look at the picture above. Let the sequence be a1=3a_1 = 3, a2=1a_2 = 1, a3=1a_3 = 1, a4=2a_4 = 2. The 1−2−31-2-3 eraser cannot be made, because 3<1<13 < 1 < 1 is false. The 2−4−12-4-1 eraser can be made, because 1<2<31 < 2 < 3 is true. The only erasers that can be made from this sequence are the 2−4−12-4-1 eraser and the 3−4−13-4-1 eraser.

Seunghyun wants to know the total volume of every eraser he can make. The volume of an eraser is the product of its width, depth and height. He needs that number to design the warehouse where the erasers will be stacked, so help him compute it.

Input

The first line contains the length nn of the sequence. (1≤n≤100,0001 \le n \le 100{,}000)

The second line contains a1,a2,…,ana_1, a_2, \dots, a_n in order, separated by spaces. (1≤ai≤100,0001 \le a_i \le 100{,}000)

Output

Print the sum of the volumes of every eraser that can be made. Seunghyun wants to check that the program runs correctly first, so print that sum modulo 1,000,000,0071{,}000{,}000{,}007 (109+710^9 + 7).

Hint

Take the sequence a1=3a_1 = 3, a2=1a_2 = 1, a3=1a_3 = 1, a4=2a_4 = 2. The only erasers that can be made are the 2−4−12-4-1 eraser and the 3−4−13-4-1 eraser, so the total volume is a2a4a1+a3a4a1=1×2×3+1×2×3=12a_2 a_4 a_1 + a_3 a_4 a_1 = 1 \times 2 \times 3 + 1 \times 2 \times 3 = 12.

Examples3

  1. Example 1

    Input
    4
    3 1 1 2
    
    Expected output
    12
    
  2. Example 2

    Input
    1
    5
    
    Expected output
    0
    
  3. Example 3

    Input
    3
    1 2 3
    
    Expected output
    6