#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <iterator>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <chrono>

#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;
typedef int mt;

template<typename T>
constexpr mt infValue = std::is_same<T, int>::value ? 2000100007 : 8600000000000000007ll;
template<typename T>
constexpr mt maxValue = std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;

#define rep(a, b, i) for(register mt i = a; i < b; ++i)
#define repe(a, b, i) for(register mt i = a; i <= b; ++i)
#define repd(a, b, i) for(register mt i = a; i > b; --i)
#define repde(a, b, i) for(register mt i = a; i >= b; --i)

const mt INF = infValue<mt>;
const long long MOD = 1000000007ll;
const double EPS = 1e-6;
const mt MAX = maxValue<mt>;

#define N 100101
#define K 64
#define P 18
#define M (2000111)

typedef pair<mt, mt> pr;
mt n, m;
char s[N];
set<pr> s0;
vector<mt> p[26];
mt dp[2][N];
multiset<mt> ms;
int main(void)
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &m);
	scanf("%s", s);
	while (s[n]) p[s[n] - 'a'].push_back(n), ++n;
	if(n > m) s0.insert({0, n - 1});
	rep(0, 26, i) {
		if (s0.empty()) break;
		if (p[i].empty()) continue;

		rep(0, p[i].size(), j) {
			mt l = 0, r = 0;
			auto sp = s0.upper_bound({ p[i][j], INF });
			if (sp == s0.begin()) continue;
			--sp;

			l = sp->first, r = p[i][j] - 1;
			if (r - l + 1 >= m) s0.insert({ l, r });

			l = p[i][j] + 1, r = sp->second;
			if (r - l + 1 >= m) s0.insert({ l, r });

			s0.erase(sp);
		}

		if (s0.empty()) {
			mt cnt = p[i].size();
			if (m > 1) {
				rep(0, n, j) {
					dp[0][j] = (j > 0 ? *ms.begin() : 0);
					dp[1][j] = INF;//(j - m >= 0 ? dp[0][j - m]: 0) + 1;
					if (s[j] < i + 'a') dp[1][j] = min(dp[1][j], (j - m >= 0 ? dp[1][j - m] : 0));
					if (s[j] == i + 'a') dp[1][j] = min(dp[1][j], (j - m >= 0 ? dp[0][j - m] : 0) + 1);
					dp[0][j] = min(dp[0][j], dp[1][j]);

					ms.insert(dp[1][j]);
					if (j - m + 1 >= 0) ms.erase(ms.lower_bound(dp[1][j - m + 1]));
				}
				cnt = dp[0][n - 1];
			}

			while (cnt--) putc('a' + i, stdout);
			break;
		}
		else {
			mt cnt = p[i].size();
			while (cnt--) putc('a' + i, stdout);
		}
	}
	putc('\n', stdout);

	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <iterator>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <chrono>

#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;
typedef int mt;

template<typename T>
constexpr mt infValue = std::is_same<T, int>::value ? 2000100007 : 8600000000000000007ll;
template<typename T>
constexpr mt maxValue = std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;

#define rep(a, b, i) for(register mt i = a; i < b; ++i)
#define repe(a, b, i) for(register mt i = a; i <= b; ++i)
#define repd(a, b, i) for(register mt i = a; i > b; --i)
#define repde(a, b, i) for(register mt i = a; i >= b; --i)

const mt INF = infValue<mt>;
const long long MOD = 1000000007ll;
const double EPS = 1e-6;
const mt MAX = maxValue<mt>;

#define N 100101
#define K 64
#define P 18
#define M (2000111)

mt n, m, cnt;
char s[N];
vector<mt> p[26];

mt dp[2][N];

bool f(mt i) {
	bool ok = true;
	mt l = -1;
	rep(0, n, j) {
		if (s[j] <= 'a' + i) l = j;
		ok = ok && (j - l + 1 <= m);
	}
	return ok;
}
mt bs() {//ff up
	mt l = -1, r = 25;
	while (r - l > 1) {
		mt md = l + r >> 1;
		if (f(md)) r = md;
		else l = md;
	}
	return r;
}

multiset<mt> bv;
int main(void)
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &m);
	scanf("%s", s);
	while (s[n]) p[s[n] - 'a'].push_back(n), ++n;
	if (m > 1) {
		f(0);

		mt k = bs();
		rep(0, k, j) {
			mt cnt = p[j].size();
			while (cnt--) putc('a' + j, stdout);
		}

		dp[0][0] = 0;
		if (s[0] < 'a' + k) {
			dp[1][0] = 0;
		}
		else if (s[0] == 'a' + k) {
			dp[1][0] = 1;
		}
		else {
			dp[1][0] = INF;
		}
		bv.insert(dp[1][0]);
		//cnt = p[k].size();

		rep(1, n, i) {
			mt j = max(i - m + 1, 0), mm = *bv.begin();//INF;
			/*rep(j, i, di)
				mm = min(mm, dp[1][di]);*/
			mt mmm = i - m < 0 ? 0 : min(mm, dp[1][i - m]);

			if (s[i] < 'a' + k) {
				dp[0][i] = dp[1][i] = mmm;
			}
			else if (s[i] == 'a' + k) {
				dp[0][i] = mmm;
				dp[1][i] = mmm + 1;
			}
			else {
				dp[0][i] = mm;
				dp[1][i] = INF;
			}

			if (i - m + 1 >= 0) bv.erase(bv.lower_bound(dp[1][i - m + 1]));
			bv.insert(dp[1][i]);
		}
		cnt = dp[0][n - 1];
		while (cnt--) putc('a' + k, stdout);
		putc('\n', stdout);
	}
	else {
		sort(s, s + n);
		puts(s);
	}

	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <iterator>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <chrono>

#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;
typedef int mt;

template<typename T>
constexpr mt infValue = std::is_same<T, int>::value ? 2000100007 : 8600000000000000007ll;
template<typename T>
constexpr mt maxValue = std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;

#define rep(a, b, i) for(register mt i = a; i < b; ++i)
#define repe(a, b, i) for(register mt i = a; i <= b; ++i)
#define repd(a, b, i) for(register mt i = a; i > b; --i)
#define repde(a, b, i) for(register mt i = a; i >= b; --i)

const mt INF = infValue<mt>;
const long long MOD = 1000000007ll;
const double EPS = 1e-6;
const mt MAX = maxValue<mt>;

#define N 100101
#define K 64
#define P 18
#define M (2000111)

mt n, m, cnt;
char s[N];
vector<mt> p[26];

mt dp[2][N];

bool f(mt i) {
	bool ok = true;
	mt l = 0;
	rep(0, n, j) {
		if (s[j] <= 'a' + i) l = j;
		ok = ok && (j - l + 1 <= m);
	}
	return ok;
}
mt bs() {//ff up
	mt l = -1, r = 25;
	while (r - l > 1) {
		mt md = l + r >> 1;
		if (f(md)) r = md;
		else l = md;
	}
	return r;
}

int main(void)
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &m);
	scanf("%s", s);
	while (s[n]) p[s[n] - 'a'].push_back(n), ++n;
	mt k = bs();
	rep(0, k, j) {
		mt cnt = p[j].size();
		while (cnt--) putc('a' + j, stdout);
	}

	//mt l = 0, ls = -1;
	////s[n++] = 'z' + 1;
	//rep(0, n, i) {
	//	if (s[i] < 'a' + k) {
	//		//if (ls != l && i - l <= m) ++cnt;
	//		l = i;
	//	}
	//	else if (s[i] == 'a' + k) {
	//		ls = i;
	//		//if (i - l <= m && ls != l) ++cnt, ls = i;
	//		//else if(i - l >= m) l = ls;
	//	}
	//}
	dp[0][0] = 0;
	if (s[0] < 'a' + k) {
		dp[1][0] = 0;
	}
	else if (s[0] == 'a' + k) {
		dp[1][0] = 1;
	}
	else {
		dp[1][0] = INF;
	}

	rep(1, n, i) {
		mt j = max(i - m + 1, 0), mm = INF;
		rep(j, i, di)
			mm = min(mm, dp[1][di]);

		mt mmm = i - m < 0 ? 0 : min(mm, dp[1][i - m]);

		if (s[i] < 'a' + k) {
			dp[0][i] = dp[1][i] = mmm;
		}
		else if (s[i] == 'a' + k) {
			dp[0][i] = mmm;
			dp[1][i] = mmm + 1;
		}
		else {
			dp[0][i] = mm;
			dp[1][i] = INF;
		}
	}
	cnt = dp[0][n - 1];
	while (cnt--) putc('a' + k, stdout);
	putc('\n', stdout);

	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <iterator>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <chrono>

#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;
typedef int mt;

template<typename T>
constexpr mt infValue = std::is_same<T, int>::value ? 2000100007 : 8600000000000000007ll;
template<typename T>
constexpr mt maxValue = std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;

#define rep(a, b, i) for(register mt i = a; i < b; ++i)
#define repe(a, b, i) for(register mt i = a; i <= b; ++i)
#define repd(a, b, i) for(register mt i = a; i > b; --i)
#define repde(a, b, i) for(register mt i = a; i >= b; --i)

const mt INF = infValue<mt>;
const long long MOD = 1000000007ll;
const double EPS = 1e-6;
const mt MAX = maxValue<mt>;

#define N 100101
#define K 64
#define P 18
#define M (2000111)

mt n, m;
char s[N];
vector<mt> p[26];
mt dp[2][N];
multiset<mt> ms;

bool f(mt i) {
	bool ok = true;
	mt l = 0;
	rep(0, n, j) {
		if (s[j] <= 'a' + i) l = j;
		ok = ok && (j - l + 1 <= m);
	}
	return ok;
}
mt bs() {//ff up
	mt l = -1, r = 25;
	while (r - l > 1) {
		mt md = l + r >> 1;
		if (f(md)) r = md;
		else l = md;
	}
	return r;
}

int main(void)
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &m);
	scanf("%s", s);
	while (s[n]) p[s[n] - 'a'].push_back(n), ++n;
	f(1);

	//rep(0, 26, i) {
	{
		mt i = bs();
		rep(0, i, j) {
			mt cnt = p[j].size();
			while (cnt--) putc('a' + j, stdout);
		}
		mt cnt = p[i].size();
		if (m > 1) {
			rep(0, n, j) {
				dp[0][j] = (j >= m ? *ms.begin() : 0);
				dp[1][j] = INF;//(j - m >= 0 ? dp[0][j - m]: 0) + 1;
				if (s[j] < i + 'a') dp[1][j] = min(dp[1][j], (j - m >= 0 ? dp[1][j - m] : 0));
				if (s[j] == i + 'a') dp[1][j] = min(dp[1][j], (j - m >= 0 ? dp[0][j - m] : 0) + 1);
				dp[0][j] = min(dp[0][j], dp[1][j]);

				ms.insert(dp[1][j]);
				if (j - m + 1 >= 0) ms.erase(ms.lower_bound(dp[1][j - m + 1]));
			}
			cnt = dp[0][n - 1];
		}

		while (cnt--) putc('a' + i, stdout);
	}
	putc('\n', stdout);

	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <iterator>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <chrono>

#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;
typedef int mt;

template<typename T>
constexpr mt infValue = std::is_same<T, int>::value ? 2000100007 : 8600000000000000007ll;
template<typename T>
constexpr mt maxValue = std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;

#define rep(a, b, i) for(register mt i = a; i < b; ++i)
#define repe(a, b, i) for(register mt i = a; i <= b; ++i)
#define repd(a, b, i) for(register mt i = a; i > b; --i)
#define repde(a, b, i) for(register mt i = a; i >= b; --i)

const mt INF = infValue<mt>;
const long long MOD = 1000000007ll;
const double EPS = 1e-6;
const mt MAX = maxValue<mt>;

#define N 100101
#define K 64
#define P 18
#define M (2000111)

typedef pair<mt, mt> pr;
mt n, m;
char s[N];
set<pr> s0;
vector<mt> p[26];
mt dp[2][N];
multiset<mt> ms;
int main(void)
{
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
#endif
	scanf("%d", &m);
	scanf("%s", s);
	while (s[n]) p[s[n] - 'a'].push_back(n), ++n;
	if(n > m) s0.insert({0, n - 1});
	rep(0, 26, i) {
		if (s0.empty()) break;
		if (p[i].empty()) continue;

		rep(0, p[i].size(), j) {
			mt l = 0, r = 0;
			auto sp = s0.upper_bound({ p[i][j], INF });
			if (sp == s0.begin()) continue;
			--sp;

			l = sp->first, r = p[i][j] - 1;
			if (r - l + 1 >= m) s0.insert({ l, r });

			l = p[i][j] + 1, r = sp->second;
			if (r - l + 1 >= m) s0.insert({ l, r });

			s0.erase(sp);
		}

		if (s0.empty()) {
			mt cnt = p[i].size();
			if (m > 1) {
				rep(0, n, j) {
					dp[0][j] = (j >= m ? *ms.begin() : 0);
					dp[1][j] = INF;//(j - m >= 0 ? dp[0][j - m]: 0) + 1;
					if (s[j] < i + 'a') dp[1][j] = min(dp[1][j], (j - m >= 0 ? dp[1][j - m] : 0));
					if (s[j] == i + 'a') dp[1][j] = min(dp[1][j], (j - m >= 0 ? dp[0][j - m] : 0) + 1);
					dp[0][j] = min(dp[0][j], dp[1][j]);

					ms.insert(dp[1][j]);
					if (j - m + 1 >= 0) ms.erase(ms.lower_bound(dp[1][j - m + 1]));
				}
				cnt = dp[0][n - 1];
			}

			while (cnt--) putc('a' + i, stdout);
			break;
		}
		else {
			mt cnt = p[i].size();
			while (cnt--) putc('a' + i, stdout);
		}
	}
	putc('\n', stdout);

	return 0;
}


#include <bits/stdc++.h>

#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

int main () {
  ios::sync_with_stdio(false); cin.tie(0);
  int m;
  cin >> m;
  string s;
  cin >> s;
  vector <deque <int>> arr(26);
  for (int i = 0; i < m; i++) {
    arr[s[i] - 'a'].pb(i);
  }
  vi ans_i;
  auto add = [&] () {
    for (auto& row: arr) {
      if (row.empty()) continue;
      ans_i.pb(row.back());
      return;
    }
    return;
  };
  add();
  for (int i = m; i < sz(s); i++) {
    arr[s[i - m] - 'a'].pop_front();
    arr[s[i] - 'a'].pb(i);
    add();
  }
  sort(all(ans_i));
  ans_i.erase(unique(all(ans_i)), end(ans_i));
  string ans_s = "";
  for (int i: ans_i) ans_s += s[i];
  sort(all(ans_s));
  cout << ans_s << '\n';
  return (0);
}


#include <bits/stdc++.h>

#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

int main () {
  ios::sync_with_stdio(false); cin.tie(0);
  int m;
  cin >> m;
  string s;
  cin >> s;
  vector <deque <int>> arr(26);
  for (int i = 0; i < m; i++) {
    arr[s[i] - 'a'].pb(i);
  }
  vi ans_i;
  char ch_mx = 'a';
  auto add = [&] () {
    for (auto& row: arr) {
      if (row.empty()) continue;
      ans_i.pb(row.back());
      ch_mx = max(ch_mx, s[row.back()]);
      return;
    }
    return;
  };
  add();
  for (int i = m; i < sz(s); i++) {
    arr[s[i - m] - 'a'].pop_front();
    arr[s[i] - 'a'].pb(i);
    add();
  }
  for (int i = 0; i < sz(s); i++) {
    if (s[i] < ch_mx) ans_i.pb(i);
  }
  sort(all(ans_i));
  ans_i.erase(unique(all(ans_i)), end(ans_i));
  string ans_s = "";
  for (int i = 0; i < sz(ans_i); i++) {
    if (s[ans_i[i]] != ch_mx) {
      ans_s += s[ans_i[i]];
    } else {
      int p = (0 <= i - 1) ? ans_i[i - 1] : -1;
      int n = (i + 1 < sz(ans_i)) ? ans_i[i + 1] : sz(s);
      if (n - p <= m) continue;
      ans_s += s[ans_i[i]];
    }
  }
  sort(all(ans_s));
  cout << ans_s << '\n';
  return (0);
}


// aa > a ?

#include <bits/stdc++.h>

#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

int main () {
  ios::sync_with_stdio(false); cin.tie(0);
  int m;
  cin >> m;
  string s;
  cin >> s;
  auto get = [&] (char ch) {
    int need = 0;
    int last = -1;
    int choice = -1;
    for (int i = 0; i < sz(s); i++) {
      if (s[i] < ch) last = choice = i;
      if (s[i] == ch) choice = i;
      if (i - last == m) {
        if (last >= choice) {
          return -1;
        }
        need++;
        last = choice;
      }
    }
    return need;
  };
  for (char ch = 'a'; ch <= 'z'; ch++) {
    int need = get(ch);
    if (need == -1) continue;
    for (int i = 0; i < sz(s); i++) {
      if (s[i] < ch) cout << s[i];
    }
    for (int i = 0; i < need; i++) cout << ch;
    cout << '\n';
    break;
  }
  return (0);
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      if (broken.size() > 6) {
        System.out.println(0);
        return;
      }

      if (a.length == 2) {
        if (a[0] == a[1]) {
          System.out.println(0);
        } else {
          System.out.println(1);
        }
        return;
      }

      int replaceCount = 0;

      for (Integer brokenIndex : broken) {
        for (int i = 0; i < a.length; i++) {
          if (broken.contains(i)) continue;
          swap(a, brokenIndex, i);
          if (check(a, i) && broken.stream().allMatch(d -> check(a, d))) {
            replaceCount++;
          }
          swap(a, brokenIndex, i);
        }
      }

      for (int i = 0; i < broken.size(); i++) {
        int b1 = broken.get(i);
        for (int i1 = i + 1; i1 < broken.size(); i1++) {
          int b2 = broken.get(i1);
          swap(a, b1, b2);
          if (broken.stream().allMatch(d -> check(a, d))) {
            replaceCount++;
          }
          swap(a, b1, b2);
        }
      }

      System.out.println(replaceCount);
    }
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    if (i % 2 == 0)
      return  ((i == a.length - 1) || a[i] < a[i + 1]) && (i == 0 || a[i] < a[i - 1]);
    else
      return ((i == a.length - 1) || a[i] > a[i + 1]) && a[i] > a[i - 1];
  }
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      int replaceCount = 0;

      if (broken.size() > 1) {

        for (int i1 = 0; i1 < broken.size(); i1++) {

          for (int j1 = 0; j1 < broken.size(); j1++) {

            int i = broken.get(i1);
            int brokenIndex = broken.get(j1);

            if (i == brokenIndex) {
              continue;
            }

            swap(a, brokenIndex, i);

            if (checkAll(a)/*check(a, brokenIndex) && check(a, brokenIndex - 1) && check(a, i) && check(a, i - 1)*/) {
              replaceCount++;
              swap(a, brokenIndex, i);
              continue;
            }

            swap(a, brokenIndex, i);

            int bMinus = brokenIndex - 1;
            if (i != brokenIndex - 1) {

              if (brokenIndex > 0) {
                swap(a, bMinus, i);

                if (checkAll(a)/*check(a, brokenIndex) && check(a, bMinus) && check(a, i) && check(a, i + 1) && check(a, i + 2) && check(a, bMinus) && check(a, bMinus - 1)*/) {
                  //System.out.println();
                  replaceCount++;
                  swap(a, bMinus, i);
                  continue;
                }

                swap(a, bMinus, i);
              }
            }

            int bPlus = brokenIndex + 1;
            if (i != bPlus) {

              if (brokenIndex < a.length - 1) {

                swap(a, bPlus, i);

                if (checkAll(a)) {
                  replaceCount++;
                  swap(a, bPlus, i);
                  continue;
                }

                swap(a, bPlus, i);
              }
            }

          }
        }

        System.out.println(replaceCount);

        return;
      }

      int brokenIndex = broken.get(0);

      for (int i = 0; i < a.length; i++) {

        if (i == brokenIndex) {
          continue;
        }

        swap(a, brokenIndex, i);

        if (checkAll(a)) {
          replaceCount++;
          swap(a, brokenIndex, i);
          continue;
        }

        swap(a, brokenIndex, i);

        if (i != brokenIndex - 1) {

          if (brokenIndex > 0) {
            swap(a, brokenIndex - 1, i);

            if (checkAll(a)) {
              //System.out.println();
              replaceCount++;
              swap(a, brokenIndex - 1, i);
              continue;
            }

            swap(a, brokenIndex - 1, i);
          }
        }

        if (i != brokenIndex + 1) {

          if (brokenIndex < a.length - 1) {

            swap(a, brokenIndex + 1, i);

            if (checkAll(a)) {
              replaceCount++;
              swap(a, brokenIndex + 1, i);
              continue;
            }

            swap(a, brokenIndex + 1, i);
          }
        }

      }

      System.out.println(replaceCount);

    }

  }

  private static boolean checkAll(int[] a) {
    for (int i = 0; i < a.length; i++) {
      if (!check(a, i)) {
        return false;
      }
    }
    return true;
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    int k = i + 1;

    if (i >= 0 && i + 1 < a.length) {
      if (k % 2 == 0) {
        if (i + 1 < a.length) {
          if (a[i] <= a[i + 1]) {
            return false;
          }
        }
      } else {
        if (a[i] >= a[i + 1]) {
          return false;
        }
      }

    }

    return true;
  }
}


import static java.lang.Math.max;
import static java.lang.Math.min;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      int replaceCount = 0;

      Set<String> replacements = new HashSet<>();

      for (Integer brokenIndex : broken) {

        for (int i = 0; i < a.length; i++) {

          if (i == brokenIndex) {
            continue;
          }

          swap(a, brokenIndex, i);

          if (canReplace(replacements, i, (brokenIndex)) && checkAll(a, i, (brokenIndex))) {
            replaceCount++;
            swap(a, brokenIndex, i);
            continue;
          }

          swap(a, brokenIndex, i);

          if (i != brokenIndex - 1) {

            if (brokenIndex > 0) {
              swap(a, brokenIndex - 1, i);

              if (canReplace(replacements, i, brokenIndex - 1) && checkAll(a, i, brokenIndex - 1)) {
                //System.out.println();
                replaceCount++;
                swap(a, brokenIndex - 1, i);
                continue;
              }

              swap(a, brokenIndex - 1, i);
            }
          }

          if (i != brokenIndex + 1) {

            if (brokenIndex < a.length - 1) {

              swap(a, brokenIndex + 1, i);

              if (canReplace(replacements, i, brokenIndex + 1) && checkAll(a, i, brokenIndex + 1)) {
                replaceCount++;
                swap(a, brokenIndex + 1, i);
                continue;
              }

              swap(a, brokenIndex + 1, i);
            }
          }

        }

      }

      System.out.println(replaceCount);

    //  System.out.println(replacements);
    }

  }

  private static boolean canReplace(Set<String> replacements, int i, int j) {
    return replacements.add(min(i, j) + " " + max(i, j));
  }

  private static boolean checkAll(int[] a, int x, int y) {

    int margin = 10;
    for (int i = x - margin; i <= x + margin; i++) {
      if (!check(a, i)) {
        return false;
      }
    }

    for (int i = y - margin; i <= y + margin; i++) {
      if (!check(a, i)) {
        return false;
      }
    }

    return true;
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    int k = i + 1;

    if (i >= 0 && i + 1 < a.length) {
      if (k % 2 == 0) {
        if (i + 1 < a.length) {
          if (a[i] <= a[i + 1]) {
            return false;
          }
        }
      } else {
        if (a[i] >= a[i + 1]) {
          return false;
        }
      }

    }

    return true;
  }
}


import java.io.FileNotFoundException;
import java.util.*;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        try (
                Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
        ) {
            int n = scanner.nextInt();

            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }

            List<Integer> broken = new ArrayList<>();

            for (int i = 0; i < a.length; i++) {
                if (!check(a, i)) {
                    broken.add(i);
                }
            }

            if (broken.size() > 6) {
                System.out.println(0);
                return;
            }

            int replaceCount = 0;

            Set<String> replacements = new HashSet<>();

            for (Integer brokenIndex : broken) {

                for (int i = 0; i < a.length; i++) {

                    if (i == brokenIndex) {
                        continue;
                    }

                    swap(a, brokenIndex, i);

                    if (canReplace(replacements, i, (brokenIndex)) && checkAll(a, i, (brokenIndex))) {
                        replaceCount++;
                        swap(a, brokenIndex, i);
                        continue;
                    }

                    swap(a, brokenIndex, i);

                    if (i != brokenIndex - 1) {

                        if (brokenIndex > 0) {
                            swap(a, brokenIndex - 1, i);

                            if (canReplace(replacements, i, brokenIndex - 1) && checkAll(a, i, brokenIndex - 1)) {
                                //System.out.println();
                                replaceCount++;
                                swap(a, brokenIndex - 1, i);
                                continue;
                            }

                            swap(a, brokenIndex - 1, i);
                        }
                    }

                    if (i != brokenIndex + 1) {

                        if (brokenIndex < a.length - 1) {

                            swap(a, brokenIndex + 1, i);

                            if (canReplace(replacements, i, brokenIndex + 1) && checkAll(a, i, brokenIndex + 1)) {
                                replaceCount++;
                                swap(a, brokenIndex + 1, i);
                                continue;
                            }

                            swap(a, brokenIndex + 1, i);
                        }
                    }

                }

            }

            for (Integer b1 : broken) {
                for (Integer b2 : broken) {
                    if (b1 == b2) {
                        continue;
                    }

                    if (canReplace(replacements, b1, b2)) {

                        swap(a, b1, b2);

                        boolean allFixed = true;
                        for (Integer b : broken) {
                            allFixed &= check(a, b);
                        }

                        if (allFixed) {
                            replaceCount++;
                        }

                        swap(a, b1, b2);
                    }
                }
            }

            System.out.println(replaceCount);

            //  System.out.println(replacements);
        }

    }

    private static boolean canReplace(Set<String> replacements, int i, int j) {
        return replacements.add(min(i, j) + " " + max(i, j));
    }

    private static boolean checkAll(int[] a, int x, int y) {

        int margin = 10;
        for (int i = x - margin; i <= x + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        for (int i = y - margin; i <= y + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        return true;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    private static boolean check(int[] a, int i) {
        int k = i + 1;

        if (i >= 0 && i + 1 < a.length) {
            if (k % 2 == 0) {
                if (i + 1 < a.length) {
                    if (a[i] <= a[i + 1]) {
                        return false;
                    }
                }
            } else {
                if (a[i] >= a[i + 1]) {
                    return false;
                }
            }

        }

        return true;
    }
}


import static java.lang.Math.max;
import static java.lang.Math.min;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      int replaceCount = 0;

      Set<String> replacements = new HashSet<>();

      for (Integer brokenIndex : broken) {

        for (int i = 0; i < a.length; i++) {

          if (i == brokenIndex) {
            continue;
          }

          swap(a, brokenIndex, i);

          if (canReplace(replacements, i, (brokenIndex)) && checkAll(a)) {
            replaceCount++;
            swap(a, brokenIndex, i);
            continue;
          }

          swap(a, brokenIndex, i);

          if (i != brokenIndex - 1) {

            if (brokenIndex > 0) {
              swap(a, brokenIndex - 1, i);

              if (canReplace(replacements, i, (brokenIndex - 1)) && checkAll(a)) {
                //System.out.println();
                replaceCount++;
                swap(a, brokenIndex - 1, i);
                continue;
              }

              swap(a, brokenIndex - 1, i);
            }
          }

          if (i != brokenIndex + 1) {

            if (brokenIndex < a.length - 1) {

              swap(a, brokenIndex + 1, i);

              if (canReplace(replacements, i, (brokenIndex + 1)) && checkAll(a)) {
                replaceCount++;
                swap(a, brokenIndex + 1, i);
                continue;
              }

              swap(a, brokenIndex + 1, i);
            }
          }

        }

      }

      System.out.println(replaceCount);

     // System.out.println(replacements);
    }

  }

  private static boolean canReplace(Set<String> replacements, int i, int j) {
    return replacements.add(min(i, j) + " " + max(i, j));
  }

  private static boolean checkAll(int[] a) {
    for (int i = 0; i < a.length; i++) {
      if (!check(a, i)) {
        return false;
      }
    }
    return true;
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    int k = i + 1;

    if (i >= 0 && i + 1 < a.length) {
      if (k % 2 == 0) {
        if (i + 1 < a.length) {
          if (a[i] <= a[i + 1]) {
            return false;
          }
        }
      } else {
        if (a[i] >= a[i + 1]) {
          return false;
        }
      }

    }

    return true;
  }
}


import java.io.FileNotFoundException;
import java.util.*;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        try (
                Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
        ) {
            int n = scanner.nextInt();

            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }

            List<Integer> broken = new ArrayList<>();

            for (int i = 0; i < a.length; i++) {
                if (!check(a, i)) {
                    broken.add(i);
                }
            }

            if (broken.size() > 6) {
                System.out.println(0);
                return;
            }

            int replaceCount = 0;

            Set<String> replacements = new HashSet<>();

            for (Integer brokenIndex : broken) {

                for (int i = 0; i < a.length; i++) {

                    if (i == brokenIndex) {
                        continue;
                    }

                    swap(a, brokenIndex, i);

                    if (canReplace(replacements, i, (brokenIndex)) && checkAll(a, i, (brokenIndex))) {
                        replaceCount++;
                        swap(a, brokenIndex, i);
                        continue;
                    }

                    swap(a, brokenIndex, i);

                    if (i != brokenIndex - 1) {

                        if (brokenIndex > 0) {
                            swap(a, brokenIndex - 1, i);

                            if (canReplace(replacements, i, brokenIndex - 1) && checkAll(a, i, brokenIndex - 1)) {
                                //System.out.println();
                                replaceCount++;
                                swap(a, brokenIndex - 1, i);
                                continue;
                            }

                            swap(a, brokenIndex - 1, i);
                        }
                    }

                    if (i != brokenIndex + 1) {

                        if (brokenIndex < a.length - 1) {

                            swap(a, brokenIndex + 1, i);

                            if (canReplace(replacements, i, brokenIndex + 1) && checkAll(a, i, brokenIndex + 1)) {
                                replaceCount++;
                                swap(a, brokenIndex + 1, i);
                                continue;
                            }

                            swap(a, brokenIndex + 1, i);
                        }
                    }

                }

            }

            System.out.println(replaceCount);

            //  System.out.println(replacements);
        }

    }

    private static boolean canReplace(Set<String> replacements, int i, int j) {
        return replacements.add(min(i, j) + " " + max(i, j));
    }

    private static boolean checkAll(int[] a, int x, int y) {

        int margin = 10;
        for (int i = x - margin; i <= x + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        for (int i = y - margin; i <= y + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        return true;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    private static boolean check(int[] a, int i) {
        int k = i + 1;

        if (i >= 0 && i + 1 < a.length) {
            if (k % 2 == 0) {
                if (i + 1 < a.length) {
                    if (a[i] <= a[i + 1]) {
                        return false;
                    }
                }
            } else {
                if (a[i] >= a[i + 1]) {
                    return false;
                }
            }

        }

        return true;
    }
}


import static java.lang.Math.max;
import static java.lang.Math.min;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      int replaceCount = 0;

      Set<String> replacements = new HashSet<>();

      for (Integer brokenIndex : broken) {

        for (int i = 0; i < a.length; i++) {

          if (i == brokenIndex) {
            continue;
          }

          swap(a, brokenIndex, i);

          if (canReplace(replacements, i, (brokenIndex)) && checkAll(a, i, (brokenIndex))) {
            replaceCount++;
            swap(a, brokenIndex, i);
            continue;
          }

          swap(a, brokenIndex, i);

          if (i != brokenIndex - 1) {

            if (brokenIndex > 0) {
              swap(a, brokenIndex - 1, i);

              if (canReplace(replacements, i, brokenIndex - 1) && checkAll(a, i, brokenIndex - 1)) {
                //System.out.println();
                replaceCount++;
                swap(a, brokenIndex - 1, i);
                continue;
              }

              swap(a, brokenIndex - 1, i);
            }
          }

          if (i != brokenIndex + 1) {

            if (brokenIndex < a.length - 1) {

              swap(a, brokenIndex + 1, i);

              if (canReplace(replacements, i, brokenIndex + 1) && checkAll(a, i, brokenIndex + 1)) {
                replaceCount++;
                swap(a, brokenIndex + 1, i);
                continue;
              }

              swap(a, brokenIndex + 1, i);
            }
          }

        }

      }

      System.out.println(replaceCount);

     // System.out.println(replacements);
    }

  }

  private static boolean canReplace(Set<String> replacements, int i, int j) {
    return replacements.add(min(i, j) + " " + max(i, j));
  }

  private static boolean checkAll(int[] a, int x, int y) {

    for (int i = x - 3; i <= x + 3; i++) {
      if (!check(a, i)) {
        return false;
      }
    }

    for (int i = y - 3; i <= y + 3; i++) {
      if (!check(a, i)) {
        return false;
      }
    }

    return true;
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    int k = i + 1;

    if (i >= 0 && i + 1 < a.length) {
      if (k % 2 == 0) {
        if (i + 1 < a.length) {
          if (a[i] <= a[i + 1]) {
            return false;
          }
        }
      } else {
        if (a[i] >= a[i + 1]) {
          return false;
        }
      }

    }

    return true;
  }
}


import java.io.FileNotFoundException;
import java.util.*;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        try (
                Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
        ) {
            int n = scanner.nextInt();

            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = scanner.nextInt();
            }

            List<Integer> broken = new ArrayList<>();

            for (int i = 0; i < a.length; i++) {
                if (!check(a, i)) {
                    broken.add(i);
                }
            }

            if (broken.size() > 6) {
                System.out.println(0);
                return;
            }

            int replaceCount = 0;

            Set<String> replacements = new HashSet<>();

            for (Integer brokenIndex : broken) {

                for (int i = 0; i < a.length; i++) {

                    if (i == brokenIndex) {
                        continue;
                    }

                    swap(a, brokenIndex, i);

                    if (canReplace(replacements, i, (brokenIndex)) && checkAll(a, i, (brokenIndex))) {
                        replaceCount++;
                        swap(a, brokenIndex, i);
                        continue;
                    }

                    swap(a, brokenIndex, i);

                    if (i != brokenIndex - 1) {

                        if (brokenIndex > 0) {
                            swap(a, brokenIndex - 1, i);

                            if (canReplace(replacements, i, brokenIndex - 1) && checkAll(a, i, brokenIndex - 1)) {
                                //System.out.println();
                                replaceCount++;
                                swap(a, brokenIndex - 1, i);
                                continue;
                            }

                            swap(a, brokenIndex - 1, i);
                        }
                    }

                    if (i != brokenIndex + 1) {

                        if (brokenIndex < a.length - 1) {

                            swap(a, brokenIndex + 1, i);

                            if (canReplace(replacements, i, brokenIndex + 1) && checkAll(a, i, brokenIndex + 1)) {
                                replaceCount++;
                                swap(a, brokenIndex + 1, i);
                                continue;
                            }

                            swap(a, brokenIndex + 1, i);
                        }
                    }

                }

            }

            for (Integer b1 : broken) {
                for (Integer b2 : broken) {
                    if (b1 == b2) {
                        continue;
                    }

                    if (canReplace(replacements, b1, b2)) {

                        swap(a, b1, b2);

                        boolean allFixed = true;
                        for (Integer b : broken) {
                            allFixed &= checkAll(a, b);
                        }

                        if (allFixed) {
                            replaceCount++;
                        }

                        swap(a, b1, b2);
                    }
                }
            }

            System.out.println(replaceCount);

            //  System.out.println(replacements);
        }

    }

    private static boolean canReplace(Set<String> replacements, int i, int j) {
        return replacements.add(min(i, j) + " " + max(i, j));
    }

    private static boolean checkAll(int[] a, int x) {

        int margin = 10;
        for (int i = x - margin; i <= x + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        return true;
    }

    private static boolean checkAll(int[] a, int x, int y) {

        int margin = 10;
        for (int i = x - margin; i <= x + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        for (int i = y - margin; i <= y + margin; i++) {
            if (!check(a, i)) {
                return false;
            }
        }

        return true;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    private static boolean check(int[] a, int i) {
        int k = i + 1;

        if (i >= 0 && i + 1 < a.length) {
            if (k % 2 == 0) {
                if (i + 1 < a.length) {
                    if (a[i] <= a[i + 1]) {
                        return false;
                    }
                }
            } else {
                if (a[i] >= a[i + 1]) {
                    return false;
                }
            }

        }

        return true;
    }
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      if (broken.size() > 6) {
        System.out.println(0);
        return;
      }

      if (a.length == 2) {
        if (a[0] == a[1]) {
          System.out.println(0);
        } else {
          System.out.println(1);
        }
        return;
      }

      int replaceCount = 0;

      for (Integer brokenIndex : broken) {
        for (int i = 0; i < a.length; i++) {
          if (broken.contains(i)) continue;
          swap(a, brokenIndex, i);
          if (broken.stream().allMatch(d -> check(a, d))) {
            replaceCount++;
          }
          swap(a, brokenIndex, i);
        }
      }

      for (int i = 0; i < broken.size(); i++) {
        int b1 = broken.get(i);
        for (int i1 = i + 1; i1 < broken.size(); i1++) {
          int b2 = broken.get(i1);
          swap(a, b1, b2);
          if (broken.stream().allMatch(d -> check(a, d))) {
            replaceCount++;
          }
          swap(a, b1, b2);
        }
      }

      System.out.println(replaceCount);
    }
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    if (i % 2 == 0)
      return  ((i == a.length - 1) || a[i] < a[i + 1]) && (i == 0 || a[i] < a[i - 1]);
    else
      return ((i == a.length - 1) || a[i] > a[i + 1]) && a[i] > a[i - 1];
  }
}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      if (broken.size() > 6) {
        System.out.println(0);
        return;
      }

      if (a.length == 2) {
        if (a[0] == a[1]) {
          System.out.println(0);
        } else {
          System.out.println(1);
        }
        return;
      }

      int replaceCount = 0;

      for (Integer brokenIndex : broken) {
        for (int i = 0; i < a.length; i++) {
          if (i == brokenIndex) continue;
          if (broken.contains(brokenIndex) && broken.contains(i)) continue;
          swap(a, brokenIndex, i);
          if (broken.stream().allMatch(d -> check(a, d))) replaceCount++;
          swap(a, brokenIndex, i);
        }
      }

      for (int b1 : broken) {
        for (int b2 : broken) {
          if (b1 == b2) continue;
          swap(a, b1, b2);
          if (broken.stream().allMatch(d -> check(a, d))) replaceCount++;
          swap(a, b1, b2);
        }
      }

      System.out.println(replaceCount);
    }
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    if (i % 2 == 0)
      return  ((i == a.length - 1) || a[i] < a[i + 1]) && (i == 0 || a[i] < a[i - 1]);
    else
      return ((i == a.length - 1) || a[i] > a[i + 1]) && a[i] > a[i - 1];
  }
}


import static java.lang.Math.max;
import static java.lang.Math.min;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main2 {

  public static void main(String[] args) throws FileNotFoundException {

    try (
        Scanner scanner = new Scanner(System.in/*new File("input.txt")*/)
    ) {
      int n = scanner.nextInt();

      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = scanner.nextInt();
      }

      List<Integer> broken = new ArrayList<>();

      for (int i = 0; i < a.length; i++) {
        if (!check(a, i)) {
          broken.add(i);
        }
      }

      int replaceCount = 0;

      Set<String> replacements = new HashSet<>();

      for (Integer brokenIndex : broken) {

        for (int i = 0; i < a.length; i++) {

          if (i == brokenIndex) {
            continue;
          }

          swap(a, brokenIndex, i);

          if (canReplace(replacements, i, (brokenIndex)) && checkAll(a, i, (brokenIndex))) {
            replaceCount++;
            swap(a, brokenIndex, i);
            continue;
          }

          swap(a, brokenIndex, i);

          if (i != brokenIndex - 1) {

            if (brokenIndex > 0) {
              swap(a, brokenIndex - 1, i);

              if (canReplace(replacements, i, brokenIndex - 1) && checkAll(a, i, brokenIndex - 1)) {
                //System.out.println();
                replaceCount++;
                swap(a, brokenIndex - 1, i);
                continue;
              }

              swap(a, brokenIndex - 1, i);
            }
          }

          if (i != brokenIndex + 1) {

            if (brokenIndex < a.length - 1) {

              swap(a, brokenIndex + 1, i);

              if (canReplace(replacements, i, brokenIndex + 1) && checkAll(a, i, brokenIndex + 1)) {
                replaceCount++;
                swap(a, brokenIndex + 1, i);
                continue;
              }

              swap(a, brokenIndex + 1, i);
            }
          }

        }

      }

      System.out.println(replaceCount);

      System.out.println(replacements);
    }

  }

  private static boolean canReplace(Set<String> replacements, int i, int j) {
    return replacements.add(min(i, j) + " " + max(i, j));
  }

  private static boolean checkAll(int[] a, int x, int y) {

    int margin = 10;
    for (int i = x - margin; i <= x + margin; i++) {
      if (!check(a, i)) {
        return false;
      }
    }

    for (int i = y - margin; i <= y + margin; i++) {
      if (!check(a, i)) {
        return false;
      }
    }

    return true;
  }

  private static void swap(int[] arr, int i, int j) {
    int tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }

  private static boolean check(int[] a, int i) {
    int k = i + 1;

    if (i >= 0 && i + 1 < a.length) {
      if (k % 2 == 0) {
        if (i + 1 < a.length) {
          if (a[i] <= a[i + 1]) {
            return false;
          }
        }
      } else {
        if (a[i] >= a[i + 1]) {
          return false;
        }
      }

    }

    return true;
  }
}


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class C {

    static void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[n];
        for ( int i = 0; i < n; i ++ ) {
            a[i] = nextInt();
        }
        int[] l = new int[2];
        int[] r = new int[2];
        int cnt = 0;
        int left = -1;
        for ( int i = 0; i < n; i ++ ) {
            if ( bad( a, i ) ) {
                if ( left == -1 ) {
                    left = i;
                    if ( cnt == 2 ) {
                        out.println( 0 );
                        return;
                    }
                }
            } else {
                if ( left != -1 ) {
                    l[cnt] = left;
                    r[cnt ++] = i - 1;
                    left = -1;
                }
            }
        }
        if ( left != -1 ) {
            l[cnt] = left;
            r[cnt ++] = n - 1;
        }
        if ( r[0] - l[0] + 1 > 6 || r[cnt - 1] - l[cnt - 1] + 1 > 6 ) {
            out.println( 0 );
            return;
        }
        int res = 0;
        if ( cnt == 2 ) {
            for ( int i = l[0]; i <= r[0]; i++ ) {
                for ( int j = l[cnt - 1]; j <= r[cnt - 1]; j++ ) {
                    if ( i == j ) continue;
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                    boolean ok = true;
                    for ( int k = 0; k < n; k++ ) {
                        ok &= !bad( a, k );
                    }
                    t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                    if ( ok ) res++;
                }
            }
        } else if ( cnt == 1 ) {
            for ( int i = l[0]; i <= r[0]; i ++ ) {
                for ( int j = 0; j < n; j ++ ) {
                    if ( i == j ) continue;
                    if ( j >= l[0] && j <= r[0] && j > i ) continue;
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                    if ( ok( a, i ) && ok( a, j ) ) {
                        res ++;
                    }
                    t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }
        }
        out.println( res );
    }

    private static boolean ok( int[] a, int p ) {
        boolean ok = true;
        for ( int i = Math.max( 0, p - 10 ); i < a.length && i <= p + 10; i ++ ) {
            ok &= ! bad( a, i );
        }
        return ok;
    }

    private static boolean bad( int[] a, int p ) {
        if ( p % 2 == 0 ) {
            return ( p > 0 && a[p - 1] <= a[p] ) || ( p + 1 < a.length && a[p + 1] <= a[p] );
        } else {
            return ( p > 0 && a[p - 1] >= a[p] ) || ( p + 1 < a.length && a[p + 1] >= a[p] );
        }
    }

    static StreamTokenizer in;
    static PrintWriter out;

    static int nextInt() throws IOException {
        in.nextToken();
        return ( int ) in.nval;
    }

    public static void main( String[] args ) throws IOException {
        in = new StreamTokenizer( new InputStreamReader( System.in ) );
        out = new PrintWriter( System.out );
        solve();
        out.close();
    }
}


import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class C {

    static void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[n];
        for ( int i = 0; i < n; i ++ ) {
            a[i] = nextInt();
        }
        int[] l = new int[2];
        int[] r = new int[2];
        int cnt = 0;
        int left = -1;
        for ( int i = 0; i < n; i ++ ) {
            if ( bad( a, i ) ) {
                if ( left == -1 ) {
                    left = i;
                    if ( cnt == 2 ) {
                        out.println( 0 );
                        return;
                    }
                }
            } else {
                if ( left != -1 ) {
                    l[cnt] = left;
                    r[cnt ++] = i - 1;
                    left = -1;
                }
            }
        }
        if ( left != -1 ) {
            l[cnt] = left;
            r[cnt ++] = n - 1;
        }
        if ( r[0] - l[0] + 1 > 6 || r[cnt - 1] - l[cnt - 1] + 1 > 6 ) {
            out.println( 0 );
            return;
        }
        int res = 0;
        if ( cnt == 2 ) {
            for ( int i = l[0]; i <= r[0]; i++ ) {
                for ( int j = l[cnt - 1]; j <= r[cnt - 1]; j++ ) {
                    if ( i == j ) continue;
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                    boolean ok = true;
                    for ( int k = 0; k < n; k++ ) {
                        ok &= !bad( a, k );
                    }
                    t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                    if ( ok ) res++;
                }
            }
            res /= 2;
        } else if ( cnt == 1 ) {
            for ( int i = l[0]; i <= r[0]; i ++ ) {
                for ( int j = 0; j < n; j ++ ) {
                    if ( i == j ) continue;
                    if ( j >= l[0] && j <= r[0] && j > i ) continue;
                    int t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                    if ( ok( a, i ) && ok( a, j ) ) {
                        res ++;
                    }
                    t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }
        }
        out.println( res );
    }

    private static boolean ok( int[] a, int p ) {
        boolean ok = true;
        for ( int i = Math.max( 0, p - 6 ); i < a.length && i <= p + 6; i ++ ) {
            ok &= ! bad( a, i );
        }
        return ok;
    }

    private static boolean bad( int[] a, int p ) {
        if ( p % 2 == 0 ) {
            return ( p > 0 && a[p - 1] <= a[p] ) || ( p + 1 < a.length && a[p + 1] <= a[p] );
        } else {
            return ( p > 0 && a[p - 1] >= a[p] ) || ( p + 1 < a.length && a[p + 1] >= a[p] );
        }
    }

    static StreamTokenizer in;
    static PrintWriter out;

    static int nextInt() throws IOException {
        in.nextToken();
        return ( int ) in.nval;
    }

    public static void main( String[] args ) throws IOException {
        in = new StreamTokenizer( new InputStreamReader( System.in ) );
        out = new PrintWriter( System.out );
        solve();
        out.close();
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

	public class ABC {

		static int n;
		static int[] a;
		static ArrayList<Integer> list;

		public static void main(String[] args) throws IOException
		{
			Scanner sc = new Scanner(System.in);
			PrintWriter out = new PrintWriter(System.out);
			n = sc.nextInt();
			a = new int[n];
			for(int i = 0; i < n; i++)
				a[i] = sc.nextInt();
			list = new ArrayList<>();
			for(int i = 0; i < n-1; i++)
				if((i % 2 == 0 && a[i] >= a[i+1]) || (i % 2 == 1 && a[i] <= a[i+1]))
					list.add(i);
			if(list.size() > 4)
			{
				System.out.println(0);
				return;
			}
//			out.println(list);
			int res = 0;
			TreeSet<Pair> set = new TreeSet<>();
			ArrayList<Integer> newList = new ArrayList<>();
			for(int i : list)
			{
				newList.add(i);
				newList.add(i+1);
			}
			for(int i : newList)
			{
				int affected = list.contains(i)? 1 : 0;
				if(list.contains(i-1))
					affected++;
				int j;
				for(j = 0; j < n; j++)
				{
					if(i != j && list.contains(j))
						affected++;
					if(i != j-1 && list.contains(j-1))
						affected++;
					if(affected >= list.size())
					{
//						out.println(i + " " + j);
						int tmp = a[i];
						a[i] = a[j];
						a[j] = tmp;
						if(check(i, j) && !set.contains(new Pair(i, j)))
						{
							res++;
//							out.println(i + " " + j);
							set.add(new Pair(i, j));
							set.add(new Pair(j, i));
						}
						tmp = a[i];
						a[i] = a[j];
						a[j] = tmp;
					}
				}
			}
			out.println(res);
			out.flush();

		}

		static boolean valid(int x)
		{
			return x >= 0 && x < n;
		}

		static boolean subCheck(int x)
		{
			if(!valid(x) || !valid(x+1))
				return true;
			if(x % 2 == 0 && a[x] >= a[x+1] || x % 2 == 1 && a[x] <= a[x+1])
				return false;
			return true;
		}

		static boolean check(int x, int y)
		{
			if(!subCheck(x) || !subCheck(x-1) || !subCheck(y) || !subCheck(y-1))
				return false;

			for(int i : list)
				if(i % 2 == 0 && a[i] >= a[i+1] || i % 2 == 1 && a[i] <= a[i+1])
					return false;
			return true;
		}

		static class Pair implements Comparable<Pair>
		{
			int i, j;
			Pair(int x, int y){i = x; j = y;}
			public int compareTo(Pair o)
			{
				if(i != o.i)
					return i - o.i;
				return j - o.j;
			}
		}

		static class Scanner
		{
			StringTokenizer st;
			BufferedReader br;

			public Scanner(InputStream System){	br = new BufferedReader(new InputStreamReader(System));	}
			public String next() throws IOException
			{
				while (st == null || !st.hasMoreTokens())
					st = new StringTokenizer(br.readLine());
				return st.nextToken();
			}
			public String nextLine()throws IOException{return br.readLine();}
			public int nextInt() throws IOException {return Integer.parseInt(next());}
			public double nextDouble() throws IOException {return Double.parseDouble(next());}
			public char nextChar()throws IOException{return next().charAt(0);}
			public Long nextLong()throws IOException{return Long.parseLong(next());}
			public boolean ready() throws IOException{return br.ready();}
			public void waitForInput(){for(long i = 0; i < 3e9; i++);}
		}
	}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeSet;

	public class ABC {

		static int n;
		static int[] a;
		static ArrayList<Integer> list;

		public static void main(String[] args) throws IOException
		{
			Scanner sc = new Scanner(System.in);
			PrintWriter out = new PrintWriter(System.out);
			n = sc.nextInt();
			a = new int[n];
			for(int i = 0; i < n; i++)
				a[i] = sc.nextInt();
			list = new ArrayList<>();
			for(int i = 0; i < n-1; i++)
				if((i % 2 == 0 && a[i] >= a[i+1]) || (i % 2 == 1 && a[i] <= a[i+1]))
					list.add(i);
			if(list.size() > 4)
			{
				System.out.println(0);
				return;
			}
//			out.println(list);
			int res = 0;
			TreeSet<Pair> set = new TreeSet<>();
			ArrayList<Integer> newList = new ArrayList<>();
			for(int i : list)
			{
				newList.add(i);
				newList.add(i+1);
			}
			for(int i : newList)
			{
				int affected = list.contains(i)? 1 : 0;
				if(list.contains(i-1))
					affected++;
				int j;
				for(j = 0; j < n; j++)
				{
					if(i != j && list.contains(j))
						affected++;
					if(i != j-1 && list.contains(j-1))
						affected++;
					if(affected >= list.size())
					{
//						out.println(i + " " + j);
						int tmp = a[i];
						a[i] = a[j];
						a[j] = tmp;
						if(check(i, j) && !set.contains(new Pair(i, j)))
						{
							res++;
//							out.println(i + " " + j);
							set.add(new Pair(i, j));
							set.add(new Pair(j, i));
						}
						tmp = a[i];
						a[i] = a[j];
						a[j] = tmp;
					}
				}
			}
			out.println(res);
			out.flush();

		}

		static boolean valid(int x)
		{
			return x > 0 && x < n;
		}

		static boolean subCheck(int x)
		{
			if(!valid(x) || !valid(x+1))
				return true;
			if(x % 2 == 0 && a[x] >= a[x+1] || x % 2 == 1 && a[x] <= a[x+1])
				return false;
			return true;
		}

		static boolean check(int x, int y)
		{
			if(!subCheck(x) || !subCheck(x-1) || !subCheck(y) || !subCheck(y-1))
				return false;

			for(int i : list)
				if(i % 2 == 0 && a[i] >= a[i+1] || i % 2 == 1 && a[i] <= a[i+1])
					return false;
			return true;
		}

		static class Pair implements Comparable<Pair>
		{
			int i, j;
			Pair(int x, int y){i = x; j = y;}
			public int compareTo(Pair o)
			{
				if(i != o.i)
					return i - o.i;
				return j - o.j;
			}
		}

		static class Scanner
		{
			StringTokenizer st;
			BufferedReader br;

			public Scanner(InputStream System){	br = new BufferedReader(new InputStreamReader(System));	}
			public String next() throws IOException
			{
				while (st == null || !st.hasMoreTokens())
					st = new StringTokenizer(br.readLine());
				return st.nextToken();
			}
			public String nextLine()throws IOException{return br.readLine();}
			public int nextInt() throws IOException {return Integer.parseInt(next());}
			public double nextDouble() throws IOException {return Double.parseDouble(next());}
			public char nextChar()throws IOException{return next().charAt(0);}
			public Long nextLong()throws IOException{return Long.parseLong(next());}
			public boolean ready() throws IOException{return br.ready();}
			public void waitForInput(){for(long i = 0; i < 3e9; i++);}
		}
	}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            int T[] = new int[N];
            for (int i = 0; i < N; i++) {
                T[i] = in.nextInt();
            }

            List<Integer> wrongIds = new ArrayList<>();
            for (int i = 0; i < N; i++) {
                if (wrong(T, i)) wrongIds.add(i);
            }

            if (wrongIds.size() > 6) {
                out.println(0);
                return;
            }
            int ans = 0;
            int ansSym = 0;

            for (Integer wrongId : wrongIds) {
                for (int i = 0; i < N; i++) {
                    swap(T, wrongId, i);
                    List<Integer> impacted = new ArrayList<>(wrongIds);
                    impacted.add(i);
                    if (!wrong(T, impacted)) {
                        if (wrongIds.contains(i)) {
                            ansSym++;
                        } else {
                            ans++;
                        }
                    }
                    swap(T, wrongId, i);
                }
            }
            out.println(ans + ansSym / 2);
        }

        private boolean wrong(int[] t, List<Integer> impacted) {
            for (Integer id : impacted) {
                if (wrong(t, id)) return true;
            }
            return false;
        }

        private boolean wrong(int[] T, int i) {
            int N = T.length;
            boolean wrong = false;
            wrong |= i % 2 == 0 && (i > 0 && T[i - 1] <= T[i]);
            wrong |= i % 2 == 0 && (i < N - 1 && T[i] >= T[i + 1]);
            wrong |= i % 2 != 0 && (i > 0 && T[i - 1] >= T[i]);
            wrong |= i % 2 != 0 && (i < N - 1 && T[i] <= T[i + 1]);
            return wrong;
        }

        private void swap(int[] t, int a, int b) {
            int tmp = t[a];
            t[a] = t[b];
            t[b] = tmp;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Set;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            int T[] = new int[N];
            for (int i = 0; i < N; i++) {
                T[i] = in.nextInt();
            }

            Set<Integer> errors = new TreeSet<>();
            for (int i = 1; i <= N; i++) {
                if (i % 2 != 0) {
                    if (i < N && T[i - 1] >= T[i]) {
                        errors.add(i);
                    }
                    if (i > 1 && T[i - 1] >= T[i - 2]) {
                        errors.add(i);
                    }
                } else if (i % 2 == 0) {
                    if (i < N && T[i - 1] <= T[i]) {
                        errors.add(i);
                    }
                    if (i > 1 && T[i - 1] <= T[i - 2]) {
                        errors.add(i);
                    }
                }
            }

            if (errors.size() > 6) {
                out.println(0);
            } else {
                long ans = 0;
                long ansError = 0;
                for (Integer idx : errors) {
                    for (int i = 1; i <= N; i++) {
                        if (idx == i) continue;

                        swap(T, idx, i);

                        if (good(T, i)) {
                            boolean correct = true;
                            for (Integer er : errors) {
                                if (!good(T, er)) {
                                    correct = false;
                                }
                            }
                            if (correct) {
                                if (errors.contains(i)) {
                                    ansError++;
                                } else {
                                    ans++;
                                }
                            }
                        }
                        swap(T, idx, i);
                    }
                }
                out.println(ans + ansError / 2);
            }
        }

        private boolean good(int[] t, int idx) {
            int N = t.length;
            if (idx % 2 != 0) {
                if (idx > 1 && t[idx - 2] <= t[idx - 1]) return false;
                if (idx < N && t[idx - 1] >= t[idx]) return false;
            } else {
                if (t[idx - 2] >= t[idx - 1]) return false;
                if (idx < N && t[idx - 1] <= t[idx]) return false;
            }
            return true;
        }

        private void swap(int[] t, int a, int b) {
            int tmp = t[a - 1];
            t[a - 1] = t[b - 1];
            t[b - 1] = tmp;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Set;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int N = in.nextInt();
            int T[] = new int[N];
            for (int i = 0; i < N; i++) {
                T[i] = in.nextInt();
            }

            Set<Integer> errors = new TreeSet<>();
            for (int i = 1; i <= N; i++) {
                if (i % 2 != 0) {
                    if (i < N && T[i - 1] >= T[i]) {
                        errors.add(i);
                    }
                    if (i > 1 && T[i - 1] >= T[i - 2]) {
                        errors.add(i);
                    }
                } else if (i % 2 == 0) {
                    if (i < N && T[i - 1] <= T[i]) {
                        errors.add(i);
                    }
                    if (i > 1 && T[i - 1] <= T[i - 2]) {
                        errors.add(i);
                    }
                }
            }

            if (errors.size() > 4) {
                out.println(0);
            } else {
                long ans = 0;
                long ansError = 0;
                for (Integer idx : errors) {
                    for (int i = 1; i <= N; i++) {
                        if (idx == i) continue;

                        swap(T, idx, i);

                        if (good(T, i)) {
                            boolean correct = true;
                            for (Integer er : errors) {
                                if (!good(T, er)) {
                                    correct = false;
                                }
                            }
                            if (correct) {
                                if (errors.contains(i)) {
                                    ansError++;
                                } else {
                                    ans++;
                                }
                            }
                        }
                        swap(T, idx, i);
                    }
                }
                out.println(ans + ansError / 2);
            }
        }

        private boolean good(int[] t, int idx) {
            int N = t.length;
            if (idx % 2 != 0) {
                if (idx > 1 && t[idx - 2] <= t[idx - 1]) return false;
                if (idx < N && t[idx - 1] >= t[idx]) return false;
            } else {
                if (t[idx - 2] >= t[idx - 1]) return false;
                if (idx < N && t[idx - 1] <= t[idx]) return false;
            }
            return true;
        }

        private void swap(int[] t, int a, int b) {
            int tmp = t[a - 1];
            t[a - 1] = t[b - 1];
            t[b - 1] = tmp;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Alex
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskC {
        ArrayList<Integer> bad(int[] a) {
            ArrayList<Integer> res = new ArrayList<>();
            for(int i = 0; i < a.length - 1; i++) {
                if(i % 2 == 0) {
                    if(a[i + 1] <= a[i]) res.add(i);
                } else {
                    if(a[i + 1] >= a[i]) res.add(i);
                }
            }
            return res;
        }

        boolean works(int[] a, int first, int second, ArrayList<Integer> bad) {
            for(int diff = -2; diff <= 2; diff++) {
                int i = first + diff;
                if(i >= 0 && i < a.length - 1) {
                    if(i % 2 == 0) {
                        if(a[i + 1] <= a[i]) return false;
                    } else {
                        if(a[i + 1] >= a[i]) return false;
                    }
                }
                i = second + diff;
                if(i >= 0 && i < a.length - 1) {
                    if(i % 2 == 0) {
                        if(a[i + 1] <= a[i]) return false;
                    } else {
                        if(a[i + 1] >= a[i]) return false;
                    }
                }
                for(int val : bad) {
                    i = val + diff;
                    if(i >= 0 && i < a.length - 1) {
                        if(i % 2 == 0) {
                            if(a[i + 1] <= a[i]) return false;
                        } else {
                            if(a[i + 1] >= a[i]) return false;
                        }
                    }
                }
            }
//        for(int i = 0; i < a.length - 1; i++) {
//            if (i % 2 == 0) {
//                if (a[i + 1] <= a[i]) {
//                    for(int val : a)
//                        System.out.println("VAL " + val);
//                    System.out.println(i + " " + first + " " + second);
//                    return false;
//                }
//            } else {
//                if (a[i + 1] >= a[i]) {
//                    System.out.println(i);
//                    return false;
//                }
//
//            }
//        }
            return true;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int[] a = IOUtils.readIntArray(in, n);
//        solvesilly(a, out);
            int res = 0;
            ArrayList<Integer> bad = bad(a);
//        out.printLine(bad);
            if(bad.size() > 4) {
                out.printLine(0);
                return;
            }
            TreeSet<IntIntPair> considered = new TreeSet<>();

            if(bad.size() > 1) {
                for(int first = 0; first < bad.size(); first++) {
                    for(int second = first + 1; second < bad.size(); second++) {
                        for(int i = Math.max(0, bad.get(first) - 1); i < Math.min(n, bad.get(first) + 2); i++) {
                            for(int j = Math.max(0, bad.get(second) - 1); j < Math.min(n, bad.get(second) + 2); j++) {
                                if(i >= j) continue;
                                IntIntPair consider = new IntIntPair(i, j);
                                if(considered.contains(consider)) continue;
                                considered.add(consider);
                                int temp = a[i];
                                a[i] = a[j];
                                a[j] = temp;

                                if(works(a, i, j, bad)) res++;

                                temp = a[i];
                                a[i] = a[j];
                                a[j] = temp;
                            }
                        }
                    }
                }
            }
            if(bad.size() <= 2) {
                for(int b : bad) {
                    for(int ii = Math.max(0, b - 1); ii < Math.min(n, b + 2); ii++) {
                        for(int jj = 0; jj < n; jj++) {
                            int i = Math.min(ii, jj);
                            int j = Math.max(ii, jj);
                            if(i == j) continue;

                            IntIntPair considering = new IntIntPair(i, j);
                            if(considered.contains(considering)) continue;
                            considered.add(considering);
                            int temp = a[i];
                            a[i] = a[j];
                            a[j] = temp;

                            if(works(a, i, j, bad)) {
//                    out.printLine(i, j);
                                res++;
                            }

                            temp = a[i];
                            a[i] = a[j];
                            a[j] = temp;
                        }
                    }
                }
            }
            out.printLine(res);
//        throw new RuntimeException();
//        out.printLine(bad);
//        for(int i = 0; i < n; i++) {
//            for(int j = i + 1; j < n; j++) {
//                int temp = a[i];
//                a[i] = a[j];
//                a[j] = temp;
//
//                if (works(a)) res++;
//
//                temp = a[i];
//                a[i] = a[j];
//                a[j] = temp;
//            }
//        }
//        out.printLine(res);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if(numChars == -1)
                throw new InputMismatchException();
            if(curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch(IOException e) {
                    throw new InputMismatchException();
                }
                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while(isSpaceChar(c))
                c = read();
            int sgn = 1;
            if(c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if(c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while(!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if(filter != null)
                return filter.isSpaceChar(c);
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class IOUtils {
        public static int[] readIntArray(InputReader in, int size) {
            int[] array = new int[size];
            for(int i = 0; i < size; i++)
                array[i] = in.readInt();
            return array;
        }

    }

    static class IntIntPair implements Comparable<IntIntPair> {
        public final int first;
        public final int second;

        public IntIntPair(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if(this == o) return true;
            if(o == null || getClass() != o.getClass()) return false;

            IntIntPair pair = (IntIntPair) o;

            return first == pair.first && second == pair.second;

        }

        public int hashCode() {
            int result = Integer.hashCode(first);
            result = 31 * result + Integer.hashCode(second);
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings({"unchecked"})
        public int compareTo(IntIntPair o) {
            int value = Integer.compare(first, o.first);
            if(value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.util.ArrayList;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Alex
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }
    static class TaskC {
        ArrayList<Integer> bad(int[] a) {
            ArrayList<Integer> res = new ArrayList<>();
            for(int i = 0; i < a.length - 1; i++) {
                if(i % 2 == 0) {
                    if(a[i + 1] <= a[i]) res.add(i);
                } else {
                    if(a[i + 1] >= a[i]) res.add(i);
                }
            }
            return res;
        }

        boolean works(int[] a) {
            for(int i = 0; i < a.length - 1; i++) {
                if(i % 2 == 0) {
                    if(a[i + 1] <= a[i]) return false;
                } else {
                    if(a[i + 1] >= a[i]) return false;
                }
            }
            return true;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.readInt();
            int[] a = IOUtils.readIntArray(in, n);
//        solvesilly(a, out);
            int res = 0;
            ArrayList<Integer> bad = bad(a);
//        out.printLine(bad);
            if(bad.size() > 4) {
                out.printLine(0);
                return;
            }
            TreeSet<IntIntPair> considered = new TreeSet<>();

            if(bad.size() > 1) {
                for(int first = 0; first < bad.size(); first++) {
                    for(int second = first + 1; second < bad.size(); second++) {
                        for(int i = Math.max(0, bad.get(first) - 1); i < Math.min(n, bad.get(first) + 2); i++) {
                            for(int j = Math.max(0, bad.get(second) - 1); j < Math.min(n, bad.get(second) + 2); j++) {
                                if(i >= j) continue;
                                IntIntPair consider = new IntIntPair(i, j);
                                if(considered.contains(consider)) continue;
                                considered.add(consider);
                                int temp = a[i];
                                a[i] = a[j];
                                a[j] = temp;

                                if(works(a)) res++;

                                temp = a[i];
                                a[i] = a[j];
                                a[j] = temp;
                            }
                        }
                    }
                }
            }
            if(bad.size() <= 2) {
                for(int b : bad) {
                    for(int ii = Math.max(0, b - 1); ii < Math.min(n, b + 2); ii++) {
                        for(int jj = 0; jj < n; jj++) {
                            int i = Math.min(ii, jj);
                            int j = Math.max(ii, jj);
                            if(i == j) continue;

                            IntIntPair considering = new IntIntPair(i, j);
                            if(considered.contains(considering)) continue;
                            considered.add(considering);
                            int temp = a[i];
                            a[i] = a[j];
                            a[j] = temp;

                            if(works(a)) {
//                    out.printLine(i, j);
                                res++;
                            }

                            temp = a[i];
                            a[i] = a[j];
                            a[j] = temp;
                        }
                    }
                }
            }
            out.printLine(res);
//        throw new RuntimeException();
//        out.printLine(bad);
//        for(int i = 0; i < n; i++) {
//            for(int j = i + 1; j < n; j++) {
//                int temp = a[i];
//                a[i] = a[j];
//                a[j] = temp;
//
//                if (works(a)) res++;
//
//                temp = a[i];
//                a[i] = a[j];
//                a[j] = temp;
//            }
//        }
//        out.printLine(res);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if(numChars == -1)
                throw new InputMismatchException();
            if(curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch(IOException e) {
                    throw new InputMismatchException();
                }
                if(numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while(isSpaceChar(c))
                c = read();
            int sgn = 1;
            if(c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if(c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while(!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if(filter != null)
                return filter.isSpaceChar(c);
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void printLine(int i) {
            writer.println(i);
        }

    }

    static class IOUtils {
        public static int[] readIntArray(InputReader in, int size) {
            int[] array = new int[size];
            for(int i = 0; i < size; i++)
                array[i] = in.readInt();
            return array;
        }

    }

    static class IntIntPair implements Comparable<IntIntPair> {
        public final int first;
        public final int second;

        public IntIntPair(int first, int second) {
            this.first = first;
            this.second = second;
        }

        public boolean equals(Object o) {
            if(this == o) return true;
            if(o == null || getClass() != o.getClass()) return false;

            IntIntPair pair = (IntIntPair) o;

            return first == pair.first && second == pair.second;

        }

        public int hashCode() {
            int result = Integer.hashCode(first);
            result = 31 * result + Integer.hashCode(second);
            return result;
        }

        public String toString() {
            return "(" + first + "," + second + ")";
        }

        @SuppressWarnings({"unchecked"})
        public int compareTo(IntIntPair o) {
            int value = Integer.compare(first, o.first);
            if(value != 0) {
                return value;
            }
            return Integer.compare(second, o.second);
        }

    }
}