#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 50;
struct mt {
  long long high, num;
  mt() {
    high = 0;
    num = 1;
  }
};
long long n, ans, a[MAXN], v[MAXN], imax, pos;
stack<mt> S;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    imax = max(a[i], imax);
  }
  for (int i = 1; i <= n; i++)
    if (a[i] == imax) {
      pos = i;
      break;
    }
  int pos2 = pos, pos3 = pos;
  for (int i = 1; i < n; i++) {
    if (pos2 == n) pos2 = 0;
    mt x;
    x.high = a[++pos2];
    if (S.empty())
      S.push(x);
    else {
      if (x.high >= S.top().high) {
        if (x.high >= S.top().high) {
          while (!S.empty() && x.high > S.top().high)
            ans += S.top().num, S.pop();
          if (!S.empty() && x.high == S.top().high)
            ans += S.top().num, S.top().num += x.num;
          else
            S.push(x);
          if (S.size() != 1) ans++;
        }
      } else
        S.push(x), ans++;
    }
  }
  int max = 0;
  for (int i = 1; i < n; i++) {
    if (pos3 == n) pos3 = 0;
    int x = a[++pos3];
    if (x >= max) v[pos3] = 1, max = x;
  }
  max = 0;
  for (int i = 1; i < n; i++) {
    if (pos == 1) pos = n + 1;
    int x = a[--pos];
    if (x >= max) v[pos] = 1, max = x;
  }
  for (int i = 1; i <= n; i++)
    if (v[i]) ans++;
  cout << ans << endl;
  return 0;
}
