import sys input = sys.stdin.buffer.readline MOD = 998244353 class FenwickTree(): def __init__(self, n): self.n = n self.data = [0] * n def build(self, arr): for i, a in enumerate(arr): self.data[i] = a for i in range(1, self.n + 1): if i + (i & -i) <= self.n: self.data[i + (i & -i) - 1] += self.data[i - 1] self.data[i + (i & -i) - 1] %= MOD def add(self, p, x): p += 1 while p <= self.n: self.data[p - 1] += x self.data[p - 1] %= MOD p += p & -p def sum(self, r): s = 0 while r: s += self.data[r - 1] s %= MOD r -= r & -r return s def get(self, p): return self.range_sum(p, p + 1) def range_sum(self, l, r): return self.sum(r) - self.sum(l) N = int(input()) arr = list(map(int, input().split())) sx = sorted(set(arr)) cx = {v : k for k, v in enumerate(sx)} bit1 = FenwickTree(N + 1) bit2 = FenwickTree(N + 1) bit3 = FenwickTree(N + 1) bit4 = FenwickTree(N + 1) res = 0 for a in arr: p = cx[a] res += bit4.range_sum(p + 1, N + 1) + bit3.range_sum(p + 1, N + 1) * a res %= MOD bit3.add(p, bit1.range_sum(p + 1, N + 1) % MOD) bit4.add(p, bit2.range_sum(p + 1, N + 1) + bit1.range_sum(p + 1, N + 1) * a) bit1.add(p, 1) bit2.add(p, a) print(res)