#include #include #include namespace nono { template class FenwickTree { public: FenwickTree(const int size) : size_(size + 1), data_(size + 1) {} FenwickTree(const std::vector &vec) : size_(vec.size() + 1), data_(vec.size() + 1) { for (int i = 0; i < size_; ++i) { add(i, vec[i]); } } void add(int i, const T delta) { for (i++; i < size_; i += i & -i) { data_[i] += delta; } } T get_range(const int l, const int r) { return get_sum(r) - get_sum(l); } T operator[](const int i) { return get_range(i, i + 1); } private: const int size_; std::vector data_; T get_sum(int i) { T sum = 0; for (; i > 0; i -= i & -i) { sum += data_[i]; } return sum; } }; } // namespace nono using mint = atcoder::modint998244353; int main() { int n; std::cin >> n; std::vector p(n); for (int i = 0; i < n; i++) std::cin >> p[i]; nono::FenwickTree count(n); nono::FenwickTree fen(n); mint ans = 0; mint up = 1; mint down = mint(2).pow(n - 1); for (int i = 0; i < n; i++) { p[i]--; ans += count.get_range(p[i], n) * mint(2).pow(n - 1) - fen.get_range(p[i], n) * down; count.add(p[i], 1); fen.add(p[i], up); up *= 2; down /= 2; } std::cout << ans.val() << '\n'; }