import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } void bAdd(T)(T[] bit, int pos, T val) in { assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold"); } do { for (int x = pos; x < bit.length; x |= x + 1) { bit[x] += val; } } // sum of [0, pos) T bSum(T)(T[] bit, int pos) in { assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold"); } do { T ret = 0; for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) { ret += bit[x]; } return ret; } bool isKadomatsu(int a, int b, int c) { return (((a > b && b < c) || (a < b && b > c)) && a != c); } bool isWeakKadomatsu(int a, int b, int c) { return ((a > b && b < c) || (a < b && b > c)); } void main() { try { for (; ; ) { const N = readInt(); auto A = new int[N]; foreach (i; 0 .. N) { A[i] = readInt() - 1; } if (A[0] > A[1]) { foreach (i; 0 .. N) { A[i] = N - 1 - A[i]; } } bool check(int i, int j) { const ai = A[i], aj = A[j]; bool ret = true; A[i] = aj; foreach (k; i - 2 .. i + 1) if (0 <= k && k + 3 <= N) ret = ret && isWeakKadomatsu(A[k], A[k + 1], A[k + 2]); A[i] = ai; A[j] = ai; foreach (k; j - 2 .. j + 1) if (0 <= k && k + 3 <= N) ret = ret && isWeakKadomatsu(A[k], A[k + 1], A[k + 2]); A[j] = aj; return ret; } long ans; foreach (s; 0 .. 2) foreach (t; 0 .. 2) { alias Entry = Tuple!(int, "x", int, "y", int, "type"); Entry[] es; for (int i = s; i < N; i += 2) { const y = A[i]; int x; if (s == 0) { x = N; if (i - 1 >= 0) chmin(x, A[i - 1]); if (i + 1 < N) chmin(x, A[i + 1]); assert(x > y); } else { x = -1; if (i - 1 >= 0) chmax(x, A[i - 1]); if (i + 1 < N) chmax(x, A[i + 1]); assert(x < y); } es ~= Entry(x, y, 0); } for (int i = t; i < N; i += 2) { const x = A[i]; int y; if (t == 0) { y = N; if (i - 1 >= 0) chmin(y, A[i - 1]); if (i + 1 < N) chmin(y, A[i + 1]); assert(y > x); } else { y = -1; if (i - 1 >= 0) chmax(y, A[i - 1]); if (i + 1 < N) chmax(y, A[i + 1]); assert(y < x); } es ~= Entry(x, y, 1); } debug { writeln("es = ", es); } if (s == 0) { foreach (ref e; es) { e.x *= -1; } } es.sort!((e, f) => ((e.x != f.x) ? (e.x < f.x) : (e.type > f.type))); debug { writeln("es = ", es); } auto bit = new long[N]; long num; foreach (ref e; es) { if (e.type == 0) { bit.bAdd(e.y, 1); } else { if (t == 0) { num += bit.bSum(e.y); } else { num += bit.bSum(N) - bit.bSum(e.y + 1); } } } ans += num; debug { long brt; foreach (i; 0 .. N) foreach (j; 0 .. N) { if (i % 2 == s && j % 2 == t) { if (check(i, j)) { ++brt; } } } writeln("brt = ", brt); writeln("num = ", num); assert(brt == num); } } foreach (i; 0 .. N) { foreach (j; i - 4 .. i + 4 + 1) { if (0 <= j && j < N) { if (check(i, j)) { --ans; } } } } ans /= 2; debug { writeln("ans = ", ans); } foreach (i; 0 .. N) { foreach (j; i + 1 .. i + 4 + 1) { if (0 <= j && j < N) { swap(A[i], A[j]); bool ok = true; foreach (k; min(i, j) - 2 .. max(i, j) + 1) { if (0 <= k && k + 3 <= N) { ok = ok && isKadomatsu(A[k], A[k + 1], A[k + 2]); } } swap(A[i], A[j]); if (ok) { ++ans; } } } } writeln(ans); } } catch (EOFException e) { } }