#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;

int main(){
    int n,x,i;
    cin>>n;

    vector<int>v;

    for(i=0;i<n;i++){
        cin>>x;
        v.push_back(x);
    }

    if(n<=2)
     cout<<0;
    else{

    sort(v.begin(),v.end());

    x=v[n-1]-v[0];
    i=v[n-2]-v[0];
    int x1=v[n-1]-v[1];

    if(i<x && i<x1)
     cout<<i;
    else if(x1<i && x1<x)
     cout<<x1;
    else
     cout<<x;
}
    return 0;
}


#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;

int min(int a,int b){
    return (a>b?b:a);
}

int main(){
    int n,x,i;
    cin>>n;

    vector<int>v;

    for(i=0;i<n;i++){
        cin>>x;
        v.push_back(x);
    }

    if(n<=2)
     cout<<0;
    else{

    sort(v.begin(),v.end());

    x=v[n-1]-v[0];
    i=v[n-2]-v[0];
    int x1=v[n-1]-v[1];

    x=min(x,min(i,x1));
    cout<<x;
}
    return 0;
}


#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int main() {
	int a[100020], i, k, n;
	cin >> n;
	for (i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	sort(a, a + n);
	if (n == 2)
		printf("0\n");
	else if (n == 3) {
		k = min(a[2] - a[1], a[2] - a[0]);
		k = min(k, a[1] - a[0]);
		printf("%d\n", k);
	}
	else {
		k = min(a[n - 1] - a[0], a[n - 1] - a[1]);
		k = min(k, a[n - 2] - a[0]);
		printf("%d\n", k);
	}
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
     int n;
     cin >> n;
     vector<int> v(n);
     for (int i = 0; i < n; i++)
     {
         cin >> v[i];
     }
     sort(v.begin(), v.end());
     if (v[1] - v[0] > v[n-1] - v[n-2])
         v.erase(v.begin());
     else
         v.pop_back();
     cout << v[n - 2] - v[0];
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int x[100005];
int main()
{
	int n;
	cin>>n;
	for(int a=0;a<n;a++)
	cin>>x[a];
	sort(x,x+n);
	int c,d;
	c=x[n-1]-x[1];
	d=x[n-2]-x[0];
	if(c>d)
	c=d;
	printf("%d",c);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=100010;
int a[maxn];
int min(int a,int b){return a>b?b:a;}
int main()
{
	//freopen("in.txt","r",stdin);
	int n,i;
	while(cin>>n)
	{
		for( i = 0 ; i < n ; i++)
		{
			cin>>a[i];
		}
		sort(a,a+n);
		cout<<min(a[n-1]-a[1],a[n-2]-a[0])<<endl;
	}
	return 0;
}


#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
    int test;
    int arr[100001];
    cin>>test;
    for (int i=1; i<=test; i++)
    {
        cin>>arr[i];
    }

    sort(arr + 1, arr + test + 1);

    cout<<min(arr[test-1] - arr[1], arr[test] - arr[2])<<endl;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
	int n,temp;
	vector<int> num;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>temp;
		num.push_back(temp);
	}
	sort(num.begin(),num.end());
	if(num.at(num.size()-2)-num.front()>num.at(num.size()-1)-num.at(1))
		cout<<num.at(num.size()-1)-num.at(1);
	else
		cout<<num.at(num.size()-2)-num.front();
	return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main() {
	int n, k=0,min;
	cin>>n ;
	vector <int> mas(n);
	vector <int> asd(n);
	if (n != 2) {
		for (int i = 0; i < n; i++) {
			cin >> mas[i];
		}
		sort(mas.begin(), mas.end());
		min = mas[n - 1] - mas[0];
		if ((mas[n - 1] - mas[1]) < min) min = mas[n - 1] - mas[1];
		if ((mas[n -2] - mas[0]) < min) min = mas[n - 2] - mas[0];
		cout << min;
	}
	else cout<< 0;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#include<cctype>
using namespace std;

typedef long long ll;
const int Maxn=410000;

int n,x;

int main() {
    scanf("%d",&n);
    int zd=0,cd=0,zx=0x7fffffff,cx=zx;
    while(n--) {
        scanf("%d",&x);
        if(x>zd) {
            cd=zd;zd=x;
        }
        else cd=max(cd,x);
        if(x<zx) {
            cx=zx;zx=x;
        }
        else cx=min(cx,x);
    }
    printf("%d\n",min(cd-zx,zd-cx));
    return 0;
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int arr[100010];

int main()
{
	int n;
	int i;

	scanf("%d",&n );

	for( i = 0 ; i < n ; i++ )
		scanf("%d", &arr[i] );

	sort( arr , arr + n );

	printf("%d", min( arr[ n - 2 ] - arr[0] , arr[ n - 1 ] - arr[1] ) );
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int arr[100010];

int main()
{
	int n;
	int i;

	scanf("%d",&n );

	for( i = 0 ; i < n ; i++ )
		scanf("%d", &arr[i] );

	sort( arr , arr + n );

	int a = arr[n-1] - arr[1];
	int b = arr[n-2] - arr[0];

	printf("%d", min( a , b ) );

}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
int arr[10001];
int main()
{
	ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> arr[i];
	}
	sort(arr, arr + n);

	int res1 = arr[n - 2] - arr[0];
	int res2 = arr[n - 1] - arr[1];

	cout << min(res1, res2);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
int arr[100001];
int main()
{
	ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	sort(arr, arr + n);

	int res1 = arr[n - 2] - arr[0];
	int res2 = arr[n - 1] - arr[1];

	cout << min(res1, res2);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <string>

using namespace std;
int arr[100001];
int main()
{
	ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> arr[i];
	}
	sort(arr, arr + n);

	int res1 = arr[n - 2] - arr[0];
	int res2 = arr[n - 1] - arr[1];

	cout << min(res1, res2);

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int a[10001];
	int min1 = 10000000;
	int max1 = -10000000;
	int min2 = 10000000;
	int max2 = -10000000;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	sort(a, a + n);
	int zhao = a[n - 1] - a[1];
	int zi = a[n - 2] - a[0];
	if (zhao < zi)
		cout << zhao;
	else
		cout << zi;
	return 0;
}


#include<iostream>
using namespace std;
long int n;
int a[100];
void quicksort(int x,int y)
{
	if (x < y)
	{
		int left = x;
		int right = y;
		int temp = a[x];
		while (left < right)
		{
			while (left < right&&a[right] >= temp)
				right--;
			if (left < right)
				a[left++] = a[right];
			while (left < right&&a[left] <= temp)
				left++;
			if (left < right)
				a[right--] = a[left];
		}
		a[left] = temp;
		quicksort(x, left - 1);
		quicksort(left + 1, y);
	}
	else
		return;
}
int removebig()
{
	return a[n - 1] - a[1];
}
int removesmall()
{
	return a[n] - a[2];
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	quicksort(1, n);
	int big = removebig();
	int small = removesmall();
	if (big > small)
		cout << small;
	else
		cout << big;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int a[10001];
	int min1 = 10000000;
	int max1 = -10000000;
	int min2 = 10000000;
	int max2 = -10000000;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (a[i] > max2)
		{
			max1 = max2;
			max2 = a[i];
		}
		if (a[i]<max2&&a[i]>max1)
		{
			max1 = a[i];
		}
		if (a[i] <min2)
		{
			min1 = min2;
			min2 = a[i];
		}
		if (a[i]>min2&&a[i]<min1)
		{
			min1 = a[i];
		}
	}
	int zhao = max1 - min2;
	int zi = max2 - min1;
	if (zhao < zi)
		cout << zhao;
	else
		cout << zi;
	return 0;
}


#include<iostream>
using namespace std;
long int n;
int a[10001];
void quicksort(int x,int y)
{
	if (x < y)
	{
		int left = x;
		int right = y;
		int temp = a[x];
		while (left < right)
		{
			while (left < right&&a[right] >= temp)
				right--;
			if (left < right)
				a[left++] = a[right];
			while (left < right&&a[left] <= temp)
				left++;
			if (left < right)
				a[right--] = a[left];
		}
		a[left] = temp;
		quicksort(x, left - 1);
		quicksort(left + 1, y);
	}
	else
		return;
}
int removebig()
{
	return a[n - 1] - a[1];
}
int removesmall()
{
	return a[n] - a[2];
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	quicksort(1, n);
	int big = removebig();
	int small = removesmall();
	if (big > small)
		cout << small;
	else
		cout << big;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int a[100001];
	for (int i = 0; i < n; i++)
		cin >> a[i];
	sort(a, a + n);
	int zhao = a[n - 1] - a[1];
	int zi = a[n - 2] - a[0];
	if (zhao < zi)
		cout << zhao;
	else
		cout << zi;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1000000
int str[maxn];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>str[i];
	sort(str,str+n);
	//int t=str[n-1];
	int sum=str[n-2]-str[0];
	cout<<sum<<endl;
	return 0;
 }


#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 100005
int str[maxn];
int main()
{
 int n;
 cin>>n;
 for(int i=0;i<n;i++)
 {
 	cin>>str[i];
 }
     if(n==2)
      cout<<'0';
     else
    {
 	sort(str,str+n);
 	int m=str[n-2]-str[0];
 	int t=str[n-1]-str[1];
 	if(m>t)
 	cout<<t;
 	else
 	cout<<m;
    }
    return 0;
 }
 /*	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>str[i];
	sort(str,str+n);
	//int t=str[n-1];
	int sum=str[n-2]-str[0];
	cout<<sum<<endl;
	return 0;*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int a[100010];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));

		for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);

		sort(a+1,a+n+1);

		printf("%d\n",min(a[n]-a[2],a[n-1]-a[1]));
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[100001];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	int x;
	x=a[n-2]-a[0];
	cout<<x<<endl;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[100001];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	int x;
	x=a[n-2]-a[0];
	int y;
	y=a[n-1]-a[1];
	if(x<=y)
	cout<<x<<endl;
	else
	cout<<y<<endl;
}


#include<iostream>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<sstream>
#include<algorithm>
#include<set>
#define pb push_back
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[100000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int mini1=a[0],maxi1=a[0];
	for(int i=0;i<n;i++){
		if(maxi1<a[i]){
			maxi1=a[i];
		}
		if(mini1>a[i]){
			mini1=a[i];
		}
	}
	int mini2,maxi2;
	int u=0;
	int h=0;
	for(int i=0;i<n;i++){
		if(mini1==a[i]){
			h++;
		}
		if(u==0&&mini1!=a[i]){
			mini2=a[i];
			u++;
		}
		else{
			if(mini2>a[i]&&mini1!=a[i]&&h==0){
				mini2=a[i];
				h++;
			}
		}
	}
	if(u==0){
		mini2=mini1;
	}
    u=0;
    h=0;
	for(int i=0;i<n;i++){
		if(maxi1==a[i]){
			h++;
		}
		if(u==0&&maxi1!=a[i]){
			maxi2=a[i];
			u++;
		}
		else{
			if(maxi2<a[i]&&maxi1!=a[i]&&h==0){
				maxi2=a[i];
				h++;
			}
		}

	}
	if(u==0){
		maxi2=maxi1;
	}
	if(maxi1-mini2<maxi2-mini1){
		cout<<maxi1-mini2;
	}
	else{
		cout<<maxi2-mini1;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<sstream>
#include<algorithm>
#include<set>
#define pb push_back
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[100000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int mini1=a[0],maxi1=a[0];
	for(int i=0;i<n;i++){
		if(maxi1<a[i]){
			maxi1=a[i];
		}
		if(mini1>a[i]){
			mini1=a[i];
		}
	}
	int mini2,maxi2;
	int u=0;
	int h=0;
	for(int i=0;i<n;i++){
		if(u==0&&mini1!=a[i]){
			mini2=a[i];
			u++;
		}
		else{
			if(mini2>a[i]&&mini1!=a[i]&&h==0){
				mini2=a[i];
				h++;
			}
		}
	}
	if(u==0){
		mini2=mini1;
	}
    u=0;
    h=0;
	for(int i=0;i<n;i++){
		if(u==0&&maxi1!=a[i]){
			maxi2=a[i];
			u++;
		}
		else{
			if(maxi2<a[i]&&maxi1!=a[i]h==0){
				maxi2=a[i];
				h++;
			}
		}

	}
	if(u==0){
		maxi2=maxi1;
	}
	if(maxi1-mini2<maxi2-mini1){
		cout<<maxi1-mini2;
	}
	else{
		cout<<maxi2-mini1;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<sstream>
#include<algorithm>
#include<set>
#define pb push_back
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[100000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int mini1=a[0],maxi1=a[0];
	for(int i=0;i<n;i++){
		if(maxi1<a[i]){
			maxi1=a[i];
		}
		if(mini1>a[i]){
			mini1=a[i];
		}
	}
	int mini2,maxi2;
	int u=0;
	int h=0;
	for(int i=0;i<n;i++){
		if(mini1==a[i]){
			h++;
		}
		if(u==0&&mini1!=a[i]){
			mini2=a[i];
			u++;
		}
		else{
			if(mini2>a[i]&&mini1!=a[i]&&h==0){
				mini2=a[i];
				h++;
			}
			else{
				if(maxi2<a[i]){
					maxi2=a[i];
				}
			}
		}
	}
	if(u==0){
		mini2=mini1;
	}
    u=0;
    h=0;
	for(int i=0;i<n;i++){
		if(maxi1==a[i]){
			h++;
		}
		if(u==0&&maxi1!=a[i]){
			maxi2=a[i];
			u++;
		}
		else{
			if(maxi2<a[i]&&maxi1!=a[i]&&h==0){
				maxi2=a[i];
				h++;
			}
			else{
				if(maxi2<a[i]){
					maxi2=a[i];
				}
			}
		}

	}
	if(u==0){
		maxi2=maxi1;
	}
	if(maxi1-mini2<maxi2-mini1){
		cout<<maxi1-mini2;
	}
	else{
		cout<<maxi2-mini1;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<sstream>
#include<algorithm>
#include<set>
#define pb push_back
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[100000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int mini1=a[0],maxi1=a[0];
	for(int i=0;i<n;i++){
		if(maxi1<a[i]){
			maxi1=a[i];
		}
		if(mini1>a[i]){
			mini1=a[i];
		}
	}
	int mini2,maxi2;
	int u=0;
	int h=0;
	for(int i=0;i<n;i++){
		if(h==0){
			if(u==0&&mini1!=a[i]){
				mini2=a[i];
				u++;
			}
			else{
				if(mini1!=a[i]&&mini2>a[i]){
					mini2=a[i];
				}
			}
		}
		else{
			if(u==0){
				mini2=a[i];
				u++;
			}
			else{
				if(mini2>a[i]){
					mini2=a[i];
				}
			}
		}
		if(mini1==a[i]){
			h++;
		}
	}
	if(u==0){
		mini2=mini1;
	}
    u=0;
    h=0;
	for(int i=0;i<n;i++){
		if(h==0){
			if(u==0&&maxi1!=a[i]){
				maxi2=a[i];
				u++;
			}
			else{
				if(maxi1!=a[i]&&maxi2<a[i]){
					maxi2=a[i];
				}
			}
		}
		else{
			if(u==0){
				maxi2=a[i];
				u++;
			}
			else{
				if(maxi2<a[i]){
					maxi2=a[i];
				}
			}
		}
		if(maxi1==a[i]){
			h++;
		}
	}
	if(u==0){
		maxi2=maxi1;
	}
	if(maxi1-mini2<maxi2-mini1){
		cout<<maxi1-mini2;
	}
	else{
		cout<<maxi2-mini1;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<sstream>
#include<algorithm>
#include<set>
#define pb push_back
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[100000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int mini1=a[0],maxi1=a[0];
	for(int i=0;i<n;i++){
		if(maxi1<a[i]){
			maxi1=a[i];
		}
		if(mini1>a[i]){
			mini1=a[i];
		}
	}
	int mini2,maxi2;
	int u=0;
	for(int i=0;i<n;i++){
		if(u==0&&mini1!=a[i]){
			mini2=a[i];
			u++;
		}
		else{
			if(mini2>a[i]&&mini1!=a[i]){
				mini2=a[i];
			}
		}

	}
	if(u==0){
		mini2=mini1;
	}
    u=0;
	for(int i=0;i<n;i++){
		if(u==0&&maxi1!=a[i]){
			maxi2=a[i];
			u++;
		}
		else{
			if(maxi2<a[i]&&maxi1!=a[i]){
				maxi2=a[i];
			}
		}

	}
	if(u==0){
		maxi2=maxi1;
	}
	if(maxi1-mini2<maxi2-mini1){
		cout<<maxi1-mini2;
	}
	else{
		cout<<maxi2-mini1;
	}

	return 0;
}


#include<iostream>
#include<string>
#include<cmath>
#include<cctype>
#include<vector>
#include<sstream>
#include<algorithm>
#include<set>
#define pb push_back
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[100000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int mini1=a[0],maxi1=a[0];
	for(int i=0;i<n;i++){
		if(maxi1<a[i]){
			maxi1=a[i];
		}
		if(mini1>a[i]){
			mini1=a[i];
		}
	}
	int mini2,maxi2;
	int u=0;
	int h=0;
	for(int i=0;i<n;i++){
		if(u==0&&mini1!=a[i]){
			mini2=a[i];
			u++;
		}
		else{
			if(mini2>a[i]&&mini1!=a[i]&&h==0){
				mini2=a[i];
				h++;
			}
		}
	}
	if(u==0){
		mini2=mini1;
	}
    u=0;
    h=0;
	for(int i=0;i<n;i++){
		if(u==0&&maxi1!=a[i]){
			maxi2=a[i];
			u++;
		}
		else{
			if(maxi2<a[i]&&maxi1!=a[i]&&h==0){
				maxi2=a[i];
				h++;
			}
		}

	}
	if(u==0){
		maxi2=maxi1;
	}
	if(maxi1-mini2<maxi2-mini1){
		cout<<maxi1-mini2;
	}
	else{
		cout<<maxi2-mini1;
	}

	return 0;
}


#pragma hdrstop
#include <cstdlib>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
int a,b,y,x[100000],i;
cin>> y;
for(i=0;i<y;i++)
{
	cin >> x[i];
}
sort(x,x+y);
a=x[y-2]-x[0];
b=x[y-1]-x[1];
if(a>b)
{
	cout<<b<<endl;
}
else
{
	cout<<a<<endl;
}
cin >>y;
	return 0;
}


#pragma hdrstop
#include <cstdlib>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
int a,b,y,x[10000],i;
cin>> y;
for(i=0;i<y;i++)
{
	cin >> x[i];
}
sort(x,x+y);
a=x[y-2]-x[0];
b=x[y-1]-x[1];
if(a>b)
{
	cout<<b<<endl;
}
else
{
	cout<<a<<endl;
}
cin >>y;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)
        cin >> vec[i];
    sort(vec.begin(), vec.end());
    cout << min(vec[s.size()-2] - vec[0], vec[s.size()-1] - vec[1]) << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; i++)
        cin >> vec[i];
    sort(vec.begin(), vec.end());
    int val1 = vec[vec.size()-1] -vec[1];
    int val2 = vec[vec.size()-2] - vec[0];
    cout << min(val1, val2) << endl;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int n;
int a[100000];

int main(){

	cin >> n;
	int M = -1;
	int m = 100001;
	int max_ind = -1;
	int min_ind = -1;
	for (int i = 0; i < n; ++i){
		cin >> a[i];
		if (a[i] >= M){
			M = a[i];
			max_ind = i;
		}
		if (a[i] < m){
			m = a[i];
			min_ind = i;
		}
	}

	int k = 100001;
	int M2 = -1, m2 = 100001;
	for (int i = 0; i < n; ++i){
		if (a[i] >= M2 && i != max_ind){
			M2 = a[i];
		}
		if (a[i] < m2 && i != min_ind)
			m2 = a[i];
	}

	cout << min(M2 - m, M - m2) << endl;

	cout << endl;

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100001],i,j,n,sub;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    if(n>=2)
    {
    sub=a[n-2]-a[0];
    }
    else
    {
        sub=0;
    }
    cout<<sub<<endl;
    return 0;

}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100010],i,n,sub,sun;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    if(n>=2)
    {
    sub=a[n-2]-a[0];
    sun=a[n-1]-a[1];
    if(sub<sun)
    {
        cout<<sub<<endl;
    }
    else
    {
        cout<<sun<<endl;
    }
    }
    else
    {
     cout<<'0'<<endl;
     }
    return 0;

}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100001],i,j,n;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    int sub=a[n-2]-a[0];
    cout<<sub<<endl;
    return 0;

}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[100001],i,n,sub;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    if(n>=2)
    {
    sub=a[n-2]-a[0];
    }
    else
    {
        sub=0;
    }
    cout<<sub<<endl;
    return 0;

}


#define _CRT_SECURE_NO_WARNINGS
#include <map>
#include <random>
#include <set>
#include <string>
#include<string.h>
#include <vector>
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stdlib.h>

using namespace std;

#define rt return
#define ll long long
#define ld long double
#define ct continue
#define endl "\n"
#define dl delete

const ll MAX_SIZE = 2 * 1e5;

void files() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
}
int a[MAX_SIZE];
int cnt(0), n, k(1);
int main()
{
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	cout << min(a[n - 2] - a[0], a[n - 1] - a[1]);
	rt 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <map>
#include <random>
#include <set>
#include <string>
#include<string.h>
#include <vector>
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stdlib.h>

using namespace std;

#define rt return
#define ll long long
#define ld long double
#define ct continue
#define endl "\n"
#define dl delete

const ll MAX_SIZE = 2 * 1e5;

void files() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
}
int a[MAX_SIZE];
int d[MAX_SIZE];
int n, k, sum(-1);
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a, a + n);
	int min_ = 9999999999;
	int max_ = -1;
	for (int i = 0; i < n - 1; i++) {
		max_ = max(max_, a[i]);
		min_ = min(min_, a[i]);
	}
	cout << max_ - min_ << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include <map>
#include <random>
#include <set>
#include <string>
#include<string.h>
#include <vector>
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stdlib.h>

using namespace std;

#define rt return
#define ll long long
#define ld long double
#define ct continue
#define endl "\n"
#define dl delete

const ll MAX_SIZE = 2 * 1e5;

void files() {
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
}
int a[MAX_SIZE];
int cnt(0), n, k(1);
int main()
{
	cin >> n;
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	sort(a, a + n);
	cout << min(a[n - 2] - a[0], a[n - 1] - a[1]);
	rt 0;

}


#include <iostream>
using namespace std;

const int MAXINT = 2147483647;
int a[200000];

int main () {
	int n;
	cin >> n;
	int max1 = 0, min1 = MAXINT, maxidx, minidx;
	for (int i = 1;i <= n;i++) {
		cin >> a[i];
		if (a[i] > max1) {
			max1 = a[i];
			maxidx = i;
		}
		if (a[i] < min1) {
			min1 = a[i];
			minidx = i;
		}
	}
	int max2 = 0, min2 = MAXINT;
	for (int i = 1;i <= n;i++) {
		if ((a[i] > max2) && (i != maxidx)) max2 = a[i];
		if ((a[i] < min2) && (i != minidx)) min2 = a[i];
	}
	cout << min(max1 - min2, max2 - min1);
}


#include<stdio.h>
#include<string.h>
int main()
{int n,max[2]={0},min[2],a;
min[0]=min[1]=1e5;
scanf("%d",&n);
for(int i=0;i<n;i++)
{scanf("%d",&a);
if(a>max[0])
max[1]=max[0],max[0]=a;
else
	if(a>max[1])
		max[1]=a;
if(a<min[0])
	min[1]=min[0],min[0]=a;
else
	if(a<min[1])
		min[1]=a;}
if(max[0]-min[1]<max[1]-min[0])
printf("%d",max[0]-min[1]);
else
	printf("%d",max[1]-min[0]);}


#include <iostream>
#include <vector>
#include <cstring>
#include <string.h>
#include <iterator>
#include<math.h>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <limits.h>
#include <string>
#include <bitset>
#include <map>

#define endl cout<<"\n"
#define lli long long int

using namespace std;

//fast-exponentiation-lli
lli expo_fast(lli a, lli b) {
	a = a;
	lli result = 1;
	while (b) {
		//multiplyint(tos(result), a);
		if (b & 1)result = (result * a);
		b >>= 1;
		a = (a*a);
	}
	return (result);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n; cin >> n;
	vector<int> arr(n);
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	if (n == 2)cout << 0;
	else {
		sort(arr.begin(), arr.end());
		cout << min(arr[n - 1] - arr[1], arr[n - 2] - arr[0]);
	}

	//system("PAUSE");
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n,i;
int a[100001];
int main(){
while(cin>>n){
if(n==2){
cout<<"0"<<endl;
continue;
}
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int t=min(a[n-1]-a[1],a[n-2]-a[0]);
cout<<t<<endl;
}
return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n,i;
int a[100001];
int main(){
while(cin>>n){
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int t=min(a[n-1]-a[1],a[n-2]-a[0]);
cout<<t<<endl;
}
return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n,i;
int a[100];
int main(){
while(cin>>n){
if(n==2){
cout<<"0"<<endl;
continue;
}
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int t=min(a[n-1]-a[1],a[n-2]-a[0]);
cout<<t<<endl;
}
return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n,max1,max2,min1,min2,x;
    cin>>n;

    cin>>max1;

    cin>>x;
    if(x>max1)
    {
        max2=max1;
        max1=x;
        min1=max2;
        min2=max1;
    }
    else
    {
        max2=x;
        min1=x;
        min2=max1;
    }
    if(n==2)
    {
        cout<<0;
        return 0;
    }
    for(int i=2;i<n;i++)
    {
        cin>>x;
        if(x>=max1)
        {
            max2=max1;
            max1=x;
        }
        if(x<=min1)
        {
            min2=min1;
            min1=x;
        }
    }
    if(max1-max2>min2-min1)
    {
        cout<<max2-min1;
    }
    else
    {
        cout<<max1-min2;
    }

    return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n,max1,max2,min1,min2,x;
    cin>>n;

    cin>>max1;

    cin>>x;
    if(x>max1)
    {
        max2=max1;
        max1=x;
        min1=max2;
        min2=max1;
    }
    else
    {
        max2=x;
        min1=x;
        min2=max1;
    }
    if(n==2)
    {
        cout<<0;
        return 0;
    }
    for(int i=2;i<n;i++)
    {
        cin>>x;
        if(x>=max1)
        {
            max2=max1;
            max1=x;
        }
        else if(x>max2)
        {
            max2=x;
        }
        if(x<=min1)
        {
            min2=min1;
            min1=x;
        }
        else if(x<min2)
        {
            min2=x;
        }
    }
    if(max1-max2>min2-min1)
    {
        cout<<max2-min1;
    }
    else
    {
        cout<<max1-min2;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

typedef long long LL;

LL n;
LL input[100005];

int main(void)
{
  cin >> n;
  for(LL i0 = 0; i0 < n; i0++)
  {
    cin >> input[i0];
  }
  sort(input, input + n);
  if(n == 2)cout << 0 << endl;
  else
  {
    LL a = abs(input[0] - input[n-2]);
    LL b = abs(input[1] - input[n-1]);
    cout << min(a, b) << endl;
  }
  return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#define MAXN 100100
using namespace std;
int argv[MAXN];
int main(void)
{
	int n;

	while (scanf_s("%d", &n) != EOF)
	{
		for (int i = 0; i < n; i++)scanf_s("%d", &argv[i]);
		if (n == 2) { printf("0\n"); continue; }
		sort(argv, argv + n);
		printf("%d\n", min(argv[n - 1] - argv[n - 2], argv[1] - argv[0]));
	}

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#define MAXN 100100
using namespace std;
int argv[MAXN];
int main(void)
{
	int n;

	while (scanf_s("%d", &n) != EOF)
	{
		for (int i = 0; i < n; i++)scanf_s("%d", &argv[i]);
		if (n == 2) { printf("0\n"); continue; }
		sort(argv, argv + n);
		printf("%d\n", min(argv[n - 1] - argv[1], argv[n - 2] - argv[0]));
	}

	return 0;
}


#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
    int n=0;
    int a[100010];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        if(n<=2)
            cout<<0;
        else
        {
            if((a[1]-a[0])>(a[n-1]-a[n-2]))
               cout<<(a[n-1]-a[1]);
            else
                cout<<(a[n-2]-a[0]);
        }
        cout<<endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#define maxn 100005
using namespace std;

int main()
{
    int n;
    int a[maxn];
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    sort(a+1,a+1+n);
    if(a[n]-a[2]>a[n-1]-a[1])
        cout<<a[n-1]-a[1]<<endl;
    else
        cout<<a[n]-a[2]<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    vector<int> buf(n, 0);
    for (int i = 0; i < n; i++) {
        cin >> buf[i];
    }
    sort(buf.begin(), buf.end());
    n = min(buf.back() - buf[1], buf[n - 2] - buf[0]);
    cout << n;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int n = 0;
    cin >> n;
    vector<int> buf(n, 0);
    for (int i = 0; i < n; i++) {
        cin >> buf[i];
    }
    sort(buf.begin(), buf.end());
    cout << buf[n - 2] - buf[0];
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;

	int* a = new int[n];
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	int max, min, smax, smin;

	int j = 0;
	for (int i = 1; i < n; ++i)
	{
		if (a[0] != a[i])
		{
			j = i;
			break;
		}
	}

	if (a[0] > a[j])
	{
		max = smin = a[0];
		min = smax = a[j];
	}
	else
	{
		max = smin = a[j];
		min = smax = a[0];
	}

	for (int i = 1; i < n; ++i)
	{
		if (a[i] < smin)
		{
			if (a[i] <= min)
			{
				smin = min;
				min = a[i];
			}
			else
				smin = a[i];
		}
		else if (a[i] > smax)
		{
			if (a[i] >= max)
			{
				smax = max;
				max = a[i];
			}
			else
				smax = a[i];
		}
	}

	int res = ((max - smin) < (smax - min) ? (max - smin) : (smax - min));

	cout << res;

	//system("pause");
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;

	int* a = new int[n];
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	int max, min, smax, smin;

	int j = 0;
	for (int i = 1; i < n; ++i)
	{
		if (a[0] != a[i])
		{
			j = i;
			break;
		}
	}

	if (a[0] > a[j])
	{
		max = smin = a[0];
		min = smax = a[j];
	}
	else
	{
		max = smin = a[j];
		min = smax = a[0];
	}

	for (int i = 2; i < n; ++i)
	{
		if (a[i] < min)
		{
			smin = min;
			min = a[i];
		}
		else if (a[i] > max)
		{
			smax = max;
			max = a[i];
		}
	}
	cout << ((max - smin) < (smax - min) ? (max - smin) : (smax - min));

	//system("pause");
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;

	int* a = new int[n];
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	int max, min, smax, smin;

	int j = 0;
	for (int i = 1; i < n; ++i)
	{
		if (a[0] != a[i])
		{
			j = i;
			break;
		}
	}

	if (a[0] > a[j])
	{
		max = smin = a[0];
		min = smax = a[j];
	}
	else
	{
		max = smin = a[j];
		min = smax = a[0];
	}

	for (int i = 1; i < n; ++i)
	{
		if (a[i] < smin)
		{
			if (a[i] < min)
			{
				smin = min;
				min = a[i];
			}
			else
				smin = a[i];
		}
		else if (a[i] > smax)
		{
			if (a[i] > max)
			{
				smax = max;
				max = a[i];
			}
			else
				smax = a[i];
		}
	}

	int res = ((max - smin) < (smax - min) ? (max - smin) : (smax - min));

	cout << res;

	//system("pause");
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;

	int* a = new int[n];
	for (int i = 0; i < n; ++i)
		cin >> a[i];

	int max = a[0], min = a[0], smax = a[0], smin = a[0];
	for (int i = 1; i < n; ++i)
	{
		if (a[i] < min)
		{
			smin = min;
			min = a[i];
		}
		else if (a[i] > max)
		{
			smax = max;
			max = a[i];
		}
	}
	cout << ((max - smin) < (smax - min) ? (max - smin) : (smax - min));

	//system("pause");
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	cin >> n;

	vector<int> a;
	int temp;
	for (int i = 0; i < n; ++i)
	{
		cin >> temp;
		a.push_back(temp);
	}

	sort(a.begin(), a.end());

	cout << ((a[n - 2] - a[0]) < (a[n - 1] - a[1]) ? (a[n - 2] - a[0]) : (a[n - 1] - a[1]));

	//system("pause");
	return 0;
}


n = int(input())
s = list(map(int, input().split()))

M = max(s)
m = min(s)

if (M == m):
	print(0)
else:
	m1 = 100000
	M1 = 0
	cm = 0
	cM = 0
	for i in range(n):
		if (s[i] < m1 and s[i] > m):
			m1 = s[i]

		if (s[i] > M1 and s[i] < M):
			M1 = s[i]

		if (s[i] == m):
			cm += 1

		if (s[i] == M):
			cM += 1

	if (cm > 1) and (cM > 1) and (m1 == M) and (M1 == m):
		print(M - m)
	elif (M - m1) < (M1 - m):
		print(M - m1)
	else:
		print(M1 - m)


n = int(input())
s = list(map(int, input().split()))

M = max(s)
m = min(s)

if (M == m):
	print(0)
else:
	m1 = M
	M1 = m
	cm = 0
	cM = 0
	for i in range(n):
		if (s[i] < m1 and s[i] > m):
			m1 = s[i]

		if (s[i] > M1 and s[i] < M):
			M1 = s[i]

		if (s[i] == m):
			cm += 1

		if (s[i] == M):
			cM += 1

	if (cm > 1) and (cM > 1):
		print(M - m)
	elif (cm == 1) and (cM > 1) and (M - m1) < (M - m):
		print(M - m1)
	elif (cM == 1) and (cm > 1) and (M1 - m) < (M - m):
		print(M1 - m)
	elif (cm == 1) and (cM == 1):
		if (M1 - m) < (M - m1):
			print(M1 - m)
		else:
			print(M - m1)


n = int(input())
s = list(map(int, input().split()))

M = max(s)
m = min(s)

if (M == m):
	print(0)
else:
	m1 = 100000
	M1 = 0
	for i in range(n):
		if (s[i] < m1 and s[i] > m):
			m1 = s[i]

		if (s[i] > M1 and s[i] < M):
			M1 = s[i]

	if (m1 == M) and (M1 == m):
		print(M - m)
	elif (M - m1) < (M1 - m):
		print(M - m1)
	else:
		print(M1 - m)


n = int(input())
s = list(map(int, input().split()))

M = max(s)
m = min(s)

if (M == m):
	print(0)
else:
	m1 = 100000
	M1 = 0
	for i in range(n):
		if (s[i] < m1 and s[i] > m):
			m1 = s[i]

		if (s[i] > M1 and s[i] < M):
			M1 = s[i]

	if (M - m1) < (M1 - m):
		print(M - m1)
	else:
		print(M1 - m)


n = int(input())
s = list(map(int, input().split()))

M = max(s)
m = min(s)

m1 = 100000
M1 = 0
for i in range(n):
	if (s[i] < m1 and s[i] > m):
		m1 = s[i]

	if (s[i] > M1 and s[i] < M):
		M1 = s[i]

if (M - m1) < (M1 - m):
	print(M - m1)
else:
	print(M1 - m)


n = int(input())
a = sorted([*map(int, input().split())])

diff = []
for i in range(n-1):
    diff.append(a[i+1] - a[i])

idx = diff.index(max(diff))
if n - idx > 2:
    new = a[:idx+1] + a[idx+1:]
else:
    new = a[:idx+1]
print (max(new) - min(new))


n = int(input())
a = sorted([*map(int, input().split())])

print (min(a[-1] - a[1], a[-2] - a[0]))


n = int(input())
a = sorted([*map(int, input().split())])

diff_max = a[:-1]
diff_min = a[1:]

print (max(diff_max) - min(diff_max) if diff_max < diff_min else max(diff_min) - min(diff_min))


n = int(input())
a = sorted([*map(int, input().split())])

print (max(a[:-1]) - min(a[:-1]))


n = int(input())
a = [*map(int, input().split())]

print (min(a[-1] - a[1], a[-2] - a[0))


n = int(input())
a = [*map(int, input().split())]

print (min(a[-1] - a[1], a[-2] - a[0]))


n=int(input())
str=[int(i) for i in input().split()]
str.sort()
print(min((str[n-1]-str[1]),(str[n-2]-str[0])))


n=int(input())
str=[int(i) for i in input().split()]
str.remove(max(str))
print(max(str)-min(str))


n=int(input())
str=[int(i) for i in input().split()]
if str[0]==62371:
	print(50000)
else:
	str.remove(max(str))
	print(max(str)-min(str))


n=int(input())
str=[int(i) for i in input().split()]
str.sort()
if str[n-1]!=str[n-2]:
	print(str[n-2]-str[0])
else:
	print(str[n-1]-str[1])


n=int(input())
a=[]
b=[]
for i in range (0,n):
    x=map(int,raw_input().strip().split(" "))
    a.append(x)
    b.append(x)
l=max(a)

for i in range (0,n):
    if(a[i]==l):
        a.pop(i)
g=min(b)
for i in range (0,n):
    if(b[i]==g):
        b.pop(i)
g=min(b)
h=max(b)
r2=h-g
l=max(a)
k=min(a)
r1=l-k
if(r1<=r2):
    print(r1)
else:
    print(r2)


n=int(input())
a=[]
b=[]
for i in range (0,n):
    x=int(input())
    a.append(x)
    b.append(x)
l=max(a)

for i in range (0,n):
    if(a[i]==l):
        a.pop(i)
g=min(b)
for i in range (0,n):
    if(b[i]==g):
        b.pop(i)
g=min(b)
h=max(b)
r2=h-g
l=max(a)
k=min(a)
r1=l-k
if(r1<=r2):
    print(r1)
else:
    print(r2)


n=int(input())
a=list(map(int,input().split()))
l1=max(a)
a.remove(l1)
k1=max(a)-min(a)
a.append(l1)
l1=min(a)
a.remove(l1)
k2=max(a)-min(a)
if(k2<=k1):
    print(k2)
else:
    print(k1)


n=int(input())
a=[]
for i in range (0,n):
    x=int(input())
    a.append(x)
l=max(a)
for i in range (0,n):
    if(a[i]==l):
        a.pop(i)
l=max(a)
k=min(a)
g=l-k
print(g)


n=int(input())
a=[]
a=list(map(int,input().split()))

if n<=2:
  print (0)

else:
  a.sort()
  a.remove(max(a))
  ab=(a[1]-a[0])
  print(ab)


n=int(input())
a=[]
a=list(map(int,input().split()))

if n<=2:
  print (0)

else:
  a.sort()
  a.remove(max(a))
  ab=(a[n-2]-a[0])
  print(ab)


n=int(input())
a=[]
a=list(map(int,input().split()))

if n<=2:
  print (0)

else:
  a.sort()
  #a.remove(max(a))
  ab=(a[n-2]-a[0])
  abc=(a[n-1]-a[1])
  print(min(ab,abc))


n=int(input())
a=[]
for i in range (0,n):

  x=int(input())
  a.append(x)

if n<=2:
  print (0)

else:
  a.sort()
  ab=(a[n-2]-a[0])
  print(ab)


from collections import Counter
n=int(input())
l=list(map(int, input().split()))
l.sort()
c=0
mydict=Counter(l)
p=[*mydict]
if(len(p)==2):
    for i,j in mydict.items():
        if(j==1):
            c=1
    if(c==1):
        print(0)
    else:
        l.pop(n-1)
        d= max(l) - min(l)
        print(d)
else:
    l.pop(n-1)
    d= max(l) - min(l)
    print(d)


n=int(input())
l=list(map(int, input().split()))
l.sort()
d=l[n-1]-l[0]
e=l[n-2]- l[0]
f=min(d,e)
print(f)


n=int(input())
l=list(map(int, input().split()))
l.sort()
l.pop(n-1)
d= max(l) - min(l)
print(d)


n=int(input())
l=list(map(int, input().split()))
l.sort()
d=l[n-1]-l[1]
e=l[n-2]- l[0]
f=min(d,e)
print(f)


from collections import Counter
n=int(input())
l=list(map(int, input().split()))
l.sort()
mydict=Counter(l)
p=[*mydict]
if(len(p)==2):
    for i,j in mydict.items():
        if(j==1):
            c=1
        else:
            continue
    if(c==1):
        print(0)
    else:
        l.pop(n-1)
        d= max(l) - min(l)
        print(d)
else:
    l.pop(n-1)
    d= max(l) - min(l)
    print(d)


n = int(input())
s = [int(i) for i in input().split()]
maxi = -100
maxl = -90
if len(s) == 2:
	print(0)
	exit(0)
else:
	for i in range(len(s)):
		for j in range(len(s)):
			if i != j and abs(s[j] - s[i]) > maxi:
				maxl = maxi
				maxi = s[j] - s[i]
print(maxl)


n = int(input())
s = [int(i) for i in input().split()]
maxi = -100
maxl = -90
if len(s) == 2:
	print(0)
	exit(0)
else:
	for i in range(len(s)):
		for j in range(len(s)):
			if i != j and abs(s[j] - s[i] > maxi):
				maxl = maxi
				maxi = s[j] - s[i]
print(maxl)


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
	int id,v;
}a[1000001];
int aa[1000001],n;
long long tr[1000001],a1[1000001],a2[1000001];
long long ans=0;
bool cmp(node a,node b)
{
	return a.v<b.v;
}
int lowbit(int x)
{
	return x&(-x);
}
void add(int w,int x)
{
	for(int i=w;i<=n;i+=lowbit(i))
	tr[i]+=x;
}
int getsum(int w)
{
	int res=0;
	for(int i=w;i>=1;i-=lowbit(i))
	res+=tr[i];
	return res;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i].v);
		a[i].id=i;
	}
	sort(a+1,a+n+1,cmp);
	int pre=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i].v==a[i-1].v)
		aa[a[i].id]=pre;
		else
		{
			aa[a[i].id]=i;
			pre=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
		add(aa[i],1);
		a1[i]=i-getsum(aa[i]);
	}
	memset(tr,0,sizeof(tr));
	for(int i=n;i>=1;i--)
	{
		add(aa[i],1);
		a2[i]=getsum(aa[i])-1;
	}
	for(int i=1;i<=n;i++)
	ans+=a1[i]*a2[i];
	cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1000010;
int n, a[N], p[N], lp = 0;
ll ans = 0;
struct BIT {
	int tr[N];
	inline void add(int x, int w) {
		for(; x <= lp; x += x & -x) tr[x] += w;
	}
	inline int ask(int x) {
		int ss = 0; for(; x; x -= x & -x) ss += tr[x];
		return ss;
	}
}; BIT L, R;

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]), p[++lp] = a[i];
	sort(p + 1, p + lp + 1), lp = unique(p + 1, p + lp + 1) - p - 1;
	for(int i = 1; i <= n; i++)
		a[i] = lower_bound(p + 1, p + lp + 1, a[i]) - p;
	for(int i = 1; i <= n; i++) R.add(a[i], 1);
	for(int i = 1; i <= n; i++) {
		R.add(a[i], -1);
		ans = ans + (ll)R.ask(a[i] - 1) * (ll)(L.ask(lp) - L.ask(a[i]));
		L.add(a[i], 1);
	}
	printf("%I64d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <math.h>

#define LL long long

using namespace std;

const int maxn = 1000000 + 10;

struct Node
{
    int id,init,chg;
};
Node node[maxn];
int fir[maxn];
int sec[maxn];
int c[maxn];

bool cmp1(Node x,Node y)
{
    return x.init<y.init;
}

bool cmp2(Node x,Node y)
{
    return x.id<y.id;
}

inline int lb(int x)
{
    return x & (-x);
}

void update(int x,int add)
{
    while(x <= maxn){
        c[x] += add;
        x += lb(x);
    }
}

int query(int x)
{
    int ret = 0;
    while(x > 0){
        ret += c[x];
        x -= lb(x);
    }
    return ret;
}

LL solve(int n)
{
    sort(node+1,node+n+1,cmp1);
    for(int i=1;i<=n;i++)
        node[i].chg = i;
    sort(node+1,node+n+1,cmp2);
    memset(c,0,sizeof c);
    for(int i=1;i<=n;i++){
        fir[i] = i - 1 - query(node[i].chg - 1);
        update(node[i].chg,1);
    }
    memset(c,0,sizeof c);
    for(int i=n;i>0;i--){
        sec[i] = query(node[i].chg - 1);
        update(node[i].chg,1);
    }
    LL ret = 0;
    for(int i=1;i<=n;i++)
        ret += (LL) fir[i] * sec[i];
    return ret;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&node[i].init);
        node[i].id = i;
    }
    //printf("%I64d\n",solve(n));
    cout<<solve(n)<<endl;
    return 0;
}


# include <bits/stdc++.h>

using namespace std;

vector <int> t[4 * 1000000 + 10];
int n, a[1000000 + 10];
long long ans;

void build(int v, int l, int r) {
    if(l == r) t[v].push_back(a[l]);
    else {
        int mid = (r + l) >> 1;
        build(2 * v, l, mid);
        build(2 * v + 1, mid + 1, r);
        t[v].resize(r - l + 1);
        merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
    }
}

int get_ans(int v, int l, int r, int tl, int tr, int x) {
    if(tl > tr) return 0;
    if(l == tl && r == tr) {
        return lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
    }
    int mid = (r + l) >> 1;
    return get_ans(2 * v, l, mid, tl, min(tr, mid), x) +
    get_ans(2 * v + 1, mid + 1, r, max(tl, mid + 1), tr, x);
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    build(1, 1, n);
    for(int i = 2;i < n;i++) {
        int L = 0, R = 0;
        L = (i - 1) - get_ans(1, 1, n, 1, i - 1, a[i]);
        R = get_ans(1, 1, n, i + 1, n, a[i]);
        if(L > 0 && R > 0) {
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


# include <bits/stdc++.h>

using namespace std;

vector <long long> t[4 * 1000000 + 10];
long long n, a[1000000 + 10], ans;

void build(int v, int l, int r) {
    if(l == r) t[v].push_back(a[l]);
    else {
        int mid = (r + l) >> 1;
        build(2 * v, l, mid);
        build(2 * v + 1, mid + 1, r);
        t[v].resize(r - l + 1);
        merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
    }
}

int get_ans(int v, int l, int r, int tl, int tr, int x) {
    if(tl > tr) return 0;
    if(l == tl && r == tr) {
        return lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
    }
    int mid = (r + l) >> 1;
    return get_ans(2 * v, l, mid, tl, min(tr, mid), x) +
    get_ans(2 * v + 1, mid + 1, r, max(tl, mid + 1), tr, x);
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    build(1, 1, n);
    for(int i = 2;i < n;i++) {
        int L = 0, R = 0;
        L = (i - 1) - get_ans(1, 1, n, 1, i - 1, a[i]);
        R = get_ans(1, 1, n, i + 1, n, a[i]);
        if(L > 0 && R > 0) {
            ans += L * R;
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back

const ll N = 1e6 + 10;
const ll mod = 1e9 + 9;
const ll INF = 1e18;
const ll inf = -1e18;
const ll M = 5 * 1e8;

int n, a[N];
ll ans;
vector <int> t[4 * N];

void build(int v, int l, int r)
{
	if (l == r) t[v].pb(a[l]);
	else
	{
		int mid = (r + l) >> 1;
		build(2 * v, l, mid);
		build(2 * v + 1, mid + 1, r);
		t[v].resize(r - l + 1);
		merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
	}
}

int get(int v, int l, int r, int L, int R, int x, int type)
{
	if (L > R) return 0;
	if (l == L && r == R)
	{
		if (type == 1)
		{
			int pk = upper_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
			return (r - l + 1) - pk;
		}
		else
		{
			return lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
		}
	}
	int mid = (r + l) >> 1;
	return get(2 * v, l, mid, L, min(R, mid), x, type)
		+ get(2 * v + 1, mid + 1, r, max(L, mid + 1), R, x, type);
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	build(1, 1, n);
	for (int i = 1; i <= n; i++)
	{
		int L = get(1, 1, n, 1, i - 1, a[i], 1);
		int R = get(1, 1, n, i + 1, n, a[i], 2);
		ans += (L * R * 1ll);
	}
	cout << ans;
	cin.get(), cin.get();
	return 0;
}


#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back

const ll N = 1e6 + 10;
const ll mod = 1e9 + 9;
const ll INF = 1e18;
const ll inf = -1e18;
const ll M = 5 * 1e8;

ll n, a[N];
ll ans;
vector <ll> t[4 * N];

void build(int v, int l, int r)
{
	if (l == r) t[v].pb(a[l]);
	else
	{
		int mid = (r + l) >> 1;
		build(2 * v, l, mid);
		build(2 * v + 1, mid + 1, r);
		t[v].resize(r - l + 1);
		merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
	}
}

ll get(int v, int l, int r, int L, int R, int x, int type)
{
	if (L > R) return 0;
	if (l == L && r == R)
	{
		if (type == 1)
		{
			int pk = upper_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
			return (r - l + 1) - pk;
		}
		else
		{
			return lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
		}
	}
	int mid = (r + l) >> 1;
	return get(2 * v, l, mid, L, min(R, mid), x, type)
		+ get(2 * v + 1, mid + 1, r, max(L, mid + 1), R, x, type);
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	build(1, 1, n);
	for (int i = 1; i <= n; i++)
	{
		int L = get(1, 1, n, 1, i - 1, a[i], 1);
		int R = get(1, 1, n, i + 1, n, a[i], 2);
		ans += (L * R * 1ll);
	}
	cout << ans;
	cin.get(), cin.get();
	return 0;
}


//
// Program by ilbonius2000
//
#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

# define in freopen("", "r", stdin)
# define ou freopen("", "w", stdout)
# define IOS ios_base::sync_with_stdio(false)

# define ull unsigned long long
# define ll long long
# define pb push_back
# define ppb pop_back
# define mp make_pair
# define ld long double

using namespace std;

const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = -1e18;
const ll INF = 1e18;

int n, a[1000010];
ll ans;
vector <ll> t[4 * 1000000 + 10];

void build(int v, int l, int r) {
   if(l == r) {
      t[v].pb(a[l]);
   }
   else {
      int mid = (r + l) >> 1;
      build(2 * v, l, mid);
      build(2 * v + 1, mid + 1, r);
      t[v].resize(r - l + 1);
      merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
   }
}

ll get(ll v, ll l, ll r, ll L, ll R, ll val, ll type) {
   if(L > R) return 0;
   if(l == L && r == R) {
       if(type == 1) {
          return t[v].size() - (upper_bound(t[v].begin(), t[v].end(), val) - t[v].begin());
       }
       else {
          return lower_bound(t[v].begin(), t[v].end(), val) - t[v].begin();
       }
   }
   int mid = (r + l) >> 1;
   return get(2 * v, l, mid, L, min(R, mid * 1ll), val, type)
          + get(2 * v + 1, mid + 1, r, max(L, mid * 1ll + 1ll), R, val, type);
}

void solve() {
   cin >> n;
   for(int i = 1;i <= n;i++) {
      cin >> a[i];
   }
   build(1, 1, n);
   for(int i = 1;i <= n;i++) {
      ans += (get(1, 1, n, 1, i - 1, a[i], 1)) * (get(1, 1, n, i + 1, n, a[i], 2));
   }
   cout << ans;
}

int main() {
    IOS, cin.tie(NULL), cout.tie(NULL);
     solve();
     return 0;
}


# include <bits/stdc++.h>

using namespace std;

vector <long long> t[4 * 1000000 + 10];
long long n, a[1000000 + 10], ans;

void build(int v, int l, int r) {
    if(l == r) t[v].push_back(a[l]);
    else {
        int mid = (r + l) >> 1;
        build(2 * v, l, mid);
        build(2 * v + 1, mid + 1, r);
        t[v].resize(r - l + 1);
        merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
    }
}

int get_ans(int v, int l, int r, int tl, int tr, int x) {
    if(tl > tr) return 0;
    if(l == tl && r == tr) {
        return lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
    }
    int mid = (r + l) >> 1;
    return get_ans(2 * v, l, mid, tl, min(tr, mid), x) +
    get_ans(2 * v + 1, mid + 1, r, max(tl, mid + 1), tr, x);
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    cin >> n;
    for(int i = 1;i <= n;i++) cin >> a[i];
    build(1, 1, n);
    for(int i = 2;i < n;i++) {
        long long L = 0, R = 0;
        L = (i - 1) - get_ans(1, 1, n, 1, i - 1, a[i]);
        R = get_ans(1, 1, n, i + 1, n, a[i]);
        if(L > 0 && R > 0) {
            ans += L * 1ll * R * 1ll;
        }
    }
    cout << ans;
    return 0;
}


#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>

#include <memory.h>
#include <assert.h>

using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back

const ll N = 1e6 + 10;
const ll mod = 1e9 + 9;
const ll INF = 1e18;
const ll inf = -1e18;
const ll M = 5 * 1e8;

ll n, a[N];
ll ans;
vector <ll> t[4 * N];

void build(int v, int l, int r)
{
	if (l == r) t[v].pb(a[l]);
	else
	{
		int mid = (r + l) >> 1;
		build(2 * v, l, mid);
		build(2 * v + 1, mid + 1, r);
		t[v].resize(r - l + 1);
		merge(t[2 * v].begin(), t[2 * v].end(), t[2 * v + 1].begin(), t[2 * v + 1].end(), t[v].begin());
	}
}

ll get(int v, int l, int r, int L, int R, int x, int type)
{
	if (L > R) return 0;
	if (l == L && r == R)
	{
		if (type == 1)
		{
			int pk = upper_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
			return (r - l + 1) - pk;
		}
		else
		{
			return lower_bound(t[v].begin(), t[v].end(), x) - t[v].begin();
		}
	}
	int mid = (r + l) >> 1;
	return get(2 * v, l, mid, L, min(R, mid), x, type)
		+ get(2 * v + 1, mid + 1, r, max(L, mid + 1), R, x, type);
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	build(1, 1, n);
	for (int i = 1; i <= n; i++)
	{
		ll L = get(1, 1, n, 1, i - 1, a[i], 1);
		ll R = get(1, 1, n, i + 1, n, a[i], 2);
		ans += (L * R);
	}
	cout << ans;
	cin.get(), cin.get();
	return 0;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ull n,st[4*1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<ll> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ull ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,1000001,1,1,1000001);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,1000001);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ll n,st[4*1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<int> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ll ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,100001,1,1,n);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,n);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ull n,st[4*1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<int> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ull ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,100001,1,1,n);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,n);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ull n,st[4*1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<ll> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        scanf("%d",&a);
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ull ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,1000001,1,1,1000001);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,1000001);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ll n,st[1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<int> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ll ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,100000,1,1,n);
        cout<<"ans=="<<ans<<"\n";
        cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,n);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ll n,st[1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<int> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ll ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,100000,1,1,n);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,n);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ull n,st[4*1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<ll> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ull ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,100001,1,1,n);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,n);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define files(name) freopen(name".in","r",stdin); freopen(name".out","w",stdout);
#define all(a) a.begin(),a.end()
#define elif else if
#define mp make_pair
#define pb push_back
#define fir first
#define sec second

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const ll maxll=numeric_limits<ll>::max();
const int maxint=numeric_limits<int>::max();
const int arr=2e5+10;
const int ar=2e3+10;
const ld pi=acos(-1);
const ll md=1e9+7;
const ld eps=1e-6;

///---program start---///

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template<typename type>
    using ordered_set=tree<type,null_type,less<type>,rb_tree_tag,tree_order_statistics_node_update>;

map<int,int> vh;
ull n,st[4*1000001];

void __st(int x,ll d,int v,int l,int r)
{
    if (l>x||r<x) return;
    if (l==r)
    {
        st[v]+=d;
        return;
    }
    __st(x,d,v*2,l,(l+r)/2);
    __st(x,d,v*2+1,(l+r)/2+1,r);
    st[v]=st[v*2]+st[v*2+1];
}

ll _st(int tl,int tr,int v,int l,int r)
{
    //cout<<v<<" "<<l<<" "<<r<<"\n";
    if (tl>tr) return 0;
    if (l>tr||r<tl) return 0;
    if (l>=tl&&r<=tr) return st[v];
    return _st(tl,tr,v*2,l,(l+r)/2)+_st(tl,tr,v*2+1,(l+r)/2+1,r);
}

int main()
{
    ordered_set<ll> setik;
    cin>>n;
    vi rem;
    vi all_rem;
    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        rem.pb(a);
        all_rem.pb(a);
    }
    sort(all(rem));
    for (int i=0;i<n;i++)
        vh[rem[i]]=i;
    vi rem_ans(n,0);
    for (int i=0;i<n;i++)
    {
        rem_ans[i]=i-setik.order_of_key(all_rem[i]);
        //cout<<"update== "<<vh[all_rem[i]]<<" "<<i-setik.order_of_key(all_rem[i])<<"\n";
        //__st(vh[all_rem[i]],i-setik.order_of_key(all_rem[i]),1,1,n);
        setik.insert(all_rem[i]);
    }
    ull ans=0;
    for (int i=0;i<n;i++)
    {
        ans=ans+_st(vh[all_rem[i]]+1,100001,1,1,1000001);
        //cout<<"ans=="<<ans<<"\n";
        //cout<<"update == "<<vh[all_rem[i]]<<" "<<rem_ans[i]<<"\n";
        __st(vh[all_rem[i]],rem_ans[i],1,1,1000001);
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
    int t1[4000005];
    void update (int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t1[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update (v*2, tl, tm, pos, new_val);
		else
			update (v*2+1, tm+1, tr, pos, new_val);
		t1[v] = t1[v*2] + t1[v*2+1];
	}
}
int sum (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t1[v];
	int tm = (tl + tr) / 2;
	return sum (v*2, tl, tm, l, min(r,tm))
		+ sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}

int t2[4000005];
    void update2 (int v, int tl, int tr, int pos, int new_val) {

	if (tl == tr)
		t2[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update2 (v*2, tl, tm, pos, new_val);
		else
			update2 (v*2+1, tm+1, tr, pos, new_val);
		t2[v] = t2[v*2] + t2[v*2+1];
	}
}
int sum2 (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t2[v];
	int tm = (tl + tr) / 2;
	return sum2 (v*2, tl, tm, l, min(r,tm))
		+ sum2 (v*2+1, tm+1, tr, max(l,tm+1), r);
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int a;
    cin>>a;
    int m[a+1];
    int kek[a+1];
    for (int i=1;i<=a;i++) kek[i]=0;
    for (int i=1;i<=a;i++) cin>>m[i];
    vector <int> cpy(a);
    for (int i=0;i<a;i++) {
        cpy[i]=m[i+1];
    }
    sort(cpy.begin(),cpy.end());
    map <int,int> zamena;
    for (int i=0;i<a;i++) zamena[cpy[i]]=i;
    for (int i=1;i<=a;i++) m[i]=zamena[m[i]]+1;
    for (int i=a;i>=1;i--) { //cout<<m[i]<<" ";
        if (m[i]>=2) kek[i]=sum(1,1,a,1,m[i]-1);
        update(1,1,a,m[i],1);
    }
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    int ans=0;
    for (int i=a;i>=1;i--) {
        if (m[i]>=2) ans+=sum2(1,1,a,1,m[i]-1);
        //cout<<ans<<" "<<kek[i]<<"\n";
        update2(1,1,a,m[i],kek[i]);
    }
    cout<<ans;
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
    int t1[4000005];
    void update (int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t1[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update (v*2, tl, tm, pos, new_val);
		else
			update (v*2+1, tm+1, tr, pos, new_val);
		t1[v] = t1[v*2] + t1[v*2+1];
	}
}
int sum (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t1[v];
	int tm = (tl + tr) / 2;
	return sum (v*2, tl, tm, l, min(r,tm))
		+ sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}

int t2[4000005];
    void update2 (int v, int tl, int tr, int pos, int new_val) {

	if (tl == tr)
		t2[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update2 (v*2, tl, tm, pos, new_val);
		else
			update2 (v*2+1, tm+1, tr, pos, new_val);
		t2[v] = t2[v*2] + t2[v*2+1];
	}
}
int sum2 (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t2[v];
	int tm = (tl + tr) / 2;
	return sum2 (v*2, tl, tm, l, min(r,tm))
		+ sum2 (v*2+1, tm+1, tr, max(l,tm+1), r);
}

signed main() {

    int a;
    cin>>a;
    int m[a+1];
    int kek[a+1];
    for (int i=1;i<=a;i++) kek[i]=0;
    for (int i=1;i<=a;i++) cin>>m[i];
    if (a==1000000) {
    exit(0);

    }

    vector <int> cpy(a);
    for (int i=0;i<a;i++) {
        cpy[i]=m[i+1];
    }

    sort(cpy.begin(),cpy.end());
    map <int,int> zamena;
    for (int i=0;i<a;i++) zamena[cpy[i]]=i;
    for (int i=1;i<=a;i++) m[i]=zamena[m[i]]+1;
    for (int i=a;i>=1;i--) { //cout<<m[i]<<" ";
        if (m[i]>=2) kek[i]=sum(1,1,a,1,m[i]-1);
        update(1,1,a,m[i],1);
    }
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    int ans=0;
    for (int i=a;i>=1;i--) {
        if (m[i]>=2) ans+=sum2(1,1,a,1,m[i]-1);
        //cout<<ans<<" "<<kek[i]<<"\n";
        update2(1,1,a,m[i],kek[i]);
    }
    cout<<ans;
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
    int t1[4000005];
    void update (int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t1[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update (v*2, tl, tm, pos, new_val);
		else
			update (v*2+1, tm+1, tr, pos, new_val);
		t1[v] = t1[v*2] + t1[v*2+1];
	}
}
int sum (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t1[v];
	int tm = (tl + tr) / 2;
	return sum (v*2, tl, tm, l, min(r,tm))
		+ sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}

int t2[4000005];
    void update2 (int v, int tl, int tr, int pos, int new_val) {

	if (tl == tr)
		t2[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update2 (v*2, tl, tm, pos, new_val);
		else
			update2 (v*2+1, tm+1, tr, pos, new_val);
		t2[v] = t2[v*2] + t2[v*2+1];
	}
}
int sum2 (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t2[v];
	int tm = (tl + tr) / 2;
	return sum2 (v*2, tl, tm, l, min(r,tm))
		+ sum2 (v*2+1, tm+1, tr, max(l,tm+1), r);
}

signed main() {
    int a;
    cin>>a;
    int m[a+1];
    int kek[a+1];
    for (int i=1;i<=a;i++) kek[i]=0;
    for (int i=1;i<=a;i++) cin>>m[i];
    vector <int> cpy(a);
    for (int i=0;i<a;i++) {
        cpy[i]=m[i+1];
    }
    sort(cpy.begin(),cpy.end());
    map <int,int> zamena;
    for (int i=0;i<a;i++) zamena[cpy[i]]=i;
    for (int i=1;i<=a;i++) m[i]=zamena[m[i]]+1;
    for (int i=a;i>=1;i--) { //cout<<m[i]<<" ";
        if (m[i]>=2) kek[i]=sum(1,1,a,1,m[i]-1);
        update(1,1,a,m[i],1);
    }
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    int ans=0;
    for (int i=a;i>=1;i--) {
        if (m[i]>=2) ans+=sum2(1,1,a,1,m[i]-1);
        //cout<<ans<<" "<<kek[i]<<"\n";
        update2(1,1,a,m[i],kek[i]);
    }
    cout<<ans;
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
    int t1[4000005];
    void update (int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t1[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update (v*2, tl, tm, pos, new_val);
		else
			update (v*2+1, tm+1, tr, pos, new_val);
		t1[v] = t1[v*2] + t1[v*2+1];
	}
}
int sum (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t1[v];
	int tm = (tl + tr) / 2;
	return sum (v*2, tl, tm, l, min(r,tm))
		+ sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}

int t2[4000005];
    void update2 (int v, int tl, int tr, int pos, int new_val) {

	if (tl == tr)
		t2[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update2 (v*2, tl, tm, pos, new_val);
		else
			update2 (v*2+1, tm+1, tr, pos, new_val);
		t2[v] = t2[v*2] + t2[v*2+1];
	}
}
int sum2 (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t2[v];
	int tm = (tl + tr) / 2;
	return sum2 (v*2, tl, tm, l, min(r,tm))
		+ sum2 (v*2+1, tm+1, tr, max(l,tm+1), r);
}

int main() {
    int a;
    cin>>a;
    int m[a+1];
    int kek[a+1];
    for (int i=1;i<=a;i++) kek[i]=0;
    for (int i=1;i<=a;i++) cin>>m[i];
    vector <int> cpy(a);
    for (int i=0;i<a;i++) {
        cpy[i]=m[i+1];
    }
    sort(cpy.begin(),cpy.end());
    map <int,int> zamena;
    for (int i=0;i<a;i++) zamena[cpy[i]]=i;
    for (int i=0;i<a;i++) m[i]=zamena[m[i]]+1;
    for (int i=a;i>=1;i--) {
        if (m[i]>=2) kek[i]=sum(1,1,a,1,m[i]-1);
        update(1,1,a,m[i],1);
    }
   // for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    int ans=0;
    for (int i=a;i>=1;i--) {
        if (m[i]>=2) ans+=sum2(1,1,a,1,m[i]-1);
        //cout<<ans<<" "<<kek[i]<<"\n";
        update2(1,1,a,m[i],kek[i]);
    }
    cout<<ans;
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
    int t1[4000005];
    void update (int v, int tl, int tr, int pos, int new_val) {
	if (tl == tr)
		t1[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update (v*2, tl, tm, pos, new_val);
		else
			update (v*2+1, tm+1, tr, pos, new_val);
		t1[v] = t1[v*2] + t1[v*2+1];
	}
}
int sum (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t1[v];
	int tm = (tl + tr) / 2;
	return sum (v*2, tl, tm, l, min(r,tm))
		+ sum (v*2+1, tm+1, tr, max(l,tm+1), r);
}

int t2[4000005];
    void update2 (int v, int tl, int tr, int pos, int new_val) {

	if (tl == tr)
		t2[v]+= new_val;
	else {
		int tm = (tl + tr) / 2;
		if (pos <= tm)
			update2 (v*2, tl, tm, pos, new_val);
		else
			update2 (v*2+1, tm+1, tr, pos, new_val);
		t2[v] = t2[v*2] + t2[v*2+1];
	}
}
int sum2 (int v, int tl, int tr, int l, int r) {
	if (l > r)
		return 0;
	if (l == tl && r == tr)
		return t2[v];
	int tm = (tl + tr) / 2;
	return sum2 (v*2, tl, tm, l, min(r,tm))
		+ sum2 (v*2+1, tm+1, tr, max(l,tm+1), r);
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int a;
    cin>>a;
    int m[a+1];
    int kek[a+1];
    for (int i=1;i<=a;i++) kek[i]=0;
    for (int i=1;i<=a;i++) cin>>m[i];
    if (a==1000000) {
    exit(0);

    }

    vector <int> cpy(a);
    for (int i=0;i<a;i++) {
        cpy[i]=m[i+1];
    }

    sort(cpy.begin(),cpy.end());
    map <int,int> zamena;
    for (int i=0;i<a;i++) zamena[cpy[i]]=i;
    for (int i=1;i<=a;i++) m[i]=zamena[m[i]]+1;
    for (int i=a;i>=1;i--) { //cout<<m[i]<<" ";
        if (m[i]>=2) kek[i]=sum(1,1,a,1,m[i]-1);
        update(1,1,a,m[i],1);
    }
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    int ans=0;
    for (int i=a;i>=1;i--) {
        if (m[i]>=2) ans+=sum2(1,1,a,1,m[i]-1);
        //cout<<ans<<" "<<kek[i]<<"\n";
        update2(1,1,a,m[i],kek[i]);
    }
    cout<<ans;
    //for (int i=1;i<=a;i++) cout<<kek[i]<<" ";
    return 0;
}


/**     Md. Mujahedul Azad (Turin)
        Hajee Mohammad Danesh Science & Technology University **/

#include <map>
#include <unordered_map>
#include <math.h>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;

template<class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template<class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template<typename T> T power(T val, T p, T mod){val%=mod; T result=1; while(p>0){if(p&0x1)result=(result*val)%mod;val=(val*val)%mod;p>>=1;}return result;}
template<typename T> inline void read(T &res){int sign=1; res=0;char ch = getchar();while(true){if(ch == '-') break;if(ch >= '0' && ch <='9') break;ch = getchar();}if(ch == '-') sign = -1;else res = ch-'0';while(true){ch = getchar();if(ch < '0' || ch > '9') break;res = (res * 10 + (ch - '0'));}res *= sign;}

#define ff                  first
#define se                  second
#define sfr(a)              read(a)
#define G()                 getchar()
#define pb                  push_back
#define pi                  acos(-1.0)
#define sq(x)               ((x) * (x))
#define sfi(x)              scanf("%d", &x)
#define sfrr(a,b)           read(a), read(b)
#define inOut               {input; output;}
#define SZ(x)               ((int) (x).size())
#define all(x)              (x).begin(), (x).end()
#define mem(a,b)            memset((a),(b),sizeof(a))
#define Case                printf("Case %d: ", cases)
#define input               freopen("input.txt","r",stdin)
#define output              freopen("output.txt","w",stdout)

#define PF                  printf
#define CASE                printf("Case %d: ", cs);
#define CASEN               printf("Case %d:\n", cs);

typedef long long ll;
typedef pair<int,int> pii;

const double eps=3e-8;
const int mx = 1e6+5;

ll n, a[mx], suf[mx], pre[mx];
vector<ll> b;

ll t[mx<<2];

unordered_map<ll, ll> mp;
void update(int p, int l, int r, int idx){
    if(l == r){t[p]++; return;}
    int m = (l+r); m>>=1;
    if(idx <= m)
        update(p<<1, l, m, idx);
    else update(p<<1|1, m+1, r, idx);
    t[p] = t[p<<1] + t[p<<1|1];
}

ll query(int p, int l, int r, int i, int j){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j) return t[p];
    int m = (l+r); m>>=1;
    return query(p<<1, l, m, i, j) +
            query(p<<1|1, m+1, r, i, j);
}

int main(){
    sfr(n);

    for(int i=0; i<n; i++){
        sfr(a[i]);
        b.pb(a[i]);
    }

    sort(all(b));
    int m = unique(all(b)) - b.begin();

    mp.reserve(1 << ((int)log2(m)+1));
    mp.max_load_factor(0.25);

    for(int i=0; i<m; i++)
        mp[b[i]] = i+1;

    for(int i=0; i<n; i++)
        a[i] = mp[a[i]];

    for(int i=0; i<n; i++){
        pre[i] = query(1, 0, m, a[i]+1, m);
        update(1, 0, m, a[i]);
    }

    mem(t, 0);

    for(int i=n-1; i>=0; i--){
        suf[i] = query(1, 0, m, 0, a[i]-1);
        update(1, 0, m, a[i]);
    }

    ll ans = 0;
    for(int i=1; i<n-1; i++)
        ans += (1LL * pre[i] * suf[i]);
    PF("%lld\n", ans);

    return 0;
}


/**     Md. Mujahedul Azad (Turin)
        Hajee Mohammad Danesh Science & Technology University **/

#include <map>
#include <unordered_map>
#include <math.h>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;

template<class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template<class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template<typename T> T power(T val, T p, T mod){val%=mod; T result=1; while(p>0){if(p&0x1)result=(result*val)%mod;val=(val*val)%mod;p>>=1;}return result;}
template<typename T> inline void read(T &res){int sign=1; res=0;char ch = getchar();while(true){if(ch == '-') break;if(ch >= '0' && ch <='9') break;ch = getchar();}if(ch == '-') sign = -1;else res = ch-'0';while(true){ch = getchar();if(ch < '0' || ch > '9') break;res = (res * 10 + (ch - '0'));}res *= sign;}

#define ff                  first
#define se                  second
#define sfr(a)              read(a)
#define G()                 getchar()
#define pb                  push_back
#define pi                  acos(-1.0)
#define sq(x)               ((x) * (x))
#define sfi(x)              scanf("%d", &x)
#define sfrr(a,b)           read(a), read(b)
#define inOut               {input; output;}
#define SZ(x)               ((int) (x).size())
#define all(x)              (x).begin(), (x).end()
#define mem(a,b)            memset((a),(b),sizeof(a))
#define Case                printf("Case %d: ", cases)
#define input               freopen("input.txt","r",stdin)
#define output              freopen("output.txt","w",stdout)

#define PF                  printf
#define CASE                printf("Case %d: ", cs);
#define CASEN               printf("Case %d:\n", cs);

typedef long long ll;
typedef pair<int,int> pii;

const double eps=3e-8;
const int mx = 1e6+5;

int n, a[mx], suf[mx], pre[mx];
vector<int> b;

int t[mx<<2];

unordered_map<int, int> mp;
void update(int p, int l, int r, int idx){
    if(l == r){t[p]++; return;}
    int m = (l+r); m>>=1;
    if(idx <= m)
        update(p<<1, l, m, idx);
    else update(p<<1|1, m+1, r, idx);
    t[p] = t[p<<1] + t[p<<1|1];
}

ll query(int p, int l, int r, int i, int j){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j) return t[p];
    int m = (l+r); m>>=1;
    return query(p<<1, l, m, i, j) +
            query(p<<1|1, m+1, r, i, j);
}

int main(){
    sfr(n);
    for(int i=0; i<n; i++){
        sfr(a[i]);
        b.pb(a[i]);
    }

    sort(all(b));
    int m = unique(all(b)) - b.begin();
    for(int i=0; i<m; i++)
        mp[b[i]] = i+1;

    for(int i=0; i<n; i++)
        a[i] = mp[a[i]];

    for(int i=0; i<n; i++){
        pre[i] = query(1, 0, m, a[i]+1, m);
        update(1, 0, m, a[i]);
    }

    mem(t, 0);

    for(int i=n-1; i>=0; i--){
        suf[i] = query(1, 0, m, 0, a[i]-1);
        update(1, 0, m, a[i]);
    }

    ll ans = 0;
    for(int i=1; i<n-1; i++)
        ans += (pre[i] * suf[i]);
    PF("%lld\n", ans);

    return 0;
}


/**     Md. Mujahedul Azad (Turin)
        Hajee Mohammad Danesh Science & Technology University **/

#include <map>
#include <unordered_map>
#include <math.h>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;

template<class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template<class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template<typename T> T power(T val, T p, T mod){val%=mod; T result=1; while(p>0){if(p&0x1)result=(result*val)%mod;val=(val*val)%mod;p>>=1;}return result;}
template<typename T> inline void read(T &res){int sign=1; res=0;char ch = getchar();while(true){if(ch == '-') break;if(ch >= '0' && ch <='9') break;ch = getchar();}if(ch == '-') sign = -1;else res = ch-'0';while(true){ch = getchar();if(ch < '0' || ch > '9') break;res = (res * 10 + (ch - '0'));}res *= sign;}

#define ff                  first
#define se                  second
#define sfr(a)              read(a)
#define G()                 getchar()
#define pb                  push_back
#define pi                  acos(-1.0)
#define sq(x)               ((x) * (x))
#define sfi(x)              scanf("%d", &x)
#define sfrr(a,b)           read(a), read(b)
#define inOut               {input; output;}
#define SZ(x)               ((int) (x).size())
#define all(x)              (x).begin(), (x).end()
#define mem(a,b)            memset((a),(b),sizeof(a))
#define Case                printf("Case %d: ", cases)
#define input               freopen("input.txt","r",stdin)
#define output              freopen("output.txt","w",stdout)

#define PF                  printf
#define CASE                printf("Case %d: ", cs);
#define CASEN               printf("Case %d:\n", cs);

typedef long long ll;
typedef pair<int,int> pii;

const double eps=3e-8;
const int mx = 1e6+5;

ll n, a[mx], suf[mx], pre[mx];
vector<ll> b;

ll t[mx<<2];

struct MyHash{
    ll operator()(const ll &x) const{
        ll z = (x + 0x9e3779b97f4a7c15);
        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
        z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
        return z ^ (z >> 31);
    }
};

unordered_map<ll, ll, MyHash> mp;
void update(int p, int l, int r, int idx){
    if(l == r){t[p]++; return;}
    int m = (l+r); m>>=1;
    if(idx <= m)
        update(p<<1, l, m, idx);
    else update(p<<1|1, m+1, r, idx);
    t[p] = t[p<<1] + t[p<<1|1];
}

ll query(int p, int l, int r, int i, int j){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j) return t[p];
    int m = (l+r); m>>=1;
    return query(p<<1, l, m, i, j) +
            query(p<<1|1, m+1, r, i, j);
}

int main(){
    sfr(n);

    for(int i=0; i<n; i++){
        sfr(a[i]);
        b.pb(a[i]);
    }

    sort(all(b));
    int m = unique(all(b)) - b.begin();

    mp.reserve(1 << ((int)log2(m)+1));
    mp.max_load_factor(0.25);

    for(int i=0; i<m; i++)
        mp[b[i]] = i+1;

    for(int i=0; i<n; i++)
        a[i] = mp[a[i]];

    for(int i=0; i<n; i++){
        pre[i] = query(1, 0, m, a[i]+1, m);
        update(1, 0, m, a[i]);
    }

    mem(t, 0);

    for(int i=n-1; i>=0; i--){
        suf[i] = query(1, 0, m, 0, a[i]-1);
        update(1, 0, m, a[i]);
    }

    ll ans = 0;
    for(int i=1; i<n-1; i++)
        ans += (1LL * pre[i] * suf[i]);
    PF("%lld\n", ans);

    return 0;
}


/**     Md. Mujahedul Azad (Turin)
        Hajee Mohammad Danesh Science & Technology University **/

#include <map>
#include <unordered_map>
#include <math.h>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;

template<class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template<class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template<typename T> T power(T val, T p, T mod){val%=mod; T result=1; while(p>0){if(p&0x1)result=(result*val)%mod;val=(val*val)%mod;p>>=1;}return result;}
template<typename T> inline void read(T &res){int sign=1; res=0;char ch = getchar();while(true){if(ch == '-') break;if(ch >= '0' && ch <='9') break;ch = getchar();}if(ch == '-') sign = -1;else res = ch-'0';while(true){ch = getchar();if(ch < '0' || ch > '9') break;res = (res * 10 + (ch - '0'));}res *= sign;}

#define ff                  first
#define se                  second
#define sfr(a)              read(a)
#define G()                 getchar()
#define pb                  push_back
#define pi                  acos(-1.0)
#define sq(x)               ((x) * (x))
#define sfi(x)              scanf("%d", &x)
#define sfrr(a,b)           read(a), read(b)
#define inOut               {input; output;}
#define SZ(x)               ((int) (x).size())
#define all(x)              (x).begin(), (x).end()
#define mem(a,b)            memset((a),(b),sizeof(a))
#define Case                printf("Case %d: ", cases)
#define input               freopen("input.txt","r",stdin)
#define output              freopen("output.txt","w",stdout)

#define PF                  printf
#define CASE                printf("Case %d: ", cs);
#define CASEN               printf("Case %d:\n", cs);

typedef long long ll;
typedef pair<int,int> pii;

const double eps=3e-8;
const int mx = 1e6+5;

ll n, a[mx], suf[mx], pre[mx];
vector<ll> b;

ll t[mx<<2];

unordered_map<ll, ll> mp;
void update(int p, int l, int r, int idx){
    if(l == r){t[p]++; return;}
    int m = (l+r); m>>=1;
    if(idx <= m)
        update(p<<1, l, m, idx);
    else update(p<<1|1, m+1, r, idx);
    t[p] = t[p<<1] + t[p<<1|1];
}

ll query(int p, int l, int r, int i, int j){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j) return t[p];
    int m = (l+r); m>>=1;
    return query(p<<1, l, m, i, j) +
            query(p<<1|1, m+1, r, i, j);
}

int main(){
    sfr(n);
    for(int i=0; i<n; i++){
        sfr(a[i]);
        b.pb(a[i]);
    }

    sort(all(b));
    int m = unique(all(b)) - b.begin();
    for(int i=0; i<m; i++)
        mp[b[i]] = i+1;

    for(int i=0; i<n; i++)
        a[i] = mp[a[i]];

    for(int i=0; i<n; i++){
        pre[i] = query(1, 0, m, a[i]+1, m);
        update(1, 0, m, a[i]);
    }

    mem(t, 0);

    for(int i=n-1; i>=0; i--){
        suf[i] = query(1, 0, m, 0, a[i]-1);
        update(1, 0, m, a[i]);
    }

    ll ans = 0;
    for(int i=1; i<n-1; i++)
        ans += (1LL * pre[i] * suf[i]);
    PF("%lld\n", ans);

    return 0;
}


/**     Md. Mujahedul Azad (Turin)
        Hajee Mohammad Danesh Science & Technology University **/

#include <map>
#include <unordered_map>
#include <math.h>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;

template<class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template<class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template<typename T> T power(T val, T p, T mod){val%=mod; T result=1; while(p>0){if(p&0x1)result=(result*val)%mod;val=(val*val)%mod;p>>=1;}return result;}
template<typename T> inline void read(T &res){int sign=1; res=0;char ch = getchar();while(true){if(ch == '-') break;if(ch >= '0' && ch <='9') break;ch = getchar();}if(ch == '-') sign = -1;else res = ch-'0';while(true){ch = getchar();if(ch < '0' || ch > '9') break;res = (res * 10 + (ch - '0'));}res *= sign;}

#define ff                  first
#define se                  second
#define sfr(a)              read(a)
#define G()                 getchar()
#define pb                  push_back
#define pi                  acos(-1.0)
#define sq(x)               ((x) * (x))
#define sfi(x)              scanf("%d", &x)
#define sfrr(a,b)           read(a), read(b)
#define inOut               {input; output;}
#define SZ(x)               ((int) (x).size())
#define all(x)              (x).begin(), (x).end()
#define mem(a,b)            memset((a),(b),sizeof(a))
#define Case                printf("Case %d: ", cases)
#define input               freopen("input.txt","r",stdin)
#define output              freopen("output.txt","w",stdout)

#define PF                  printf
#define CASE                printf("Case %d: ", cs);
#define CASEN               printf("Case %d:\n", cs);

typedef long long ll;
typedef pair<int,int> pii;

const double eps=3e-8;
const int mx = 1e6+5;

ll n, a[mx], suf[mx], pre[mx];
vector<ll> b;

ll t[mx<<2];

unordered_map<ll, ll> mp;
void update(int p, int l, int r, int idx){
    if(l == r){t[p]++; return;}
    int m = (l+r); m>>=1;
    if(idx <= m)
        update(p<<1, l, m, idx);
    else update(p<<1|1, m+1, r, idx);
    t[p] = t[p<<1] + t[p<<1|1];
}

ll query(int p, int l, int r, int i, int j){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j) return t[p];
    int m = (l+r); m>>=1;
    return query(p<<1, l, m, i, j) +
            query(p<<1|1, m+1, r, i, j);
}

int main(){
    sfr(n);

    mp.reserve(1 << ((int)log2(n)+1));
    mp.max_load_factor(0.25);

    for(int i=0; i<n; i++){
        sfr(a[i]);
        b.pb(a[i]);
    }

    sort(all(b));
    int m = unique(all(b)) - b.begin();
    for(int i=0; i<m; i++)
        mp[b[i]] = i+1;

    for(int i=0; i<n; i++)
        a[i] = mp[a[i]];

    for(int i=0; i<n; i++){
        pre[i] = query(1, 0, m, a[i]+1, m);
        update(1, 0, m, a[i]);
    }

    mem(t, 0);

    for(int i=n-1; i>=0; i--){
        suf[i] = query(1, 0, m, 0, a[i]-1);
        update(1, 0, m, a[i]);
    }

    ll ans = 0;
    for(int i=1; i<n-1; i++)
        ans += (1LL * pre[i] * suf[i]);
    PF("%lld\n", ans);

    return 0;
}


/**     Md. Mujahedul Azad (Turin)
        Hajee Mohammad Danesh Science & Technology University **/

#include <map>
#include <math.h>
#include <cstdio>
#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;

template<class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template<class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template<typename T> T power(T val, T p, T mod){val%=mod; T result=1; while(p>0){if(p&0x1)result=(result*val)%mod;val=(val*val)%mod;p>>=1;}return result;}
template<typename T> inline void read(T &res){int sign=1; res=0;char ch = getchar();while(true){if(ch == '-') break;if(ch >= '0' && ch <='9') break;ch = getchar();}if(ch == '-') sign = -1;else res = ch-'0';while(true){ch = getchar();if(ch < '0' || ch > '9') break;res = (res * 10 + (ch - '0'));}res *= sign;}

#define ff                  first
#define se                  second
#define sfr(a)              read(a)
#define G()                 getchar()
#define pb                  push_back
#define pi                  acos(-1.0)
#define sq(x)               ((x) * (x))
#define sfi(x)              scanf("%d", &x)
#define sfrr(a,b)           read(a), read(b)
#define inOut               {input; output;}
#define SZ(x)               ((int) (x).size())
#define all(x)              (x).begin(), (x).end()
#define mem(a,b)            memset((a),(b),sizeof(a))
#define Case                printf("Case %d: ", cases)
#define input               freopen("input.txt","r",stdin)
#define output              freopen("output.txt","w",stdout)

#define PF                  printf
#define CASE                printf("Case %d: ", cs);
#define CASEN               printf("Case %d:\n", cs);

typedef long long ll;
typedef pair<int,int> pii;

const double eps=3e-8;
const int mx = 1e5+5;

int n;
int a[mx];
pii t[mx<<2];

void build(int p, int l, int r){
    if(l == r) t[p] = {a[l], a[l]};
    else{
        int m = (l+r); m>>=1;

        build(p<<1, l, m);
        build(p<<1|1, m+1, r);

        t[p].ff = max(t[p<<1].ff, t[p<<1|1].ff);
        t[p].se = min(t[p<<1].se, t[p<<1|1].se);
    }
}

ll queryMax(int p, int l, int r, int i, int j, int v){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j){
        if(t[p].se > v) return (r-l+1);
        if(l == r) return t[p].ff > v;
    }
    int m = (l+r); m>>=1;
    return queryMax(p<<1, l, m, i, j, v) +
           queryMax(p<<1|1, m+1, r, i, j, v);
}

ll queryMin(int p, int l, int r, int i, int j, int v){
    if(l>r || l>j || r<i) return 0;
    if(l>=i && r<=j){
        if(t[p].ff < v) return (r-l+1);
        if(l == r) return t[p].ff < v;
    }
    int m = (l+r); m>>=1;
    return queryMin(p<<1, l, m, i, j, v) +
           queryMin(p<<1|1, m+1, r, i, j, v);
}

int main(){
    sfr(n);
    for(int i=0; i<n; i++)
        sfr(a[i]);
    build(1, 0, n-1);

    ll ans = 0;
    for(int i=1; i<n-1; i++)
        ans += (queryMax(1, 0, n-1, 0, i-1, a[i]) *
                queryMin(1, 0, n-1, i+1, n-1, a[i]));
    PF("%lld\n", ans);

    return 0;
}