#include <bits/stdc++.h>
using namespace std;
#include <iostream>
#define Sa7afy22 ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
#define cin(v) for (auto&i:v) cin >> i;
#define cout(v) for (auto&i:v) cout << i << " ";cout<<e;
#include <algorithm>
#include <vector>
#define ll long long int
#define e '\n'
#define PI acos(-1)
#define PHI (long double)1.61803398875 //golden ratio
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define tan(a) tan((a)*PI/180)
#define eb emplace_back
#define ones(x) __builtin_popcountll(x)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define pii pair<ll,ll>
#define vi vector<int>
#define vll vector<ll>
#include <set>
#include <deque>
#define d2v vector<vector<int>>
typedef pair<int,int> Pii;
#define pb push_back
const int mod = 1e9+7;
ll mult(ll a, ll b){ return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[]={1,0,-1,0,1,1,-1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};
const int N = 1e5+5;
struct BIT{
vector<ll>tree;
BIT(int sz) {
tree.resize(sz + 1);
}
ll get(int x) {
ll res = 0;
while (x>0) {
res = add(tree[x],res);
x -= x & -x;
}
return res;
}
void update (ll x,ll val) {
while (x < tree.size()) {
tree[x] = add(tree[x],val);
x += x & -x;
}
}
ll query(int l,int r) {
if (r < l)return 0;
return get(r) - get(l - 1);
}
};
void solve () {
int n;
cin >> n;
vector<ll> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (i)arr[i] += arr[i - 1];
}
vector<ll> prefix(n);
for (int i = 0; i < n; i++) {
prefix[i] = arr[i];
}
vector<ll> vals;
for (int i = 0; i < n; ++i) {
vals.push_back(arr[i]);
}
sort(all(vals));
vals.erase(unique(vals.begin(), vals.end()),
vals.end());
for (int i = 0; i < n; ++i) {
arr[i] = lower_bound(all(vals), arr[i]) -
vals.begin() + 1;
}
BIT Freqtree(n+ 1);
BIT Sumtree(n + 1);
ll ans{};
for (int i = 0; i < n; i++) {
if (prefix[i] > 0)
ans = add(prefix[i], ans);
ll freq = Freqtree.query(1, arr[i] - 1), sum = Sumtree.query(1, arr[i] - 1);
ans = add(ans, sub(mult(freq, prefix[i]), sum));
Sumtree.update(arr[i], prefix[i]);
Freqtree.update(arr[i], 1);
}
cout << ans << '\n';
}
signed main() {
Sa7afy22
#ifndef ONLINE_JUDGE
freopen("testcase.txt", "r", stdin
); freopen("output.txt", "w", stdout
); #endif
int tests = 1;
//cin >> tests;
for (int i = 1; i <= tests; i++) {
/*cout<<"Case #"<<i<<": ";*/
solve();
}
Time
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2RlZmluZSBTYTdhZnkyMiBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApO2Npbi50aWUoMCk7Y291dC50aWUoMCk7CiNkZWZpbmUgVGltZSBjZXJyIDw8ICJUaW1lIFRha2VuOiAiIDw8IChmbG9hdCljbG9jaygpIC8gQ0xPQ0tTX1BFUl9TRUMgPDwgIiBTZWNzIiA8PCAiXG4iOwojZGVmaW5lIGNpbih2KSBmb3IgKGF1dG8maTp2KSBjaW4gPj4gaTsKI2RlZmluZSBjb3V0KHYpIGZvciAoYXV0byZpOnYpIGNvdXQgPDwgaSA8PCAiICI7Y291dDw8ZTsKI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2RlZmluZSBsbCBsb25nIGxvbmcgaW50CiNkZWZpbmUgZSAnXG4nCiNkZWZpbmUgUEkgYWNvcygtMSkKI2RlZmluZSBQSEkgKGxvbmcgZG91YmxlKTEuNjE4MDMzOTg4NzUgLy9nb2xkZW4gcmF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gIGxsIGdldChpbnQgeCkgewogICAgICAgIGxsIHJlcyA9IDA7CiAgICAgICAgd2hpbGUgKHg+MCkgewogICAgICAgICAgICByZXMgPSBhZGQodHJlZVt4XSxyZXMpOwogICAgICAgICAgICB4IC09IHggJiAteDsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlczsKICAgIH0KCiAgICB2b2lkIHVwZGF0ZSAobGwgeCxsbCB2YWwpIHsKICAgICAgICB3aGlsZSAoeCA8IHRyZWUuc2l6ZSgpKSB7CiAgICAgICAgICAgIHRyZWVbeF0gPSBhZGQodHJlZVt4XSx2YWwpOwogICAgICAgICAgICB4ICs9IHggJiAteDsKICAgICAgICB9CiAgICB9CgogICAgbGwgcXVlcnkoaW50IGwsaW50IHIpIHsKICAgICAgICBpZiAociA8IGwpcmV0dXJuIDA7CiAgICAgICAgcmV0dXJuIGdldChyKSAtIGdldChsIC0gMSk7CiAgICB9Cn07CnZvaWQgc29sdmUgKCkgewogICAgaW50IG47CiAgICBjaW4gPj4gbjsKICAgIHZlY3RvcjxsbD4gYXJyKG4pOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBjaW4gPj4gYXJyW2ldOwogICAgICAgIGlmIChpKWFycltpXSArPSBhcnJbaSAtIDFdOwogICAgfQogICAgdmVjdG9yPGxsPiBwcmVmaXgobik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHByZWZpeFtpXSA9IGFycltpXTsKICAgIH0KCiAgICB2ZWN0b3I8bGw+IHZhbHM7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIHZhbHMucHVzaF9iYWNrKGFycltpXSk7CiAgICB9CiAgICBzb3J0KGFsbCh2YWxzKSk7CiAgICB2YWxzLmVyYXNlKHVuaXF1ZSh2YWxzLmJlZ2luKCksIHZhbHMuZW5kKCkpLAogICAgICAgICAgICAgICB2YWxzLmVuZCgpKTsKICAgIGZvciAoaW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