#P6375. Mountain Journey Expected Energy Cost
Mountain Journey Expected Energy Cost
Mountain Journey Expected Energy Cost
You are given n mountains placed in a row. The ith mountain has a height hi. Xiao Z is initially standing on the unique highest mountain and wants to reach the unique lowest mountain. He can move according to the following rules:
- He may move from his current mountain to any mountain with a strictly lower height. (This move costs |i - j| energy if he moves from mountain i to mountain j.)
- He may move from his current mountain to any different mountain with the same height, but for each distinct height this move can only be used once over the entire journey. (This move also costs |i - j| energy.)
Whenever there are multiple available moves, Xiao Z chooses one uniformly at random. Compute the expected total energy cost required for Xiao Z to reach the lowest mountain, and output the result modulo 998244353. Note that if the expected value is a rational number \(\frac{p}{q}\), you should output \(p \cdot q^{-1}\) modulo 998244353 (where \(q^{-1}\) is the modular inverse of \(q\) modulo 998244353).
Note: All formulae are in LaTeX; for example, the cost to move from mountain \(i\) to \(j\) is \(|i-j|\) and the allowed same-height move can be used at most once for each distinct height.
inputFormat
The input consists of two lines:
- The first line contains an integer n — the number of mountains.
- The second line contains n integers h1, h2, \dots, hn — the heights of the mountains in order.
It is guaranteed that the highest mountain is unique and the lowest mountain is unique.
outputFormat
Output a single integer which is the expected energy cost modulo 998244353.
sample
3
5 4 12