#include <bits/stdc++.h>
using namespace std;
using ipair = pair<int, int>;
using int64 = int64_t;
using uint64 = uint64_t;
template <typename T>
void remin(T &a, const T &b) {
  if (b < a) {
    a = b;
  }
}
template <typename T>
void remax(T &a, const T &b) {
  if (b > a) {
    a = b;
  }
}
template <class T1, class T2>
inline istream &operator>>(istream &s, pair<T1, T2> &p) {
  return s >> p.first >> p.second;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << " " << p.second << ")";
}
template <class T1>
inline ostream &operator<<(ostream &s, const set<T1> &container) {
  for (const auto &x : container) {
    s << x << ' ';
  }
  return s;
}
template <class T1>
inline ostream &operator<<(ostream &s, const vector<T1> &container) {
  for (const auto &x : container) {
    s << x << ' ';
  }
  return s;
}
template <class T1>
inline istream &operator>>(istream &s, vector<T1> &container) {
  for (auto &x : container) {
    s >> x;
  }
  return s;
}
template <class T>
inline T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <class T>
inline T gcd(T a, T b) {
  while (b > 0) {
    T tmp = a;
    a = a % b;
    b = tmp;
  }
  return a;
}
template <class T>
inline vector<pair<T, int>> factorize(T n) {
  vector<pair<T, int>> R;
  for (T i = 2; n > 1;) {
    if (n % i == 0) {
      int C = 0;
      for (; n % i == 0; C++, n /= i)
        ;
      R.push_back(make_pair(i, C));
    }
    i++;
    if (i > n / i) i = n;
  }
  if (n > 1) R.push_back(make_pair(n, 1));
  return R;
}
template <class T>
inline bool isPrimeNumber(T n) {
  if (n <= 1) return false;
  for (T i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
vector<bool> gen_primes(int limit) {
  vector<bool> is_prime(limit + 1, true);
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i * i <= limit; i += 1) {
    if (is_prime[i]) {
      for (int j = i + i; j <= limit; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return is_prime;
}
template <int64_t P>
struct TZp {
  static_assert(P > 1, "P should be > 1");
  static_assert(numeric_limits<int64_t>::max() / P >= P,
                "P * P should be less then 2^63");
  int64_t Value = 0;
  TZp(int64_t val = 0) : Value(val) {}
  TZp &operator+=(const TZp &other) {
    Value = (Value + other.Value) % P;
    return *this;
  }
  TZp &operator-=(const TZp &other) {
    Value = (Value - other.Value + P) % P;
    return *this;
  }
  TZp &operator*=(const TZp &other) {
    Value = (Value * other.Value) % P;
    return *this;
  }
  TZp Inverse() const {
    int64_t x, y;
    euclide(Value, P, x, y);
    return x;
  }
  TZp &operator/=(const TZp &other) {
    Value = (Value * other.Inverse().Value) % P;
    return *this;
  }
  TZp operator-() const { return TZp((P - Value) % P); }
  TZp operator+(const TZp &other) const {
    TZp result(Value);
    result += other;
    return result;
  }
  TZp operator-(const TZp &other) const {
    TZp result(Value);
    result -= other;
    return result;
  }
  TZp operator*(const TZp &other) const {
    TZp result(Value);
    result *= other;
    return result;
  }
  TZp operator/(const TZp &other) const {
    TZp result(Value);
    result /= other;
    return result;
  }
  int64_t ToInt() const { return Value; }
};
template <class T>
struct TPlus {
  using Type = T;
  const static T Neutral;
  T operator()(const T &x, const T &y) const { return x + y; }
};
template <class T>
const T TPlus<T>::Neutral = 0;
template <class T>
struct TMax {
  using Type = T;
  static const T Neutral;
  T operator()(const T &x, const T &y) const { return max(x, y); }
};
template <class T>
T const TMax<T>::Neutral = numeric_limits<T>::min();
template <class T>
struct TMin {
  using Type = T;
  static const T Neutral;
  T operator()(const T &x, const T &y) const { return min(x, y); }
};
template <class T>
T const TMin<T>::Neutral = numeric_limits<T>::max();
template <class TF>
struct TSegmentTree {
  using T = typename TF::Type;
  size_t N = 0;
  TF Func;
  vector<T> Data;
  TSegmentTree(size_t n, const TF &f = TF()) : N(n), Func(f) {
    size_t s = 1;
    while (s < n) {
      s *= 2;
    }
    Data.assign(s * 2 - 1, TF::Neutral);
  }
  void Set(size_t i, const T &x) {
    size_t index = i + Data.size() / 2;
    Data[index] = x;
    while (index) {
      index = (index - 1) / 2;
      Data[index] = Func(Data[index * 2 + 1], Data[index * 2 + 2]);
    }
  }
  T Get(size_t left, size_t right, size_t l = 0, size_t r = -1,
        size_t v = 0) const {
    if (left >= right) {
      return TF::Neutral;
    }
    if (r == -1) {
      r = Data.size() / 2 + 1;
    }
    if (l == left && r == right) {
      return Data[v];
    }
    size_t m = l + (r - l) / 2;
    while (m < left || m > right) {
      if (m < left) {
        v = v * 2 + 2;
        l = m;
      } else {
        v = v * 2 + 1;
        r = m;
      }
      m = l + (r - l) / 2;
    }
    return Func(Get(left, min(m, right), l, m, v * 2 + 1),
                Get(max(m, left), right, m, r, v * 2 + 2));
  }
  T GetUpTo(size_t right) {
    T result = TF::Neutral;
    if (right == 0) {
      return result;
    }
    size_t l = 0;
    size_t r = Data.size() / 2 + 1;
    size_t v = 0;
    while (r > right) {
      size_t m = l + (r - l) / 2;
      if (m >= right) {
        r = m;
        v = v * 2 + 1;
      } else {
        result = Func(Data.at(v * 2 + 1), result);
        v = v * 2 + 2;
        l = m;
      }
    }
    result = Func(Data.at(v), result);
    return result;
  }
};
struct TDSU {
  vector<size_t> Parents;
  vector<size_t> Sizes;
  TDSU(size_t n) : Parents(n), Sizes(n, 1) {
    iota(Parents.begin(), Parents.end(), 0);
  }
  size_t Get(size_t x) {
    if (Parents[x] != x) {
      Parents[x] = Get(Parents[x]);
    }
    return Parents[x];
  }
  void Unite(size_t x, size_t y) {
    x = Get(x);
    y = Get(y);
    if (x == y) {
      return;
    }
    if (Sizes[x] > Sizes[y]) {
      std::swap(x, y);
    }
    Parents[x] = y;
    Sizes[y] += Sizes[x];
  }
};
template <class T>
struct TDisjointIntervalSet : public set<pair<T, T>> {
  using TParent = set<pair<T, T>>;
  void Insert(T left, T right) {
    if (right <= left) {
      return;
    }
    auto it = TParent::upper_bound(make_pair(right, numeric_limits<T>::max()));
    auto it1 = it;
    while (it != TParent::begin()) {
      --it;
      if (it->second >= left) {
        remin(left, it->first);
        remax(right, it->second);
      } else {
        ++it;
        break;
      }
    }
    TParent::erase(it, it1);
    TParent::emplace(left, right);
  }
  void Remove(T left, T right) {
    if (right <= left) {
      return;
    }
    auto it = TParent::upper_bound(make_pair(right, numeric_limits<T>::max()));
    auto it1 = it;
    T l = left;
    T r = right;
    while (it != TParent::begin()) {
      --it;
      if (it->second >= left) {
        remin(l, it->first);
        remax(r, it->second);
      } else {
        ++it;
        break;
      }
    }
    TParent::erase(it, it1);
    if (l < left) {
      TParent::emplace(l, left);
    }
    if (r > right) {
      TParent::emplace(right, r);
    }
  }
};
void Solve() {}
void RunGCJ() {
  size_t __number_of_cases;
  cin >> __number_of_cases;
  for (size_t __number_of_case = 0; __number_of_case < __number_of_cases;
       ++__number_of_case) {
    cout << "Case #" << __number_of_case + 1 << ": ";
    Solve();
  }
}
unordered_map<int, int> get_counts(const vector<int> &q, int a, int b) {
  unordered_map<int, int> result;
  for (int i = a; i <= b; ++i) {
    ++result[q[i]];
  }
  return result;
}
int main(void) {
  std::ios::sync_with_stdio(false);
  int64_t n;
  cin >> n;
  vector<int> q(n);
  cin >> q;
  int64_t l = 0;
  while (l * 2 < n && q[l] == q[n - 1 - l]) {
    ++l;
  }
  if (l * 2 >= n) {
    cout << n * (n + 1) / 2 << endl;
    return 0;
  }
  int64_t r = n - 1 - l;
  int64_t c = n / 2;
  while (q[c] == q[n - 1 - c]) {
    --c;
  }
  int64_t a = c, b = n - 1 - c;
  if (get_counts(q, l, a) == get_counts(q, b, r)) {
    cout << (l + 1) * (n - a) + (b - l) * (n - r) << endl;
    return 0;
  }
  auto counts = get_counts(q, l, r);
  int odd = 0;
  for (auto x : counts) {
    odd += x.second % 2;
  }
  if (odd > 1) {
    cout << 0 << endl;
    return 0;
  }
  int64_t bestr = r;
  while (true) {
    if ((counts[q[bestr]] -= 2) >= 0) {
      --bestr;
    } else {
      break;
    }
  }
  int64_t bestl = l;
  counts = get_counts(q, l, r);
  while (true) {
    if ((counts[q[bestl]] -= 2) >= 0) {
      ++bestl;
    } else {
      break;
    }
  }
  int64_t result = 0;
  result += (bestl - l) * (n - r);
  result += (r - bestr) * (l + 1);
  result += (l + 1) * (n - r);
  cout << result << endl;
  return 0;
}

### Prompt

Please provide a cpp coded solution to the problem described below:

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;
using ipair = pair<int, int>;
using int64 = int64_t;
using uint64 = uint64_t;
template <typename T>
void remin(T &a, const T &b) {
  if (b < a) {
    a = b;
  }
}
template <typename T>
void remax(T &a, const T &b) {
  if (b > a) {
    a = b;
  }
}
template <class T1, class T2>
inline istream &operator>>(istream &s, pair<T1, T2> &p) {
  return s >> p.first >> p.second;
}
template <class T1, class T2>
inline ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
  return s << "(" << p.first << " " << p.second << ")";
}
template <class T1>
inline ostream &operator<<(ostream &s, const set<T1> &container) {
  for (const auto &x : container) {
    s << x << ' ';
  }
  return s;
}
template <class T1>
inline ostream &operator<<(ostream &s, const vector<T1> &container) {
  for (const auto &x : container) {
    s << x << ' ';
  }
  return s;
}
template <class T1>
inline istream &operator>>(istream &s, vector<T1> &container) {
  for (auto &x : container) {
    s >> x;
  }
  return s;
}
template <class T>
inline T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
template <class T>
inline T gcd(T a, T b) {
  while (b > 0) {
    T tmp = a;
    a = a % b;
    b = tmp;
  }
  return a;
}
template <class T>
inline vector<pair<T, int>> factorize(T n) {
  vector<pair<T, int>> R;
  for (T i = 2; n > 1;) {
    if (n % i == 0) {
      int C = 0;
      for (; n % i == 0; C++, n /= i)
        ;
      R.push_back(make_pair(i, C));
    }
    i++;
    if (i > n / i) i = n;
  }
  if (n > 1) R.push_back(make_pair(n, 1));
  return R;
}
template <class T>
inline bool isPrimeNumber(T n) {
  if (n <= 1) return false;
  for (T i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
vector<bool> gen_primes(int limit) {
  vector<bool> is_prime(limit + 1, true);
  is_prime[0] = false;
  is_prime[1] = false;
  for (int i = 2; i * i <= limit; i += 1) {
    if (is_prime[i]) {
      for (int j = i + i; j <= limit; j += i) {
        is_prime[j] = false;
      }
    }
  }
  return is_prime;
}
template <int64_t P>
struct TZp {
  static_assert(P > 1, "P should be > 1");
  static_assert(numeric_limits<int64_t>::max() / P >= P,
                "P * P should be less then 2^63");
  int64_t Value = 0;
  TZp(int64_t val = 0) : Value(val) {}
  TZp &operator+=(const TZp &other) {
    Value = (Value + other.Value) % P;
    return *this;
  }
  TZp &operator-=(const TZp &other) {
    Value = (Value - other.Value + P) % P;
    return *this;
  }
  TZp &operator*=(const TZp &other) {
    Value = (Value * other.Value) % P;
    return *this;
  }
  TZp Inverse() const {
    int64_t x, y;
    euclide(Value, P, x, y);
    return x;
  }
  TZp &operator/=(const TZp &other) {
    Value = (Value * other.Inverse().Value) % P;
    return *this;
  }
  TZp operator-() const { return TZp((P - Value) % P); }
  TZp operator+(const TZp &other) const {
    TZp result(Value);
    result += other;
    return result;
  }
  TZp operator-(const TZp &other) const {
    TZp result(Value);
    result -= other;
    return result;
  }
  TZp operator*(const TZp &other) const {
    TZp result(Value);
    result *= other;
    return result;
  }
  TZp operator/(const TZp &other) const {
    TZp result(Value);
    result /= other;
    return result;
  }
  int64_t ToInt() const { return Value; }
};
template <class T>
struct TPlus {
  using Type = T;
  const static T Neutral;
  T operator()(const T &x, const T &y) const { return x + y; }
};
template <class T>
const T TPlus<T>::Neutral = 0;
template <class T>
struct TMax {
  using Type = T;
  static const T Neutral;
  T operator()(const T &x, const T &y) const { return max(x, y); }
};
template <class T>
T const TMax<T>::Neutral = numeric_limits<T>::min();
template <class T>
struct TMin {
  using Type = T;
  static const T Neutral;
  T operator()(const T &x, const T &y) const { return min(x, y); }
};
template <class T>
T const TMin<T>::Neutral = numeric_limits<T>::max();
template <class TF>
struct TSegmentTree {
  using T = typename TF::Type;
  size_t N = 0;
  TF Func;
  vector<T> Data;
  TSegmentTree(size_t n, const TF &f = TF()) : N(n), Func(f) {
    size_t s = 1;
    while (s < n) {
      s *= 2;
    }
    Data.assign(s * 2 - 1, TF::Neutral);
  }
  void Set(size_t i, const T &x) {
    size_t index = i + Data.size() / 2;
    Data[index] = x;
    while (index) {
      index = (index - 1) / 2;
      Data[index] = Func(Data[index * 2 + 1], Data[index * 2 + 2]);
    }
  }
  T Get(size_t left, size_t right, size_t l = 0, size_t r = -1,
        size_t v = 0) const {
    if (left >= right) {
      return TF::Neutral;
    }
    if (r == -1) {
      r = Data.size() / 2 + 1;
    }
    if (l == left && r == right) {
      return Data[v];
    }
    size_t m = l + (r - l) / 2;
    while (m < left || m > right) {
      if (m < left) {
        v = v * 2 + 2;
        l = m;
      } else {
        v = v * 2 + 1;
        r = m;
      }
      m = l + (r - l) / 2;
    }
    return Func(Get(left, min(m, right), l, m, v * 2 + 1),
                Get(max(m, left), right, m, r, v * 2 + 2));
  }
  T GetUpTo(size_t right) {
    T result = TF::Neutral;
    if (right == 0) {
      return result;
    }
    size_t l = 0;
    size_t r = Data.size() / 2 + 1;
    size_t v = 0;
    while (r > right) {
      size_t m = l + (r - l) / 2;
      if (m >= right) {
        r = m;
        v = v * 2 + 1;
      } else {
        result = Func(Data.at(v * 2 + 1), result);
        v = v * 2 + 2;
        l = m;
      }
    }
    result = Func(Data.at(v), result);
    return result;
  }
};
struct TDSU {
  vector<size_t> Parents;
  vector<size_t> Sizes;
  TDSU(size_t n) : Parents(n), Sizes(n, 1) {
    iota(Parents.begin(), Parents.end(), 0);
  }
  size_t Get(size_t x) {
    if (Parents[x] != x) {
      Parents[x] = Get(Parents[x]);
    }
    return Parents[x];
  }
  void Unite(size_t x, size_t y) {
    x = Get(x);
    y = Get(y);
    if (x == y) {
      return;
    }
    if (Sizes[x] > Sizes[y]) {
      std::swap(x, y);
    }
    Parents[x] = y;
    Sizes[y] += Sizes[x];
  }
};
template <class T>
struct TDisjointIntervalSet : public set<pair<T, T>> {
  using TParent = set<pair<T, T>>;
  void Insert(T left, T right) {
    if (right <= left) {
      return;
    }
    auto it = TParent::upper_bound(make_pair(right, numeric_limits<T>::max()));
    auto it1 = it;
    while (it != TParent::begin()) {
      --it;
      if (it->second >= left) {
        remin(left, it->first);
        remax(right, it->second);
      } else {
        ++it;
        break;
      }
    }
    TParent::erase(it, it1);
    TParent::emplace(left, right);
  }
  void Remove(T left, T right) {
    if (right <= left) {
      return;
    }
    auto it = TParent::upper_bound(make_pair(right, numeric_limits<T>::max()));
    auto it1 = it;
    T l = left;
    T r = right;
    while (it != TParent::begin()) {
      --it;
      if (it->second >= left) {
        remin(l, it->first);
        remax(r, it->second);
      } else {
        ++it;
        break;
      }
    }
    TParent::erase(it, it1);
    if (l < left) {
      TParent::emplace(l, left);
    }
    if (r > right) {
      TParent::emplace(right, r);
    }
  }
};
void Solve() {}
void RunGCJ() {
  size_t __number_of_cases;
  cin >> __number_of_cases;
  for (size_t __number_of_case = 0; __number_of_case < __number_of_cases;
       ++__number_of_case) {
    cout << "Case #" << __number_of_case + 1 << ": ";
    Solve();
  }
}
unordered_map<int, int> get_counts(const vector<int> &q, int a, int b) {
  unordered_map<int, int> result;
  for (int i = a; i <= b; ++i) {
    ++result[q[i]];
  }
  return result;
}
int main(void) {
  std::ios::sync_with_stdio(false);
  int64_t n;
  cin >> n;
  vector<int> q(n);
  cin >> q;
  int64_t l = 0;
  while (l * 2 < n && q[l] == q[n - 1 - l]) {
    ++l;
  }
  if (l * 2 >= n) {
    cout << n * (n + 1) / 2 << endl;
    return 0;
  }
  int64_t r = n - 1 - l;
  int64_t c = n / 2;
  while (q[c] == q[n - 1 - c]) {
    --c;
  }
  int64_t a = c, b = n - 1 - c;
  if (get_counts(q, l, a) == get_counts(q, b, r)) {
    cout << (l + 1) * (n - a) + (b - l) * (n - r) << endl;
    return 0;
  }
  auto counts = get_counts(q, l, r);
  int odd = 0;
  for (auto x : counts) {
    odd += x.second % 2;
  }
  if (odd > 1) {
    cout << 0 << endl;
    return 0;
  }
  int64_t bestr = r;
  while (true) {
    if ((counts[q[bestr]] -= 2) >= 0) {
      --bestr;
    } else {
      break;
    }
  }
  int64_t bestl = l;
  counts = get_counts(q, l, r);
  while (true) {
    if ((counts[q[bestl]] -= 2) >= 0) {
      ++bestl;
    } else {
      break;
    }
  }
  int64_t result = 0;
  result += (bestl - l) * (n - r);
  result += (r - bestr) * (l + 1);
  result += (l + 1) * (n - r);
  cout << result << endl;
  return 0;
}

```