const val MOD = 998244353 class Bit(val size: Int) { private val vec = IntArray(size) fun get(position: Int): Int { var result = 0 var pos = position while (pos in vec.indices) { result = (result + vec[pos]) % MOD pos -= pos.inv() and (pos + 1) } return result } fun add(position: Int, value: Int) { var pos = position while (pos in vec.indices) { vec[pos] = (vec[pos] + value) % MOD pos += pos.inv() and (pos + 1) } } fun add(position: Int, value: Long) { return add(position, (value % MOD).toInt()) } } fun main() { val n = readLine()!!.trim().toInt() val num = readLine()!!.trim().split(' ').map(String::toInt) val rank = num.distinct().sorted().let { list -> list.indices.associateBy { list[it] }} val sumBits = Array(2){Bit(rank.size)} val countBits = Array(2){Bit(rank.size)} var result = 0L for (a in num.reversed()) { val i = rank[a]!! val sum2 = sumBits[1].get(i - 1) val count2 = countBits[1].get(i - 1) result = (result + sum2 + count2.toLong() * a) % MOD val sum1 = sumBits[0].get(i - 1) val count1 = countBits[0].get(i - 1) sumBits[1].add(i, sum1 + count1.toLong() * a) countBits[1].add(i, count1) sumBits[0].add(i, a) countBits[0].add(i, 1) } println(result) }