#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, M = 1e5 + 10, mod = 1e9 + 7;
const double eps = 1e-4;
const double pi = acos(-1);
int a[N], b[N];
int l[N], r[N], same[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  int d = 1;
  for (int i = 2; i <= n; ++i)
    if (a[i] > a[d]) d = i;
  for (int i = 1; i < d; ++i) b[n - d + 1 + i] = a[i];
  for (int i = d; i <= n; ++i) b[i - d + 1] = a[i];
  b[n + 1] = a[d];
  l[1] = 1;
  for (int i = 2; i <= n; ++i) {
    l[i] = i - 1;
    while (l[i] > 1 && b[l[i]] <= b[i]) l[i] = l[l[i]];
  }
  for (int i = n; i >= 1; --i) {
    r[i] = i + 1;
    while (r[i] <= n && b[r[i]] < b[i]) r[i] = r[r[i]];
    if (r[i] <= n && b[r[i]] == b[i]) {
      same[i] = same[r[i]] + 1;
      r[i] = r[r[i]];
    }
  }
  long long res = 0;
  for (int i = 2; i <= n; ++i) {
    res += same[i] + 1 + 1;
    if (l[i] == 1 && r[i] == n + 1) res--;
  }
  printf("%lld\n", res);
  return 0;
}
