import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] sa = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } br.close(); Obj[] arr = new Obj[n]; for (int i = 0; i < n; i++) { Obj o = new Obj(); o.i = i + 1; o.a = a[i]; arr[i] = o; } Arrays.sort(arr, (o1, o2) -> { if (o1.a != o2.a) { return o1.a - o2.a; } else { return o1.i - o2.i; } }); int mod = 998244353; BIT bit1 = new BIT(n + 1); BIT bit1c = new BIT(n + 1); BIT bit2 = new BIT(n + 1); BIT bit2c = new BIT(n + 1); long ans = 0; for (int i = 0; i < n; i++) { Obj o = arr[i]; long v2 = bit2.sum(n) - bit2.sum(o.i); long c2 = bit2c.sum(n) - bit2c.sum(o.i); long val = o.a * c2 + v2; ans += val; ans %= mod; long v1 = bit1.sum(n) - bit1.sum(o.i); long c1 = bit1c.sum(n) - bit1c.sum(o.i); long p = o.a * c1 + v1; bit2.add(o.i, p % mod); bit2c.add(o.i, c1); bit1.add(o.i, o.a); bit1c.add(o.i, 1); } System.out.println(ans); } static class Obj { int i, a; } static class BIT { int n; long[] arr; public BIT(int n) { this.n = n; arr = new long[n + 1]; } void add(int idx, long val) { for (int i = idx; i <= n; i += i & -i) { arr[i] += val; } } long sum(int idx) { long sum = 0; for (int i = idx; i > 0; i -= i & -i) { sum += arr[i]; } return sum; } } }