#include #if __has_include() #include #include #endif using namespace std; #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define RDVL(T,n,...) vec__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);lin(__VA_ARGS__) #define VL(n,...) RDVL(ll,n,__VA_ARGS__) #define fo(i,...) for(auto[i,i##stop,i##step]=for_range(0,__VA_ARGS__);ivoid pp(const auto&...a){[[maybe_unused]]const char*c="";((o<(a...);} #define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{ #define use_ml998244353 using ml=atcoder::modint998244353; namespace my{ auto&operator<<(ostream&o,const atcoder::modint998244353&x){return o<<(int)x.val();} void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};} void lin(auto&...a){(cin>>...>>a);} templateusing pack_back_t=tuple_element_t>; } namespace my{ templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateconstexpr int depth=0; templatestruct core_t_helper{using type=T;}; templateusing core_t=core_t_helper::type; templatestruct vec; templatestruct hvec_helper{using type=vec::type>;}; templatestruct hvec_helper<0,T>{using type=T;}; templateusing hvec=hvec_helper::type; templatestruct vec:vector{ static constexpr int D=depth+1; using C=core_t; using vector::vector; void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else{ }} static auto make(ll n,const auto&...a){ if constexpr(sizeof...(a)==1)return vec(n,array{a...}[0]); else { } } ll size()const{return vector::size();} auto iota()const{vecr(size());std::iota(r.begin(),r.end(),0);return r;} template>auto stable_sort(F f={})const{vec v=*this;ranges::stable_sort(v,f);return v;} template>auto arg_sort(F f={})const{return iota().stable_sort([&](ll i,ll j){return f((*this)[i],(*this)[j]);});} auto inverse_permutation()const{return arg_sort();} template>auto zip_distinct(F f={})const{return arg_sort(f).inverse_permutation();} auto rev()const{vec v=*this;ranges::reverse(v);return v;} }; templaterequires(sizeof...(A)>=2)vec(const A&...a)->vec>>; } namespace my{ namespace sgt{ templateT identity(){return x;} templateT identity(){return T{};} templateT add(T a,T b){return a+b;} templateS mul_mapping(F f,S x){return x*f;} templateF mul_composition(F f,F g){return g*f;} templatestruct lazyseg_mul_sum:atcoder::lazy_segtree,identity,T,mul_mapping,mul_composition,identity>{ using base=atcoder::lazy_segtree,identity,T,mul_mapping,mul_composition,identity>; lazyseg_mul_sum(const vec&a):base(vec(a.size(),1)){} }; } } namespace my{entry void main(){ LL(N); VL(N,A); auto rank=A.zip_distinct(); // 同じ値も順序づけ use_ml998244353 ml inv2=ml(1)/2; // cnt(l,r,x)=#{i|l<=i<=r \land a[i]&rank){ sgt::lazyseg_mul_sums(vec(N,1)); vecres(N); fe(rank.arg_sort(),i){ res[i]=s.get(i).inv()*s.prod(0,i+1); s.apply(i+1,N,inv2); } return res; }; ml ans=0; vecl=calc(rank); vecr=calc(rank.rev()).rev(); fo(i,N)ans+=A[i]*l[i]*r[i]; pp(ans); }}