Given a sequence of positive integers a1,a2,…,an, a triplet (i,j,k) is called a "beautiful triplet" if it satisfies the following conditions:
- 1≤i<j<k≤n.
- Define the consecutive subsequence ai,ai+1,…,aj as A and the consecutive subsequence aj,aj+1,…,ak as B. The subsequences A and B satisfy:
- Every value that appears in A also appears in B.
- Every value that appears in B also appears in A.
**Objective**: Given the sequence a, count the number of beautiful triplets (i,j,k) in the sequence.
#### Input
- The first line contains an integer n (1≤n≤105).
- The second line contains n integers a1,a2,…,an (1≤ai≤106).
#### Output
- Output a single integer, which is the number of beautiful triplets in the given sequence.
#### Example
Input:
73121231
Output:
4
Explanation:
The valid beautiful triplets (i,j,k) are:
1. (1,3,5)
2. (1,4,6)
3. (2,4,6)
4. (3,5,7)
#### Subtasks
- **Subtask 1 (20 points)**: n≤100.
- **Subtask 2 (20 points)**: n≤1000.
- **Subtask 3 (20 points)**: n≤104.
- **Subtask 4 (20 points)**: Each value appears exactly twice.
- **Subtask 5 (20 points)**: No additional constraints.