Given a sequence, add the products of every triple of entries at distinct positions with three different values, modulo 1,000,000,007.
Medium6CombinatoricsMathArrayNo attempts yetTime limit1sMemory limit256 MBSeunghyun is worried that someone will ruin his erasers, so he decided to make his own.
Seunghyun picks a sequence of natural numbers a1,a2,…,an. He then chooses three elements ai, aj, ak 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−k eraser. Seunghyun likes erasers that are tall and thin, so ai<aj<ak must hold.

Look at the picture above. Let the sequence be a1=3, a2=1, a3=1, a4=2. The 1−2−3 eraser cannot be made, because 3<1<1 is false. The 2−4−1 eraser can be made, because 1<2<3 is true. The only erasers that can be made from this sequence are the 2−4−1 eraser and the 3−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.
The first line contains the length n of the sequence. (1≤n≤100,000)
The second line contains a1,a2,…,an in order, separated by spaces. (1≤ai≤100,000)
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,007 (109+7).
Take the sequence a1=3, a2=1, a3=1, a4=2. The only erasers that can be made are the 2−4−1 eraser and the 3−4−1 eraser, so the total volume is a2a4a1+a3a4a1=1×2×3+1×2×3=12.