#include #define rep(i, p, n) for (ll i = p; i < (ll)(n); i++) #define rep2(i, p, n) for (ll i = p; i >= (ll)(n); i--) using namespace std; using ll = long long; using ld = long double; const double pi = 3.141592653589793; const long long inf = 2 * 1e9; const long long linf = 4 * 1e18; const ll mod1 = 1000000007; const ll mod2 = 998244353; template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } // atcoder #include using namespace atcoder; using mint1 = modint1000000007; using mint2 = modint998244353; vector> base = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; vector li; struct D { mint1 num; ll co; }; D op(D a, D b) { return {b.num * li.at(a.co) + a.num, a.co + b.co}; } D e() { return {0, 0}; } int main() { ////////////////// ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ////////////////// ll N; cin >> N; li.push_back(1); rep(i, 0, 101010) { li.push_back(li.back() * 2); } vector> A(N); rep(i, 0, N) { ll B; cin >> B; A.at(i) = {B, i}; } sort(A.begin(), A.end()); mint1 ans = 0; vector init(N, {0, 1}); rep(i, 0, 2) { segtree Lseg(init), Rseg(init); queue Q; ll now = -1; rep(i, 0, N) { if (A.at(i).first != now) { now = A.at(i).first; while (Q.size()) { ll P = Q.front(); Q.pop(); Lseg.set(P, {1, 1}); Rseg.set(N - 1 - P, {1, 1}); } } // rep(i, 0, N) // { // cout << Lseg.get(i).num.val(); // } // cout << endl; // cout << Lseg.prod(0, A.at(i).second).num.val() << Rseg.prod(0, N - A.at(i).second).num.val() << endl; ans += Lseg.prod(0, A.at(i).second).num * Rseg.prod(0, N - A.at(i).second).num; Q.push(A.at(i).second); } rep(i, 0, N) { A.at(i).first = inf - A.at(i).first; } sort(A.begin(), A.end()); } cout << ans.val() << endl; }