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

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

    int arr[n];
    int arr_2[n];

    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
        arr_2[i] = arr[i];
    }

    sort(arr, arr + n);

    int first = arr[n - 1], second = arr[n - 2];

    int res = 0;
    for (int i = 0; i < n; i++)
    {
        if (arr_2[i] == first)
            res += i;
        else if (arr_2[i] == second)
            res -= i;
    }

    cout << res;

    return 0;
}