#include template class SegmentTree { public: using Operation = std::function; SegmentTree(int size, Operation operation, T identity = T()) : operation_(operation), identity_(identity) { int two = 1; while (two < size) { two <<= 1; } v_.resize(two * 2 - 1, identity_); } void Set(int i, T v) { int index = Leaf(i); while (true) { v_[index] = v; if (index == 0) break; v = operation_(v, v_[index + (IsRight(index) ? -1 : 1)]); index = Parent(index); } } T Get(int i) const { return Aggregate(i, i + 1); } T Aggregate(int begin, int end) const { int l = Leaf(begin), r = Leaf(end); T v = identity_; while (l < r) { if (IsRight(l)) { v = operation_(v, v_[l]); ++l; } l = Parent(l); if (IsRight(r)) { v = operation_(v, v_[r - 1]); } r = Parent(r); } return v; } private: int Leaf(int i) const { return i + (v_.size() >> 1); } bool IsRight(int i) const { return !(i & 1); } int Parent(int i) const { return (i - 1) >> 1; } const Operation operation_; const T identity_; std::vector v_; }; template class AddSegmentTree : public SegmentTree { public: AddSegmentTree(int n) : SegmentTree(n, [](T a, T b) { return a + b; }) {} }; struct Compressor { Compressor(const std::vector& v) : coord(v) { std::sort(coord.begin(), coord.end()); coord.erase(std::unique(coord.begin(), coord.end()), coord.end()); } int64_t operator()(int64_t x) const { return std::lower_bound(coord.begin(), coord.end(), x) - coord.begin(); } std::vector coord; }; #ifndef MODINT_H_ #define MODINT_H_ #ifndef DASSERT_H_ #define DASSERT_H_ #if DEBUG #define dassert(x) assert(x) #else #define dassert(x) ((void)0) #endif #endif // DASSERT_H_ namespace { using i32 = int32_t; using i64 = int64_t; } // namespace #define BIN_OPS(F) F(+) F(-) F(*) F(/) #define CMP_OPS(F) F(!=) F(<) F(<=) F(==) F(>) F(>=) template class ModInt { public: ModInt() : n_(0) {} ModInt(i64 n) : n_(n % Mod) { if (n_ < 0) { // In C++, (-n)%m == -(n%m). n_ += Mod; } } ModInt& operator+=(const ModInt& m) { n_ += m.n_; if (n_ >= Mod) { n_ -= Mod; } return *this; } ModInt& operator++() { return (*this) += 1; } ModInt& operator-=(const ModInt& m) { n_ -= m.n_; if (n_ < 0) { n_ += Mod; } return *this; } ModInt& operator--() { return (*this) -= 1; } ModInt& operator*=(const ModInt& m) { n_ = i64(n_) * m.n_ % Mod; return *this; } ModInt& operator/=(const ModInt& m) { *this *= m.Inv(); return *this; } #define DEFINE(op) \ ModInt operator op(const ModInt& m) const { return ModInt(*this) op## = m; } BIN_OPS(DEFINE) #undef DEFINE #define DEFINE(op) \ bool operator op(const ModInt& m) const { return n_ op m.n_; } CMP_OPS(DEFINE) #undef DEFINE ModInt operator-() const { return ModInt(-n_); } ModInt Pow(i64 n) const { if (n < 0) { return Inv().Pow(-n); } // a * b ^ n = answer. ModInt a = 1, b = *this; while (n != 0) { if (n & 1) { a *= b; } n /= 2; b *= b; } return a; } ModInt Inv() const { dassert(n_ != 0); if (n_ > kMaxCacheSize) { // Compute the inverse based on Fermat's little theorem. Note that this // only works when n_ and Mod are relatively prime. The theorem says that // n_^(Mod-1) = 1 (mod Mod). So we can compute n_^(Mod-2). return Pow(Mod - 2); } for (i64 i = inv_.size(); i <= n_; ++i) { inv_.push_back(i <= 1 ? i : (Mod / i * -inv_[Mod % i])); } return inv_[n_]; } i64 value() const { return n_; } static ModInt Fact(i64 n) { dassert(0 <= n && n <= kMaxCacheSize); for (i64 i = fact_.size(); i <= n; ++i) { fact_.push_back(i == 0 ? 1 : fact_.back() * i); } return fact_[n]; } static ModInt InvFact(i64 n) { dassert(0 <= n && n <= kMaxCacheSize); for (i64 i = inv_fact_.size(); i <= n; ++i) { inv_fact_.push_back(i == 0 ? 1 : inv_fact_.back() / i); } return inv_fact_[n]; } static ModInt Comb(i64 n, i64 k) { if (!Valid(n, k)) return 0; return Perm(n, k) * InvFact(k); } static ModInt CombSlow(i64 n, i64 k) { if (!Valid(n, k)) return 0; return PermSlow(n, k) * InvFact(k); } static ModInt Perm(i64 n, i64 k) { if (!Valid(n, k)) return 0; dassert(n <= kMaxCacheSize && "n is too large. If k is small, consider using PermSlow."); return Fact(n) * InvFact(n - k); } static ModInt PermSlow(i64 n, i64 k) { if (!Valid(n, k)) return 0; ModInt p = 1; for (i64 i = 0; i < k; ++i) { p *= (n - i); } return p; } private: static bool Valid(i64 n, i64 k) { return 0 <= n && 0 <= k && k <= n; } i32 n_; inline static std::vector fact_; inline static std::vector inv_fact_; inline static std::vector inv_; static const i64 kMaxCacheSize = 10000000; }; #define DEFINE(op) \ template \ ModInt operator op(const T& t, const ModInt& m) { \ return ModInt(t) op m; \ } BIN_OPS(DEFINE) CMP_OPS(DEFINE) #undef DEFINE template std::ostream& operator<<(std::ostream& out, const ModInt& m) { out << m.value(); return out; } #endif // MODINT_H_ #ifndef CONSTANTS_H_ #define CONSTANTS_H_ // big = 2305843009213693951 = 2^61-1 ~= 2.3*10^18 const int64_t big = std::numeric_limits::max() / 4; #endif // CONSTANTS_H_ #ifndef DEBUG_H_ #define DEBUG_H_ #ifndef TYPE_TRAITS_H_ #define TYPE_TRAITS_H_ template struct is_dereferenceable : std::false_type {}; template struct is_dereferenceable())>> : std::true_type {}; template struct is_iterable : std::false_type {}; template struct is_iterable())), decltype(std::end(std::declval()))>> : std::true_type {}; template struct is_applicable : std::false_type {}; template struct is_applicable::value)>> : std::true_type {}; #endif // TYPE_TRAITS_H template void debug(std::ostream& os, const T& value, const Ts&... args); template void debug(std::ostream& os, const T& v) { if constexpr (std::is_same>::value) { if (v == big) { os << "big"; } else { os << v; } } else if constexpr (std::is_same>::value || std::is_same::value) { os << v; } else if constexpr (is_dereferenceable::value) { os << "{"; if (v) { debug(os, *v); } else { os << "nil"; } os << "}"; } else if constexpr (is_iterable::value) { os << "{"; for (auto it = std::begin(v); it != std::end(v); ++it) { if (it != std::begin(v)) os << ", "; debug(os, *it); } os << "}"; } else if constexpr (is_applicable::value) { os << "{"; std::apply([&os](const auto&... args) { debug(os, args...); }, v); os << "}"; } else { os << v; } } template void debug(std::ostream& os, const T& value, const Ts&... args) { debug(os, value); os << ", "; debug(os, args...); } #if DEBUG #define dbg(...) \ do { \ std::cerr << #__VA_ARGS__ << ": "; \ debug(std::cerr, __VA_ARGS__); \ std::cerr << " (L" << __LINE__ << ")\n"; \ } while (0) #else #define dbg(...) #endif #endif // DEBUG_H_ #ifndef FIX_H_ #define FIX_H_ template struct FixPoint { F f; template decltype(auto) operator()(Args&&... args) const { return f(std::ref(*this), std::forward(args)...); } }; template FixPoint> Fix(F&& f) { return {std::forward(f)}; } #endif // FIX_H_ #ifndef IO_H_ #define IO_H void read_from_cin() {} template void read_from_cin(T& value, Ts&... args) { std::cin >> value; read_from_cin(args...); } #define rd(type, ...) \ type __VA_ARGS__; \ read_from_cin(__VA_ARGS__); #define ints(...) rd(int, __VA_ARGS__); #define strings(...) rd(string, __VA_ARGS__); const char *yes_str = "Yes", *no_str = "No"; template void write_to_cout(const T& value) { if constexpr (std::is_same::value) { std::cout << (value ? yes_str : no_str); } else if constexpr (is_iterable::value && !std::is_same::value) { for (auto it = std::begin(value); it != std::end(value); ++it) { if (it != std::begin(value)) std::cout << " "; std::cout << *it; } } else { std::cout << value; } } template void write_to_cout(const T& value, const Ts&... args) { write_to_cout(value); std::cout << ' '; write_to_cout(args...); } #define wt(...) \ do { \ write_to_cout(__VA_ARGS__); \ cout << '\n'; \ } while (0) template std::istream& operator>>(std::istream& is, std::vector& v) { for (T& vi : v) is >> vi; return is; } template std::istream& operator>>(std::istream& is, std::pair& p) { is >> p.first >> p.second; return is; } #endif // IO_H_ #ifndef MACROS_H_ #define MACROS_H_ #define all(x) (x).begin(), (x).end() #define eb(...) emplace_back(__VA_ARGS__) #define pb(...) push_back(__VA_ARGS__) #define dispatch(_1, _2, _3, name, ...) name #define as_i64(x) \ ( \ [] { \ static_assert( \ std::is_integral< \ typename std::remove_reference::type>::value, \ "rep macro supports std integral types only"); \ }, \ static_cast(x)) #define rep3(i, a, b) for (int64_t i = as_i64(a); i < as_i64(b); ++i) #define rep2(i, n) rep3(i, 0, n) #define rep1(n) rep2(_loop_variable_, n) #define rep(...) dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__) #define rrep3(i, a, b) for (int64_t i = as_i64(b) - 1; i >= as_i64(a); --i) #define rrep2(i, n) rrep3(i, 0, n) #define rrep1(n) rrep2(_loop_variable_, n) #define rrep(...) dispatch(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__) #define each3(k, v, c) for (auto&& [k, v] : c) #define each2(e, c) for (auto&& e : c) #define each(...) dispatch(__VA_ARGS__, each3, each2)(__VA_ARGS__) template bool chmax(T& a, U b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, U b) { if (a > b) { a = b; return true; } return false; } template auto max(T a, U b) { return a > b ? a : b; } template auto min(T a, U b) { return a < b ? a : b; } template auto max(const T& v) { return *std::max_element(v.begin(), v.end()); } template auto min(const T& v) { return *std::min_element(v.begin(), v.end()); } template int64_t sz(const T& v) { return std::size(v); } template int64_t popcount(T i) { return std::bitset::digits>(i).count(); } template bool hasbit(T s, int i) { return std::bitset::digits>(s)[i]; } template auto div_floor(T n, U d) { if (d < 0) { n = -n; d = -d; } if (n < 0) { return -((-n + d - 1) / d); } return n / d; }; template auto div_ceil(T n, U d) { if (d < 0) { n = -n; d = -d; } if (n < 0) { return -(-n / d); } return (n + d - 1) / d; } template bool even(T x) { return x % 2 == 0; } std::array, 4> adjacent(int64_t i, int64_t j) { return {{{i + 1, j}, {i, j + 1}, {i - 1, j}, {i, j - 1}}}; } bool inside(int64_t i, int64_t j, int64_t I, int64_t J) { return 0 <= i && i < I && 0 <= j && j < J; } template void sort(T& v) { return std::sort(v.begin(), v.end()); } template void sort(T& v, Compare comp) { return std::sort(v.begin(), v.end(), comp); } template void reverse(T& v) { return std::reverse(v.begin(), v.end()); } template typename T::value_type accumulate(const T& v) { return std::accumulate(v.begin(), v.end(), typename T::value_type()); } using i64 = int64_t; using i32 = int32_t; template using low_priority_queue = std::priority_queue, std::greater>; template using V = std::vector; template using VV = V>; #endif // MACROS_H_ void Main(); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout << std::fixed << std::setprecision(20); Main(); return 0; } using namespace std; #define int i64 using mint = ModInt<>; void Main() { ints(n); V a(n); cin >> a; Compressor c(a); int N = sz(c.coord); mint ans = 0; { V p(n); { AddSegmentTree t(N); rep(i, n) { int ci = c(a[i]); p[i] = t.Aggregate(0, ci); t.Set(ci, t.Get(ci) + mint(2).Pow(i)); } } { AddSegmentTree t(N); rrep(i, n) { int ci = c(a[i]); p[i] *= t.Aggregate(0, ci); t.Set(ci, t.Get(ci) + mint(2).Pow(n - 1 - i)); } } ans += accumulate(p); } { V p(n); { AddSegmentTree t(N); rep(i, n) { int ci = c(a[i]); p[i] = t.Aggregate(ci + 1, N); t.Set(ci, t.Get(ci) + mint(2).Pow(i)); } } { AddSegmentTree t(N); rrep(i, n) { int ci = c(a[i]); p[i] *= t.Aggregate(ci + 1, N); t.Set(ci, t.Get(ci) + mint(2).Pow(n - 1 - i)); } } ans += accumulate(p); } wt(ans); }