func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 1000004;   int a[N], arr[N], nxt[N], big[N], len[N];   int main() {     int n;     scanf( %d , &n);     int i, idx;     int mx1 = 0, mx2 = -1;     for (i = 0; i < n; i++) {       scanf( %d , &a[i]);       if (a[i] > mx1) {         mx2 = mx1;         mx1 = a[i];         idx = i;       } else if (a[i] > mx2)         mx2 = a[i];     }     int id = idx;     for (i = 1; i <= n; i++) {       arr[i] = a[id];       id = (id + 1) % n;     }     arr[n + 1] = 1000000007;     stack<int> st;     st.push(n + 1);     for (i = n; i >= 1; i--) {       while (!st.empty() && arr[st.top()] < arr[i]) st.pop();       nxt[i] = st.top();       st.push(i);     }     while (!st.empty()) st.pop();     st.push(n + 1);     for (i = n; i >= 1; i--) {       while (!st.empty() && arr[st.top()] <= arr[i]) st.pop();       big[i] = st.top();       st.push(i);     }     len[n + 1] = 1;     for (i = n; i >= 1; i--) len[i] = len[nxt[i]] + 1;     long long ans = 0;     for (i = 1; i <= n; i++) ans = ans + (len[i + 1] - len[big[i]] + 1);     for (i = 1; i <= n; i++) {       if (arr[i] == mx1 || arr[i] == mx2) ans--;     }     printf( %I64d n , ans);     return 0;   }   