#include #include using namespace std; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) #define ll long long #define cout std::cout #define yes cout<<"Yes"<<"\n" #define no cout<<"No"<<"\n" #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #define allr(x) (x).rbegin(),(x).rend() #define SUM(v) accumulate(all(v), 0LL) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define pii pair #define pll pair #define pb push_back #define eb emplace_back #define ff first #define ss second #define vi vector #define vll vector #define vc vector #define vvi vector> #define vec(type, name, ...) vector name(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ IN(name) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } template void print(const T &a) { cout << a; } void OUT() { cout << endl; } template void OUT(const Head &head, const Tail &...tail) { print(head); if(sizeof...(tail)) cout << ' '; OUT(tail...); } #define vv(type, name, h, ...) vector> name(h, vector(__VA_ARGS__)) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ IN(name) #define vvv(type, name, h, w, ...) vector>> name(h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name(a, vector>>(b, vector>(c, vector(__VA_ARGS__)))) template using min_priority_queue = priority_queue, greater>; template pair operator-(const pair &x, const pair &y) { return pair(x.ff - y.ff, x.ss - y.ss); } template pair operator+(const pair &x, const pair &y) { return pair(x.ff + y.ff, x.ss + y.ss); } template pair operator&(const pair &l, const pair &r) { return pair(max(l.ff, r.ff), min(l.ss, r.ss)); } template vector &operator--(vector &v) { fore(e, v) e--; return v; } template vector operator--(vector &v, int) { auto res = v; fore(e, v) e--; return res; } 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; } #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) //座標圧縮 template void zip(vector &x) { vector y = x; UNIQUE(y); for(int i = 0; i < x.size(); ++i) { x[i] = lb(y, x[i]); } } template T ceil(T x, T y) { assert(y >= 1); return (x > 0 ? (x + y - 1) / y : x / y); } template T floor(T x, T y) { assert(y >= 1); return (x > 0 ? x / y : (x + y - 1) / y); } long long POW(long long x, int n) { long long res = 1LL; for(; n; n >>= 1, x *= x) if(n & 1) res *= x; return res; } //0^n=0 long long modpow(long long a, long long n, long long mod) { a%=mod; assert(a!=0||n!=0); if(a==0)return 0; long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } //return 0<=a&&a> x; return x; } ll lin() { unsigned long long x; cin >> x; return x; } long long sqrtll(long long x) { assert(x >= 0); long long rev = sqrt(x); while(rev * rev > x) --rev; while((rev+1) * (rev+1)<=x) ++rev; return rev; } int logN(long long n){ int ret=1; while((1LL<::max() / 2; const long long INFL = numeric_limits::max() / 2; #define inf INFINITY template void debug(vector a){ rep(i,0,(int)a.size()){ cout< class modint { using u64 = std::uint_fast64_t; public: u64 a; constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {} constexpr u64 &val() noexcept { return a; } constexpr const u64 &val() const noexcept { return a; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } constexpr modint &operator+=(const modint rhs) noexcept { a += rhs.a; if (a >= Modulus) { a -= Modulus; } return *this; } constexpr modint &operator-=(const modint rhs) noexcept { if (a < rhs.a) { a += Modulus; } a -= rhs.a; return *this; } constexpr modint &operator*=(const modint rhs) noexcept { a = a * rhs.a % Modulus; return *this; } constexpr modint &operator/=(modint rhs) noexcept { u64 exp = Modulus - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } friend bool operator==(const modint& a,const modint& b) { return a.val()==b.val(); } friend bool operator!=(const modint& a,const modint& b) { return a.val()!=b.val(); } }; using mint9=modint<998244353>; using mint1=modint<1000000007>; //基本的にcostを指定しないと重みなし辺になります struct Edge{ int from,to; ll cost; Edge()=default; Edge(int from,int to,ll cost=1):from(from),to(to),cost(cost){} operator int() const {return to;} }; constexpr pii dx4[4] = {pii{-1, 0},pii{0, -1}, pii{0, 1}, pii{1, 0} }; constexpr pii dx8[8] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; constexpr pii dx[2][8] = { {{-1,-1},{-1,0},{0,-1},{0,1},{1,-1},{1,0}},{{-1,0},{0,-1},{1,0},{-1,1},{0,1},{1,1}}}; #define el "\n" #define endl "\n" #define fastio std::cin.sync_with_stdio(false);std::cin.tie(nullptr),cout< struct LazySegmentTree { private: int n{}, sz{}, height{}; vector< T > data; vector< E > lazy; const F f; const G g; const H h; const T ti; const E ei; inline void update(int k) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } inline void all_apply(int k, const E &x) { data[k] = g(data[k], x); if(k < sz) lazy[k] = h(lazy[k], x); } inline void propagate(int k) { if(lazy[k] != ei) { all_apply(2 * k + 0, lazy[k]); all_apply(2 * k + 1, lazy[k]); lazy[k] = ei; } } public: LazySegmentTree() = default; explicit LazySegmentTree(int n, const F f, const G g, const H h,const T &ti, const E &ei) : n(n), f(f), g(g), h(h), ti(ti), ei(ei) { sz = 1; height = 0; while(sz < n) sz <<= 1, height++; data.assign(2 * sz, ti); lazy.assign(2 * sz, ei); } explicit LazySegmentTree(const vector< T > &v, const F f, const G g, const H h,const T &ti, const E &ei) : LazySegmentTree(v.size(), f, g, h, ti, ei) { build(v); } void build(const vector< T > &v) { assert(n == (int) v.size()); for(int k = 0; k < n; k++) data[k + sz] = v[k]; for(int k = sz - 1; k > 0; k--) update(k); } void set(int k, const T &x) { k += sz; for(int i = height; i > 0; i--) propagate(k >> i); data[k] = x; for(int i = 1; i <= height; i++) update(k >> i); } T get(int k) { k += sz; for(int i = height; i > 0; i--) propagate(k >> i); return data[k]; } T operator[](int k) { return get(k); } T prod(int l, int r) { if(l >= r) return ti; l += sz; r += sz; for(int i = height; i > 0; i--) { if(((l >> i) << i) != l) propagate(l >> i); if(((r >> i) << i) != r) propagate((r - 1) >> i); } T L = ti, R = ti; for(; l < r; l >>= 1, r >>= 1) { if(l & 1) L = f(L, data[l++]); if(r & 1) R = f(data[--r], R); } return f(L, R); } T all_prod() const { return data[1]; } void apply(int k, const E &x) { k += sz; for(int i = height; i > 0; i--) propagate(k >> i); data[k] = g(data[k], x); for(int i = 1; i <= height; i++) update(k >> i); } void apply(int l, int r, const E &x) { if(l >= r) return; l += sz; r += sz; for(int i = height; i > 0; i--) { if(((l >> i) << i) != l) propagate(l >> i); if(((r >> i) << i) != r) propagate((r - 1) >> i); } { int l2 = l, r2 = r; for(; l < r; l >>= 1, r >>= 1) { if(l & 1) all_apply(l++, x); if(r & 1) all_apply(--r, x); } l = l2, r = r2; } for(int i = 1; i <= height; i++) { if(((l >> i) << i) != l) update(l >> i); if(((r >> i) << i) != r) update((r - 1) >> i); } } template< typename C > int find_first(int l, const C &check) { if(l >= n) return n; l += sz; for(int i = height; i > 0; i--) propagate(l >> i); T sum = ti; do { while((l & 1) == 0) l >>= 1; if(check(f(sum, data[l]))) { while(l < sz) { propagate(l); l <<= 1; auto nxt = f(sum, data[l]); if(not check(nxt)) { sum = nxt; l++; } } return l + 1 - sz; } sum = f(sum, data[l++]); } while((l & -l) != l); return n; } template< typename C > int find_last(int r, const C &check) { if(r <= 0) return -1; r += sz; for(int i = height; i > 0; i--) propagate((r - 1) >> i); T sum = ti; do { r--; while(r > 1 and (r & 1)) r >>= 1; if(check(f(data[r], sum))) { while(r < sz) { propagate(r); r = (r << 1) + 1; auto nxt = f(data[r], sum); if(not check(nxt)) { sum = nxt; r--; } } return r - sz; } sum = f(data[r], sum); } while((r & -r) != r); return -1; } }; template< typename T, typename E, typename F, typename G, typename H > LazySegmentTree< T, E, F, G, H > get_lazy_segment_tree(int N, const F &f, const G &g, const H &h, const T &ti, const E &ei) { return LazySegmentTree{N, f, g, h, ti, ei}; } template< typename T, typename E, typename F, typename G, typename H > LazySegmentTree< T, E, F, G, H > get_lazy_segment_tree(const vector< T > &v, const F &f, const G &g, const H &h, const T &ti, const E &ei) { return LazySegmentTree{v, f, g, h, ti, ei}; } int main(){ fastio INT(n); VEC(ll,a,n); struct node{ mint9 len; ll mini,maxi; mint9 min_sum,max_sum; mint9 val; }; auto f=[](node a,node b){ a.len+=b.len; chmin(a.mini,b.mini); chmax(a.maxi,b.maxi); a.min_sum+=b.min_sum; a.max_sum+=b.max_sum; a.val+=b.val; return a; }; auto g=[](node a,pll t){ //順に処理していく if(t.ff!=-INFL){ a.mini=t.ff; a.min_sum=(mint9)t.ff*a.len; a.val=(mint9)t.ff*a.max_sum; } if(t.ss!=-INFL){ a.maxi=t.ss; a.max_sum=(mint9)t.ss*a.len; a.val=(mint9)t.ss*a.min_sum; } return a; }; auto h=[](pll a,pll b){ if(b.ff!=-INFL){ a.ff=b.ff; } if(b.ss!=-INFL){ a.ss=b.ss; } return a; }; auto seg=get_lazy_segment_tree(n,f,g,h,node{0,INFL,-INFL,0,0,0},pll(-INFL,-INFL)); mint9 ans=0; rep(i,0,n){ seg.set(i,node{1,a[i],a[i],a[i],a[i],a[i]*a[i]}); { int ok=i,ng=-1; while(abs(ok-ng)>1){ int mid=(ok+ng)>>1; if(seg[mid].mini>=a[i])ok=mid; else ng=mid; } seg.apply(ok,i+1,pll(a[i],-INFL)); } { int ok=i,ng=-1; while(abs(ok-ng)>1){ int mid=(ok+ng)>>1; if(seg[mid].maxi<=a[i])ok=mid; else ng=mid; } seg.apply(ok,i+1,pll(-INFL,a[i])); } ans+=seg.prod(0,n).val; } OUT(ans.val()); return 0; } /* 区間更新みたいな感じ 一番下のノード以外miniとmaxiはあんま重要ではない */