Problem2388--Min Max Pair

2388: Min Max Pair

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 128 MB

Description

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

You are given a sequence a = (a_1, \dots, a_N)a=(a1,,aN) of length NN consisting of integers between 11 and NN.
Find the number of pairs of integers i, ji,j that satisfy all of the following conditions:
  • 1 \leq i \lt j \leq N1i<jN
  • \min(a_i, a_j) = imin(ai,aj)=i
  • \max(a_i, a_j) = jmax(ai,aj)=j

Constraints

  • 2 \leq N \leq 5 \times 10^52N5×105
  • 1 \leq a_i \leq N \, (1 \leq i \leq N)1aiN(1iN)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:
NNa_1a1\ldotsa_NaN

Output

Print the answer.

Sample Input Copy

4
1 3 2 4

Sample Output Copy

2

HINT

(i,j)=(1,4),(2,3) satisfy the conditions.

Source/Category