算法
(前缀和) $O(n)$
注意到$$\sum {{a_i}{a_j}} = {a_1}\left( {\sum\limits_{i = 2}^n {{a_i}} } \right) + {a_2}\left( {\sum\limits_{i = 3}^n {{a_i}} } \right) + \cdots + {a_{n - 1}}{a_n}$$
然后用前缀和处理一下即可。
C++ 代码
#include <iostream>
#define int long long
using namespace std;
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
int a[N];
signed main() {
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; ++i) cin >> a[i], sum += a[i];
int ans = 0;
for (int i = 1; i <= n; ++i) {
sum -= a[i];
ans += a[i] * (sum % MOD);
ans %= MOD;
}
cout << ans << '\n';
return 0;
}