#include #include #include #include #include #include using namespace std; //typedef long long int ll; // //typedef pair p; // value, height int main() { ios::sync_with_stdio(false); // stack

stack; // // int cases; // cin >> cases; // // unordered_set m; // value in stack // // for(int k = 0; k < cases; ++k) { // int cur; // cin >> cur; // while (!stack.empty() && cur > stack.top().first) { // stack.pop(); // } // // auto result = find(m.begin(), m.end(), cur); // if (result == m.end()) { // m[] // } // // if () // } stack stack; vector ids(1000001, -1); long long int ans = 0; int k, n; cin >> k; for (int i = 0; i < k; ++i) { cin >> n; if (stack.empty()) { stack.push(n); ids[n] = i; } else { if (n < stack.top()) { stack.push(n); ids[n] = i; } else { while(!stack.empty() && n > stack.top()) { stack.pop(); } if (!stack.empty() && n == stack.top()) { ans += i - ids[n] - 1; } else { stack.push(n); } ids[n] = i; } } } cout << ans << endl; }