#P10173. 「OICon-02」maxiMINImax

「OICon-02」maxiMINImax

Problem Description

You are given a permutation aa of length nn. Define the minimum value of aia_i in a subarray [l,r][l,r] as min[l,r]\min_{[l,r]}, and the maximum value of aia_i in [l,r][l,r] as max[l,r]\max_{[l,r]}. For all triples of subarrays ([l1,r1],[l2,r2],[l3,r3])([l_1,r_1],[l_2,r_2],[l_3,r_3]) such that 1l1r1<l2r2<l3r3n1\leq l_1\leq r_1<l_2\leq r_2<l_3\leq r_3\leq n, compute the sum of $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})$, and output the result modulo 97121769712176.

Input Format

The first line contains a positive integer nn, the length of the permutation.

The second line contains nn positive integers aa, representing the given permutation aa.

Output Format

Output one line with a positive integer: the sum of $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})$ over all valid triples, modulo 97121769712176.

4
1 3 4 2
14
10
1 3 6 2 7 9 4 10 8 5
1992

Hint

Sample Explanation

For sample 11:

  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([1,1],[2,2],[3,3])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=0$.
  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([1,1],[2,2],[3,4])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=0$.
  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([1,1],[2,2],[4,4])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=2$.
  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([1,1],[2,3],[4,4])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=2$.
  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([1,1],[3,3],[4,4])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=6$.
  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([1,2],[3,3],[4,4])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=2$.
  • $([l_1,r_1],[l_2,r_2],[l_3,r_3])=([2,2],[3,3],[4,4])$: $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})=2$.

The total sum of $\max(0,\min_{[l_2,r_2]}-\max_{[l_1,r_1]})\times\max(0,\min_{[l_2,r_2]}-\max_{[l_3,r_3]})$ over all ([l1,r1],[l2,r2],[l3,r3])([l_1,r_1],[l_2,r_2],[l_3,r_3]) is 0+0+2+2+6+2+2=140+0+2+2+6+2+2=14.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} Special Property Score\text{Score}
11 n60n\leq60 55
22 n100n\leq100 99
33 n200n\leq200
44 n500n\leq500
55 n2000n\leq2000 1919
66 n6000n\leq6000 1111
77 n105n\leq10^5 1919
88 No special restrictions

For 100%100\% of the data: 1n1061\leq n\leq10^6, 1ain1\leq a_i\leq n, and aa is guaranteed to be a permutation of {1,2,,n}\{1,2,\dots,n\}.

Translated by ChatGPT 5