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 N1≤i<j≤N
-
\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^52≤N≤5×105
-
1 \leq a_i \leq N \, (1 \leq i \leq N)1≤ai≤N(1≤i≤N)
-
All values in input are integers.
Input
Input is given from Standard Input in the following format:
NNa_1a1\ldots…a_NaN
Output
Print the answer.
HINT
(i,j)=(1,4),(2,3) satisfy the conditions.