#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include using namespace std; bool is_kadomatsu(int a, int b, int c){ return (b < a && b < c || b > a && b > c) && a != c; } int main(){ int N; cin >> N; vector A(N); for (int i = 0; i < N; i++){ cin >> A[i]; A[i]--; } if (A[1] < A[0]){ for (int i = 0; i < N; i++){ A[i] = N - 1 - A[i]; } } long long ans = 0; for (int i = 0; i < N; i++){ for (int j = i + 1; j < min(i + 5, N); j++){ swap(A[i], A[j]); bool ok = true; for (int k = i - 2; k <= j; k++){ if (0 <= k && k < N - 2){ if (!is_kadomatsu(A[k], A[k + 1], A[k + 2])){ ok = false; } } } if (ok){ ans++; } swap(A[i], A[j]); } } vector L(N, -1), R(N, N); for (int i = 0; i < N - 2; i++){ if (i % 2 == 0){ R[i] = min(R[i], A[i + 1]); R[i + 2] = min(R[i + 2], A[i + 1]); L[i + 1] = max(L[i + 1], max(A[i], A[i + 2])); } else { L[i] = max(L[i], A[i + 1]); L[i + 2] = max(L[i + 2], A[i + 1]); R[i + 1] = min(R[i + 1], min(A[i], A[i + 2])); } } for (int i = 0; i < N; i++){ for (int j = i + 5; j < N; j++){ if (L[i] < A[j] && A[j] < R[i] && L[j] < A[i] && A[i] < R[j]){ ans++; } } } cout << ans << endl; }