import sys from bisect import bisect_left sys.setrecursionlimit(10 ** 7) input = sys.stdin.readline f_inf = float('inf') mod = 10 ** 9 + 7 class BIT: """ 1-indexで使用しないと無限ループするので注意 """ def __init__(self, n): self.n = n self.bit = [0] * (n + 1) def update(self, idx, x): """ idx番目にxをO(logN)で加算する """ while idx <= self.n: self.bit[idx] += x idx += idx & (-idx) def query(self, idx): """ 1 ~ idx番目までの区間和をO(logN)で返す """ res = 0 while idx > 0: res += self.bit[idx] idx -= idx & (-idx) return res def sec_sum(self, left, right): """ left ~ right番目までの区間和をO(logN)で返す """ return self.query(right) - self.query(left - 1) def lower_bound(self, w): """ 区間和がw以上になる最小のindexをO(logN)で返す """ if w <= 0: return 0 x = 0 k = 1 << self.n.bit_length() while k: if x + k <= self.n and self.bit[x + k] < w: w -= self.bit[x + k] x += k k //= 2 return x + 1 def debug(self): """ bitの状態を出力する """ print(*[self.sec_sum(i, i) for i in range(1, self.n + 1)]) def resolve(): n = int(input()) A = list(map(int, input().split())) comp = sorted(list(set(A))) bit_cnt = BIT(n + 10) bit_total = BIT(n + 10) cnt = [[0, 0] for _ in range(n)] total = [[0, 0] for _ in range(n)] for i in range(n): idx = bisect_left(comp, A[i]) + 1 cnt[i][0] = bit_cnt.sec_sum(idx + 1, n) total[i][0] = bit_total.sec_sum(idx + 1, n) % mod bit_cnt.update(idx, 1) bit_total.update(idx, A[i]) bit_cnt = BIT(n + 10) bit_total = BIT(n + 10) for i in reversed(range(n)): idx = bisect_left(comp, A[i]) + 1 cnt[i][1] = bit_cnt.query(idx - 1) total[i][1] = bit_total.query(idx - 1) % mod bit_cnt.update(idx, 1) bit_total.update(idx, A[i]) res = 0 for i in range(n): left_cnt, right_cnt = cnt[i] left_tot, right_tot = total[i] if (not left_cnt) or (not right_cnt): continue res += left_cnt * right_tot % mod + right_cnt * left_tot % mod + A[i] * left_cnt % mod * right_cnt % mod res %= mod print(res) if __name__ == '__main__': resolve()