#include <bits/stdc++.h>   using namespace std;   const int MXN = 1 << 19;   const int MOD = 1000000007;   int arr[MXN];   int suf[2 * MXN];   int pre[2 * MXN];   int lb[2 * MXN];   int rb[2 * MXN];   void build(int c, int l, int r) {     lb[c] = l;     rb[c] = r;     if (l == r) return;     int k = (l + r) / 2;     build(2 * c, l, k);     build(2 * c + 1, k + 1, r);   }   void updatesuf(int c, int idx, int amt) {     if (lb[c] == rb[c]) {       suf[c] += amt;       return;     }     int k = (lb[c] + rb[c]) / 2;     if (idx <= k)       updatesuf(2 * c, idx, amt);     else       updatesuf(2 * c + 1, idx, amt);     suf[c] = suf[2 * c] + suf[2 * c + 1];     suf[c] %= MOD;   }   void updatepre(int c, int idx, int amt) {     if (lb[c] == rb[c]) {       pre[c] += amt;       return;     }     int k = (lb[c] + rb[c]) / 2;     if (idx <= k)       updatepre(2 * c, idx, amt);     else       updatepre(2 * c + 1, idx, amt);     pre[c] = pre[2 * c] + pre[2 * c + 1];     pre[c] %= MOD;   }   int querysuf(int c, int l, int r) {     if (lb[c] == l && rb[c] == r) return suf[c];     int k = (lb[c] + rb[c]) / 2;     if (l <= k && k < r)       return (querysuf(2 * c, l, k) + querysuf(2 * c + 1, k + 1, r)) % MOD;     else if (r <= k)       return querysuf(2 * c, l, r);     else       return querysuf(2 * c + 1, l, r);   }   int querypre(int c, int l, int r) {     if (lb[c] == l && rb[c] == r) return pre[c];     int k = (lb[c] + rb[c]) / 2;     if (l <= k && k < r)       return (querypre(2 * c, l, k) + querypre(2 * c + 1, k + 1, r)) % MOD;     else if (r <= k)       return querypre(2 * c, l, r);     else       return querypre(2 * c + 1, l, r);   }   int main() {     int n;     scanf( %d , &n);     vector<pair<int, int> > proc;     for (int i = 1; i <= n; i++) {       scanf( %d , arr + i);       proc.emplace_back(arr[i], i);     }     build(1, 0, MXN - 1);     sort(proc.begin(), proc.end());     int ans = 0;     for (auto p : proc) {       int x = p.first;       int idx = p.second;       int l = idx - 1;       int r = n - l - 1;       int a = querysuf(1, idx, n);       int b = querypre(1, 1, idx);       long long cur =           1ll * a * (l + 1) + 1ll * b * (r + 1) + 1ll * (l + 1) * (r + 1);       cur %= (long long)MOD;       cur *= (long long)x;       cur %= (long long)MOD;       ans += cur;       ans %= MOD;       updatesuf(1, idx, r + 1);       updatepre(1, idx, l + 1);     }     printf( %d n , ans);     return 0;   }   