var
n,i,j,k,ans:longint;
a,aa,b,bb,e:array[1..30000]of longint;
begin
assign(input,'three.in');reset(input);
assign(output,'three.out');rewrite(output);
read(n);
for i:=1 to n do read(e[i]);
a[1]:=0;
for i:=2 to n do
begin
j:=i-1;
while (e[j]>e[i])and(j>0) do dec(j);
if j=0 then a[i]:=0
else
begin
if e[j]=e[i]then begin a[i]:=a[j];aa[i]:=aa[j]+1;end;
if e[i]>e[j]then a[i]:=a[j]+aa[j]+1;
end;
//write(a[i]);
end;
//----------------------------------------
b[n]:=0;
for i:=n-1 downto 1 do
begin
j:=i+1;
while (e[j]<e[i])and(j<=n) do inc(j);
if j=0 then b[i]:=0
else
begin
if e[j]=e[i]then begin b[i]:=b[j];bb[i]:=bb[j]+1;end;
if e[i]<e[j]then b[i]:=b[j]+bb[j]+1;
end;
//write(b[i]);
end;
//_______________________________________
for i:=2 to n-1 do ans:=ans+a[i]*b[i];
write(ans);
close(input);close(output);
end.