#P9461. Sum of Minimum Modes over Subarrays

    ID: 22612 Type: Default 1000ms 256MiB

Sum of Minimum Modes over Subarrays

Sum of Minimum Modes over Subarrays

You are given an integer sequence ( a = [a_1, a_2, \dots, a_n] ). Using this sequence, we construct a new sequence ( b ) by the following process:

  1. Start with an empty sequence ( b ).
  2. For each ( i = 1, 2, \dots, n ), append the numbers ( 1, 2, \dots, a_i ) to the end of ( b ).

For any contiguous subarray (subinterval) of ( b ), define its minimum mode as follows: determine the frequency of each number in the subarray; let the mode be any number that appears the maximum number of times, and among these, choose the smallest one. For example, for the subarray ( [1, 1, 4, 5, 1, 4] ) the minimum mode is ( 1 ) since ( 1 ) appears three times, and for ( [1, 14, 5, 14, 19, 19, 8, 10] ) both ( 14 ) and ( 19 ) appear twice so the minimum mode is ( 14 ).

Your task is to compute the sum of the minimum modes of every subarray of ( b ) and output the result modulo ( 998244353 ).

inputFormat

The first line contains an integer ( n ) denoting the length of sequence ( a ). The second line contains ( n ) space-separated integers ( a_1, a_2, \dots, a_n ).

outputFormat

Output a single integer: the sum of the minimum modes of all contiguous subarrays of ( b ) taken modulo ( 998244353 ).

sample

2
1 2
7