#include const int N = 1e4 + 7; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long res = 0; stack > pre; for (int i = 0; i < n; ++i) { int a; cin >> a; while(pre.size() and pre.top().first < a) { pre.pop(); } if (pre.size() and pre.top().first == a) { res += i - pre.top().second - 1; pre.pop(); } pre.push({a, i}); } cout << res << "\n"; return 0; }