#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
template <unsigned MOD>
class modular_int {
 public:
 protected:
  unsigned x;

 public:
  modular_int inv() const {
    unsigned a = MOD, b = x;
    int u = 0, v = 1;
    while (b) {
      int t = a / b;
      a -= t * b, std ::swap(a, b);
      u -= t * v, std ::swap(u, v);
    }
    if (u < 0) u += MOD;
    return modular_int(u);
  }
  inline modular_int pow(long long y) const {
    modular_int b = *this, r = 1;
    if (y < 0) b = b.inv(), y = -y;
    for (; y; y >>= 1, b *= b)
      if (y & 1) r *= b;
    return r;
  }

 public:
  modular_int() { x = 0; }
  modular_int(const modular_int &y) { x = y.x; }
  modular_int(modular_int &&y) { x = y.x; }
  inline void clear() { *this = modular_int(); }
  inline int to_int() const { return (int)x; }
  inline unsigned value() const { return x; }

 public:
  modular_int(int y) {
    if (y < 0 || (int)MOD <= y) y %= (int)MOD;
    if (y < 0) y += MOD;
    x = y;
  }
  modular_int(unsigned y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  modular_int(long long y) {
    if (y < 0 || MOD <= y) y %= MOD;
    if (y < 0) y += MOD;
    x = y;
  }
  modular_int(unsigned long long y) {
    if (MOD <= y)
      x = y % MOD;
    else
      x = y;
  }
  modular_int &operator=(const modular_int &y) { x = y.x; }
  modular_int &operator=(const int &y) { x = modular_int(y); }
  modular_int &operator=(const long long &y) { x = modular_int(y); }
  modular_int &operator=(const unsigned int &y) { x = modular_int(y); }
  modular_int &operator=(const unsigned long long &y) { x = modular_int(y); }
  modular_int &operator+=(const modular_int &y) {
    if ((x += y.x) >= MOD) x -= MOD;
    return *this;
  }
  modular_int &operator-=(const modular_int &y) {
    if ((x -= y.x) & (1u << 31)) x += MOD;
    return *this;
  }
  modular_int &operator*=(const modular_int &y) {
    x = (unsigned long long)x * y.x % MOD;
    return *this;
  }
  modular_int &operator/=(const modular_int &y) {
    x = (unsigned long long)x * y.inv().x % MOD;
    return *this;
  }
  modular_int &operator+=(const int &y) { return *this += modular_int(y); }
  modular_int &operator-=(const int &y) { return *this -= modular_int(y); }
  modular_int &operator*=(const int &y) { return *this *= modular_int(y); }
  modular_int &operator/=(const int &y) { return *this /= modular_int(y); }
  modular_int &operator+=(const long long &y) {
    return *this += modular_int(y);
  }
  modular_int &operator-=(const long long &y) {
    return *this -= modular_int(y);
  }
  modular_int &operator*=(const long long &y) {
    return *this *= modular_int(y);
  }
  modular_int &operator/=(const long long &y) {
    return *this /= modular_int(y);
  }
  modular_int &operator+=(const unsigned int &y) {
    return *this += modular_int(y);
  }
  modular_int &operator-=(const unsigned int &y) {
    return *this -= modular_int(y);
  }
  modular_int &operator*=(const unsigned int &y) {
    return *this *= modular_int(y);
  }
  modular_int &operator/=(const unsigned int &y) {
    return *this /= modular_int(y);
  }
  modular_int &operator+=(const unsigned long long &y) {
    return *this += modular_int(y);
  }
  modular_int &operator-=(const unsigned long long &y) {
    return *this -= modular_int(y);
  }
  modular_int &operator*=(const unsigned long long &y) {
    return *this *= modular_int(y);
  }
  modular_int &operator/=(const unsigned long long &y) {
    return *this /= modular_int(y);
  }
  friend modular_int operator+(modular_int x, const modular_int &y) {
    return x += y;
  }
  friend modular_int operator-(modular_int x, const modular_int &y) {
    return x -= y;
  }
  friend modular_int operator*(modular_int x, const modular_int &y) {
    return x *= y;
  }
  friend modular_int operator/(modular_int x, const modular_int &y) {
    return x *= y.inv();
  }
  friend modular_int operator+(modular_int x, const int &y) { return x += y; }
  friend modular_int operator-(modular_int x, const int &y) { return x -= y; }
  friend modular_int operator*(modular_int x, const int &y) { return x *= y; }
  friend modular_int operator/(modular_int x, const int &y) { return x /= y; }
  friend modular_int operator+(modular_int x, const long long &y) {
    return x += y;
  }
  friend modular_int operator-(modular_int x, const long long &y) {
    return x -= y;
  }
  friend modular_int operator*(modular_int x, const long long &y) {
    return x *= y;
  }
  friend modular_int operator/(modular_int x, const long long &y) {
    return x /= y;
  }
  friend modular_int operator+(modular_int x, const unsigned &y) {
    return x += y;
  }
  friend modular_int operator-(modular_int x, const unsigned &y) {
    return x -= y;
  }
  friend modular_int operator*(modular_int x, const unsigned &y) {
    return x *= y;
  }
  friend modular_int operator/(modular_int x, const unsigned &y) {
    return x /= y;
  }
  friend modular_int operator+(modular_int x, const unsigned long long &y) {
    return x += y;
  }
  friend modular_int operator-(modular_int x, const unsigned long long &y) {
    return x -= y;
  }
  friend modular_int operator*(modular_int x, const unsigned long long &y) {
    return x *= y;
  }
  friend modular_int operator/(modular_int x, const unsigned long long &y) {
    return x /= y;
  }
  modular_int operator-() const { return (x ? MOD - x : 0); }
  modular_int operator++() const { return *this += 1; }
  modular_int operator--() const { return *this -= 1; }
  modular_int operator++(int) const {
    modular_int ret = *this;
    *this += 1;
    return ret;
  }
  modular_int operator--(int) const {
    modular_int ret = *this;
    *this -= 1;
    return ret;
  }
  friend bool operator<(const modular_int &x, const modular_int &y) {
    return x.x < y.x;
  }
  friend bool operator==(const modular_int &x, const modular_int &y) {
    return x.x == y.x;
  }
  friend bool operator!=(const modular_int &x, const modular_int &y) {
    return x.x != y.x;
  }
  friend std ::ostream &operator<<(std ::ostream &os, modular_int x) {
    return os << x.x;
  }
  friend std ::istream &operator>>(std ::istream &is, modular_int &x) {
    is >> x.x;
    x.x %= MOD;
    return is;
  }
};
template <typename T>
class binary_indexed_tree {
 public:
 protected:
  int N;
  std ::vector<T> sum1, sum2;
  inline T ask(int p) {
    T res = 0;
    for (int i = p; i; i -= i & -i) res += (p + 1) * sum1[i] - sum2[i];
    return res;
  }
  inline void add(int p, T x) {
    for (int i = p; i <= N; i += i & -i) sum1[i] += x, sum2[i] += x * p;
  }
  inline void _check(int l) {
    if (l < 0 || l >= N) throw "Binary Indexed Tree Range Error!";
  }

 public:
  binary_indexed_tree() { clear(); }
  binary_indexed_tree(int n) { resize(n); }
  size_t size() { return N; }
  void resize(int n) { sum1.resize((N = n) + 1), sum2.resize(n + 1); }
  inline void clear() { resize(0); }
  inline void range_add(int l, int r, T x) {
    _check(l), _check(r);
    add(l + 1, x), add(r + 1, -x);
  }
  inline T range_sum(int l, int r) {
    _check(l), _check(r);
    return ask(r) - ask(l);
  }
  inline void add_only(int l, T x) {
    _check(l);
    range_add(l, l + 1, x);
  }
};
binary_indexed_tree<modular_int<1000000007> > tr1, tr2;
std ::pair<int, int> a[500005];
int main() {
  int n, i, pos, val;
  modular_int<1000000007> ans(0);
  std ::cin >> n;
  for (i = 1; i <= n; ++i) std ::cin >> a[i].first, a[i].second = i;
  std ::sort(a + 1, a + n + 1);
  tr1.resize(n + 5), tr2.resize(n + 5);
  for (i = 1; i <= n; ++i) {
    pos = a[i].second, val = a[i].first;
    ans += (tr1.range_sum(pos + 1, n + 1) -
            tr1.range_sum(pos, pos + 1) * (n - pos)) *
           pos * val;
    ans += (tr2.range_sum(1, pos) - tr2.range_sum(pos, pos + 1) * (pos - 1)) *
           (n + 1 - pos) * val;
    ans += modular_int<1000000007>(1) * pos * (n + 1 - pos) * val;
    tr1.range_add(pos, n + 1, modular_int<1000000007>(1));
    tr2.range_add(1, pos + 1, modular_int<1000000007>(1));
  }
  std ::cout << ans << std ::endl;
  return 0;
}
