#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif #include #include using mint = atcoder::modint1000000007; vector compress(vector A) { vector B; B.reserve(A.size()); for (const auto& x : A) B.push_back(x); sort(B.begin(), B.end()); B.erase(unique(B.begin(), B.end()), B.end()); for (unsigned i{}; i < A.size(); i++) A[i] = distance(B.begin(), lower_bound(B.begin(), B.end(), A[i])); return A; } void solve() { int N; cin >> N; vector A(N); for (auto& x : A) cin >> x; vector B = compress(A); atcoder::fenwick_tree L(N), R(N); for(int i = 0; i < N; i++) R.add(B[i], mint(2).pow(N - i - 1)); mint ans = 0; for(int i = 0; i < N; i++) { R.add(B[i], -mint(2).pow(N - i - 1)); { mint l = L.sum(0, B[i]); mint r = R.sum(0, B[i]); ans += l * r; } { mint l = L.sum(B[i] + 1, N); mint r = R.sum(B[i] + 1, N); ans += l * r; } L.add(B[i], mint(2).pow(i)); } cout << ans.val() << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while (tt--) { solve(); } }