local mfl, mce = math.floor, math.ceil local mmi, mma = math.min, math.max local bls, brs = bit.lshift, bit.rshift local mod = 998244353 local function bmul(x, y) local x0, y0 = x % 31596, y % 31596 local x1, y1 = mfl(x / 31596), mfl(y / 31596) return (x1 * y1 * 62863 + (x1 * y0 + x0 * y1) * 31596 + x0 * y0) % mod end local function badd(x, y) return (x + y) % mod end local function bsub(x, y) return x < y and x - y + mod or x - y end local SegTree = {} SegTree.updateAll = function(self) for i = self.stagenum - 1, 1, -1 do local cnt = bls(1, i - 1) for j = 1, cnt do self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2]) end end end SegTree.create = function(self, n, func, emptyvalue) self.func, self.emptyvalue = func, emptyvalue local stagenum, mul = 1, 1 self.stage = {{}} while mul < n do mul, stagenum = mul * 2, stagenum + 1 self.stage[stagenum] = {} end self.stagenum = stagenum self.left_stage = {} for i = 1, n do local sp, sz = 1, bls(1, stagenum - 1) while(i - 1) % sz ~= 0 do sp, sz = sp + 1, brs(sz, 1) end self.left_stage[i] = sp end self.sz_stage = {} local tmp, sp = 1, stagenum for i = 1, n do if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end self.sz_stage[i] = sp end for i = 1, mul do self.stage[stagenum][i] = emptyvalue end self:updateAll() end SegTree.getRange = function(self, left, right) if left == right then return self.stage[self.stagenum][left] end local stagenum = self.stagenum local ret = self.emptyvalue while left <= right do local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1]) local sz = bls(1, stagenum - stage) ret = self.func(ret, self.stage[stage][1 + brs(left - 1, stagenum - stage)]) left = left + sz end return ret end SegTree.update = function(self, idx) for i = self.stagenum - 1, 1, -1 do local dst = brs(idx + 1, 1) local rem = dst * 4 - 1 - idx self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem]) idx = dst end end SegTree.setValue = function(self, idx, value, silent) self.stage[self.stagenum][idx] = value if not silent then self:update(idx) end end SegTree.addValue = function(self, idx, value) self.stage[self.stagenum][idx] = self.func(self.stage[self.stagenum][idx], value) self:update(idx) end SegTree.new = function(n, func, emptyvalue) local obj = {} setmetatable(obj, {__index = SegTree}) obj:create(n, func, emptyvalue) return obj end local n = io.read("*n") local a = {} local ainv = {} for i = 1, n do a[i] = io.read("*n") if not ainv[a[i]] then ainv[a[i]] = 1 else ainv[a[i]] = ainv[a[i]] + 1 end end local asorted = {} for k, _u in pairs(ainv) do table.insert(asorted, k) end table.sort(asorted) for i = 1, #asorted do ainv[asorted[i]] = i end local m = #asorted local stcnt = SegTree.new(m, function(a, b) return a + b end, 0) local stsum = SegTree.new(m, function(a, b) return badd(a, b) end, 0) local asum = 0 for i = 1, n do asum = badd(asum, a[i]) end local leftcnt, leftsum = {}, {} for i = 1, n do local v = a[i] local vidx = ainv[v] if vidx < m then leftcnt[i] = stcnt:getRange(vidx + 1, m) leftsum[i] = stsum:getRange(vidx + 1, m) else leftcnt[i] = 0 leftsum[i] = 0 end stcnt:addValue(vidx, 1) stsum:addValue(vidx, v) end local rightcnt, rightsum = {}, {} for i = 1, n do rightcnt[i], rightsum[i] = 0, 0 end for i = 1, m do stcnt:setValue(i, 0, true) stsum:setValue(i, 0, true) end stcnt:updateAll() stsum:updateAll() for i = n, 1, -1 do local v = a[i] local vidx = ainv[v] if 1 < vidx then rightcnt[i] = stcnt:getRange(1, vidx - 1) rightsum[i] = stsum:getRange(1, vidx - 1) else rightcnt[i], rightsum[i] = 0, 0 end stcnt:addValue(vidx, 1) stsum:addValue(vidx, v) end local ret = 0 for i = 1, n do local center = bmul(a[i], bmul(leftcnt[i], rightcnt[i])) local left = bmul(leftsum[i], rightcnt[i]) local right = bmul(leftcnt[i], rightsum[i]) ret = badd(ret, center + left + right) end print(ret)