Problem:
Score : 300 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7).

Constraints

2 \leq N \leq 2\times 10^5

0 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7).

Sample Input 1

3
1 2 3

Sample Output 1

11

We have 1 \times 2 + 1 \times 3 + 2 \times 3 = 11.

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829

Solution in PHP (7.4.4) (judge verdict: Accepted)

<?php
define("MOD", 10**9+7);

$N = (int)trim(fgets(STDIN));
//$T = trim(fgets(STDIN));
//list($D,$T,$S) = array_map('intval',explode(" ",trim(fgets(STDIN))));
$A = array_map('intval',explode(" ",trim(fgets(STDIN))));

$rsum=0;
for($i=0;$i<$N;$i++) {
  $rsum=addm($rsum,$A[$i]);
}
$sum=0;
for($i=0;$i<$N;$i++) {
  $rsum -= $A[$i];
  if ($rsum < 0) $rsum+=MOD;
  $r = $A[$i]*$rsum;
  $sum = addm($sum,$r);
}
printf("%d\n",$sum);


//https://atcoder.jp/contests/abc145/submissions/8474723


//足し算
function addm($a, $b){
    return ($a + $b) % MOD;
}
//引き算
function subm($a, $b){
    return ($a + MOD - $b) % MOD;
}
//掛け算
function mulm($a, $b){
    return ($a * $b) % MOD;
}
//割り算
function divm($a, $b){
    return mulm($a, powerm($b, MOD - 2));
}
//累乗
function powerm($a, $b){
    if($b == 0)return 1;
    if($b & 1)return (powerm($a, $b>>1)**2 % MOD) * $a % MOD;
    return powerm($a, $b>>1)**2 % MOD;
}
//階乗
function factorialm($a){
    if($a == 0)return 1;
    return factorialm($a - 1) * $a % MOD;
}
//順列
function nPm($n, $m){
    if($m == 0)return 1;
    return nPm($n, $m - 1) * ($n - $m + 1) % MOD;
}
//組み合わせ
function nCm($n, $m){
    $m = min($m, $n-$m);
    return divm(nPm($n, $m), factorialm($m));
}

//解説のcombinationを移植
Class combinationsnk {
  private $fact;
  private $ifact;
  public function __construct(int $n) {
    $this->fact[0]=1;
    for($i=1;$i<=$n;$i++) $this->fact[$i]=mulm($this->fact[$i-1],$i);
    $this->ifact[$n]=powerm($this->fact[$n],MOD-2);
    for ($i = $n; $i >= 1; --$i) $this->ifact[$i-1] = mulm($this->ifact[$i],$i);
  }
  public function calc(int $n,int $k)
  {
    if ($k < 0 || $k > $n) return 0;
    return mulm(mulm($this->fact[$n],$this->ifact[$k]),$this->ifact[$n-$k]);
  }
}