#include <bits/stdc++.h>
using namespace std;
int count1[100005], count2[100005], count3[100005];
bool can_be_palin(int *a, int size) {
  int i, no_of_odd = 0;
  for (i = 0; i < size; i++) no_of_odd += a[i] % 2;
  return (no_of_odd <= 1);
}
bool is_palin(int *a, int l, int r) {
  for (int i = l; i <= (l + r) / 2; i++)
    if (a[i] != a[r + l - i]) return false;
  return true;
}
bool func(int *a, int n, int end, int l, int r) {
  int i;
  fill(count1, count1 + n + 1, 0);
  fill(count2, count2 + n + 1, 0);
  if (end - l + 1 >= r - end) {
    for (i = l; i <= end; i++) count1[a[i]]++;
    for (i = r; i > end; i--) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1);
  } else {
    for (i = l; i <= end; i++) count1[a[i]]++;
    for (i = r; i >= r - end + l; i--) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1) && is_palin(a, end + 1, r - end + l - 1);
  }
}
bool func2(int *a, int n, int end, int l, int r) {
  int i;
  fill(count1, count1 + n + 1, 0);
  fill(count2, count2 + n + 1, 0);
  if (r - end + 1 >= end - l) {
    for (i = r; i >= end; i--) count1[a[i]]++;
    for (i = l; i < end; i++) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1);
  } else {
    for (i = r; i >= end; i--) count1[a[i]]++;
    for (i = l; i <= l + r - end; i++) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1) && is_palin(a, r - end + l + 1, end - 1);
  }
}
int main() {
  int n, i;
  scanf("%d", &n);
  long long ans = 0;
  int a[n];
  for (i = 0; i < n; i++) scanf("%d", &a[i]);
  for (i = 0; i < n / 2 && a[i] == a[n - 1 - i]; i++)
    ;
  if (i == n / 2) {
    printf("%I64d", (long long)n * (n + 1) / 2);
    return 0;
  }
  int l = i, r = n - 1 - i, left_prev = l, right_prev = r, end = (l + r) / 2;
  int prev_pos = -1;
  while (left_prev < end - 1 && end < right_prev - 1) {
    bool flag = true;
    flag = func(a, n, end, l, r);
    int mid1 = (end + left_prev) / 2;
    int mid2 = (end + right_prev) / 2;
    if (flag == false) {
      left_prev = end;
      end = mid2;
    } else {
      right_prev = end;
      prev_pos = end;
      end = mid1;
    }
  }
  for (int k = left_prev; k < r && k < right_prev; k++)
    if (func(a, n, k, l, r) == true)
      prev_pos = (prev_pos == -1) ? k : min(prev_pos, k);
  if (prev_pos != -1) ans += (long long)(l + 1) * (r - prev_pos);
  right_prev = r, left_prev = l, end = (l + r) / 2;
  prev_pos = -1;
  while (end < right_prev - 1 && left_prev < end - 1) {
    bool flag = true;
    flag = func2(a, n, end, l, r);
    int mid1 = (end + right_prev) / 2;
    int mid2 = (end + left_prev) / 2;
    if (flag == false) {
      right_prev = end;
      end = mid2;
    } else {
      left_prev = end;
      prev_pos = end;
      end = mid1;
    }
  }
  for (int k = right_prev; k > l && k > left_prev; k--)
    if (func2(a, n, k, l, r) == true)
      prev_pos = (prev_pos == -1) ? k : max(prev_pos, k);
  if (prev_pos != -1) ans += (long long)(n - r) * (prev_pos - l);
  fill(count1, count1 + n + 1, 0);
  for (i = l; i <= r; i++) count1[a[i]]++;
  if (can_be_palin(count1, n + 1) == true) ans += (long long)(l + 1) * (n - r);
  printf("%I64d", ans);
}

### Prompt

Please create a solution in Cpp to the following problem:

Misha has an array of n integers indexed by integers from 1 to n. Let's define palindrome degree of array a as the number of such index pairs (l, r)(1 ≤ l ≤ r ≤ n), that the elements from the l-th to the r-th one inclusive can be rearranged in such a way that the whole array will be a palindrome. In other words, pair (l, r) should meet the condition that after some rearranging of numbers on positions from l to r, inclusive (it is allowed not to rearrange the numbers at all), for any 1 ≤ i ≤ n following condition holds: a[i] = a[n - i + 1]. 

Your task is to find the palindrome degree of Misha's array.

Input

The first line contains integer n (1 ≤ n ≤ 105).

The second line contains n positive integers a[i] (1 ≤ a[i] ≤ n), separated by spaces — the elements of Misha's array.

Output

In a single line print the answer to the problem.

Examples

Input

3
2 2 2


Output

6


Input

6
3 6 5 3 3 5


Output

0


Input

5
5 5 2 5 2


Output

4

Note

In the first sample test any possible pair (l, r) meets the condition.

In the third sample test following pairs (1, 3), (1, 4), (1, 5), (2, 5) meet the condition.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int count1[100005], count2[100005], count3[100005];
bool can_be_palin(int *a, int size) {
  int i, no_of_odd = 0;
  for (i = 0; i < size; i++) no_of_odd += a[i] % 2;
  return (no_of_odd <= 1);
}
bool is_palin(int *a, int l, int r) {
  for (int i = l; i <= (l + r) / 2; i++)
    if (a[i] != a[r + l - i]) return false;
  return true;
}
bool func(int *a, int n, int end, int l, int r) {
  int i;
  fill(count1, count1 + n + 1, 0);
  fill(count2, count2 + n + 1, 0);
  if (end - l + 1 >= r - end) {
    for (i = l; i <= end; i++) count1[a[i]]++;
    for (i = r; i > end; i--) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1);
  } else {
    for (i = l; i <= end; i++) count1[a[i]]++;
    for (i = r; i >= r - end + l; i--) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1) && is_palin(a, end + 1, r - end + l - 1);
  }
}
bool func2(int *a, int n, int end, int l, int r) {
  int i;
  fill(count1, count1 + n + 1, 0);
  fill(count2, count2 + n + 1, 0);
  if (r - end + 1 >= end - l) {
    for (i = r; i >= end; i--) count1[a[i]]++;
    for (i = l; i < end; i++) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1);
  } else {
    for (i = r; i >= end; i--) count1[a[i]]++;
    for (i = l; i <= l + r - end; i++) count2[a[i]]++;
    for (i = 1; i <= n; i++) {
      if (count2[i] > count1[i]) return false;
      count1[i] -= count2[i];
    }
    return can_be_palin(count1, n + 1) && is_palin(a, r - end + l + 1, end - 1);
  }
}
int main() {
  int n, i;
  scanf("%d", &n);
  long long ans = 0;
  int a[n];
  for (i = 0; i < n; i++) scanf("%d", &a[i]);
  for (i = 0; i < n / 2 && a[i] == a[n - 1 - i]; i++)
    ;
  if (i == n / 2) {
    printf("%I64d", (long long)n * (n + 1) / 2);
    return 0;
  }
  int l = i, r = n - 1 - i, left_prev = l, right_prev = r, end = (l + r) / 2;
  int prev_pos = -1;
  while (left_prev < end - 1 && end < right_prev - 1) {
    bool flag = true;
    flag = func(a, n, end, l, r);
    int mid1 = (end + left_prev) / 2;
    int mid2 = (end + right_prev) / 2;
    if (flag == false) {
      left_prev = end;
      end = mid2;
    } else {
      right_prev = end;
      prev_pos = end;
      end = mid1;
    }
  }
  for (int k = left_prev; k < r && k < right_prev; k++)
    if (func(a, n, k, l, r) == true)
      prev_pos = (prev_pos == -1) ? k : min(prev_pos, k);
  if (prev_pos != -1) ans += (long long)(l + 1) * (r - prev_pos);
  right_prev = r, left_prev = l, end = (l + r) / 2;
  prev_pos = -1;
  while (end < right_prev - 1 && left_prev < end - 1) {
    bool flag = true;
    flag = func2(a, n, end, l, r);
    int mid1 = (end + right_prev) / 2;
    int mid2 = (end + left_prev) / 2;
    if (flag == false) {
      right_prev = end;
      end = mid2;
    } else {
      left_prev = end;
      prev_pos = end;
      end = mid1;
    }
  }
  for (int k = right_prev; k > l && k > left_prev; k--)
    if (func2(a, n, k, l, r) == true)
      prev_pos = (prev_pos == -1) ? k : max(prev_pos, k);
  if (prev_pos != -1) ans += (long long)(n - r) * (prev_pos - l);
  fill(count1, count1 + n + 1, 0);
  for (i = l; i <= r; i++) count1[a[i]]++;
  if (can_be_palin(count1, n + 1) == true) ans += (long long)(l + 1) * (n - r);
  printf("%I64d", ans);
}

```