atcoder#ABC234G. [ABC234G] Divide a Sequence

[ABC234G] Divide a Sequence

Score : 600600 points

Problem Statement

Given is a sequence AA of NN numbers.

There are 2N12^{N-1} ways to divide AA into non-empty contiguous subsequences B1,B2,,BkB_1,B_2,\ldots,B_k. Find the value below for each of those ways, and print the sum, modulo 998244353998244353, of those values.

  • i=1k(max(Bi)min(Bi))\prod_{i=1}^{k} (\max(B_i)-\min(B_i))

Here, for a sequence Bi=(Bi,1,Bi,2,,Bi,j)B_i=(B_{i,1},B_{i,2},\ldots,B_{i,j}), max(Bi)\max(B_i) and min(Bi)\min(B_i) are defined to be the maximum and minimum values of an element of BiB_i, respectively.

Constraints

  • 1N3×1051 \leq N \leq 3 \times 10^5
  • 1Ai1091 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the sum, modulo 998244353998244353, of the values found.

3
1 2 3
2

There are 44 ways to divide A=(1,2,3)A=(1,2,3) into non-empty contiguous subsequences, as follows.

  • (1)(1), (2)(2), (3)(3)
  • (1)(1), (2,3)(2,3)
  • (1,2)(1,2), (3)(3)
  • (1,2,3)(1,2,3)

i=1k(max(Bi)min(Bi))\prod_{i=1}^{k} (\max(B_i)-\min(B_i)) for these divisions are 00, 00, 00, 22, respectively. The sum of them, 22, should be printed.

4
1 10 1 10
90
10
699498050 759726383 769395239 707559733 72435093 537050110 880264078 699299140 418322627 134917794
877646588

Be sure to print the sum modulo 998244353998244353.