#include<bits/stdc++.h>
using namespace std;
// my choco pie
#define ft first
#define sd second
#define pb push_back
#define mp make_pair
#define PII pair<int,int>
#define all(x) x.begin(),x.end()
#define VI vector<int>
#define VII vector<pair<int,int> >
#define VL vector<long long int >
#define VLL vector<pair<long long ,long long > >
#define PLL pair<long long ,long long >
#define itr iterator
#define fill(x,v) fill(all(x),v)
// my fastest car series
//#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define ms(x) memset(x,0,sizeof(x))
// data pies
#define ll long long int
#define li long int
#define ff float
#define db double
// loopi loops
#define rep(i,a,b) for(i=a;i<b;i++)
#define repr(i,a,b) for(i=a;i>=b;i--)
// debugger
#define print_v(x) for(int i=0;i<x.size();i++) cout << x[i] << " "
#define debug(x) cout << "#debug" << " " << x << endl
#define MOD 1000000007
int random_long(int digit=8)
{
int len=digit;
int x=0;
for ( int i = 0; i < len; ++i )
{
int dig = rand() % 10;
while ( x == 0 && dig == 0 )
dig = rand() % 10;
x = x * 10 + dig;
}
return x;
}
int random(int l, int r)
{
int diff = r - l + 1;
return l+random_long()%diff;
}
inline void scan(int &a)
{
register int c;
a = 0;
do c = getchar_unlocked(); while(c < '0');
do{
a = (a << 1) + (a << 3) + c - '0';
c = getchar_unlocked();
}while(c >= '0');
}
int main(){
int t;
t=1;
while (t--) {
int n , i , val;
scan(n);
VII arr(n+1,mp(0,0));
rep(i,1,n+1){
scan( arr[i].ft ) ;
arr[i].sd = i ;
}
sort( all(arr) );
set<int> s ;
ll ans = 0 ;
i = n ;
while (i>=1) {
int x = arr[i].ft ;
int savei = i ;
VI tt;
while (i>=1 && x == arr[i].ft){
tt.pb( arr[i].sd );
i--;
}
reverse( all(tt) );
i = savei;
while ( i>=1 && x == arr[i].ft ) {
val = arr[i].sd;
if( s.empty()){
val = 1;
}else{
set <int> :: itr it = lower_bound(all(s),arr[i].sd);
if ( it!=s.begin() ){
--it;
val = (*it)+1;
}else {
val = 1;
}
}
if( val != arr[i].sd ){
ans += (lower_bound(all(tt), arr[i].sd) - lower_bound(all(tt), val) ) ;
}
i--;
}
i = savei;
while ( i>=1 && x==arr[i].ft ){
s.insert( arr[i].sd );
i--;
}
}
printll(2*ans);
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIG15IGNob2NvIHBpZQoKI2RlZmluZSBmdCBmaXJzdAojZGVmaW5lIHNkIHNlY29uZAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIFBJSSBwYWlyPGludCxpbnQ+CiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSx4LmVuZCgpCiNkZWZpbmUgVkkgdmVjdG9yPGludD4KI2RlZmluZSBWSUkgdmVjdG9yPHBhaXI8aW50LGludD4gPgojZGVmaW5lIFZMIHZlY3Rvcjxsb25nIGxvbmcgaW50ID4KI2RlZmluZSBWTEwgdmVjdG9yPHBhaXI8bG9uZyBsb25nICxsb25nIGxvbmcgPiA+CiNkZWZpbmUgUExMIHBhaXI8bG9uZyBsb25nICxsb25nIGxvbmcgPgojZGVmaW5lIGl0ciBpdGVyYXRvcgojZGVmaW5lIGZpbGwoeCx2KSBmaWxsKGFsbCh4KSx2KQoKLy8gbXkgZmFzdGVzdCBjYXIgc2VyaWVzCgovLyNkZWZpbmUgc2Nhbih4KSBzY2FuZigiJWQiLCZ4KQojZGVmaW5lIHByaW50KHgpIHByaW50ZigiJWRcbiIseCkKI2RlZmluZSBzY2FubGwoeCkgc2NhbmYoIiVsbGQiLCZ4KQojZGVmaW5lIHByaW50bGwoeCkgcHJpbnRmKCIlbGxkXG4iLHgpCiNkZWZpbmUgbXMoeCkgbWVtc2V0KHgsMCxzaXplb2YoeCkpCgoKLy8gZGF0YSBwaWVzCgojZGVmaW5lIGxsIGxvbmcgbG9uZyBpbnQKI2RlZmluZSBsaSBsb25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