Palisection

In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», «racecar», «rotor», «madam».

Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.

Let’s look at the actions, performed by Nick, by the example of text «babb». At first he wrote out all subpalindromes:• «b» — 1..1• «bab» — 1..3• «a» — 2..2• «b» — 3..3• «bb» — 3..4• «b» — 4..4

Then Nick counted the amount of different pairs among these subpalindromes that cross. These pairs were six:1. 1..1 cross with 1..32. 1..3 cross with 2..23. 1..3 cross with 3..34. 1..3 cross with 3..45. 3..3 cross with 3..46. 3..4 cross with 4..4

Since it’s very exhausting to perform all the described actions manually, Nick asked you to help him and write a program that can find out the amount of different subpalindrome pairs that cross. Two subpalindrome pairs are regarded as different if one of the pairs contains a subpalindrome that the other does not.

Input

The first input line contains integer n (1 ≤ n ≤ 2·106) — length of the text. The following line contains n lower-case Latin letters (from a to z).

Output

In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987.

Examples

input

4
babb

output

6

input

2
aa

output

2

Solution:

#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
return '"' + s + '"';
}

string to_string(const char* s) {
return to_string((string) s);
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template <typename T>
vector<int> manacher(int n, const T &s) {
if (n == 0) {
return vector<int>();
}
vector<int> res(2 * n - 1, 0);
{
// odd
int l = -1, r = -1;
for (int i = 0; i < n; i++) {
      int p = (i >= r ? 0 : min(r - i, res[2 * (l + r - i)]));
while (i + p + 1 < n && i - p - 1 >= 0) {
if (s[i + p + 1] != s[i - p - 1]) {
break;
}
p++;
}
if (i + p > r) {
l = i - p;
r = i + p;
}
res[2 * i] = p;
}
}
{
// even
int l = -1, r = -1;
for (int i = 0; i < n - 1; i++) {
      debug(l, r);
      int p = (i >= r ? 0 : min(r - i, res[2 * (l + r - i) - 1]));
while (i + p + 1 < n && i - p >= 0) {
if (s[i + p + 1] != s[i - p]) {
break;
}
p++;
}
if (i + p > r) {
l = i - p + 1;
r = i + p;
}
res[2 * i + 1] = p;
}
}
return res;
// res[2 * i] = odd radius in position i
// res[2 * i + 1] = even radius between positions i and i + 1
// s = "abaa" -> res = {0, 0, 1, 0, 0, 1, 0}
}

template <typename T>
vector<int> manacher(const T &s) {
return manacher(s.size(), s);
}

const int md = 51123987;

inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}

inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}

inline int mul(int a, int b) {
#if !defined(_WIN32) || defined(_WIN64)
  return (long long) a * b % md;
#endif
  unsigned long long x = (long long) a * b;
  unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
asm(
"divl %4; \n\t"
: "=a" (d), "=d" (m)
: "d" (xh), "a" (xl), "r" (md)
);
return m;
}

inline int power(int a, long long b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}

inline int inv(int a) {
a %= md;
if (a < 0) a += md;
  int b = md, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  assert(b == 1);
  if (u < 0) u += md;
  return u;
}

char s[2345678];

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  vector<int> p = manacher(n, s);
debug(p);
int total = n;
for (int i = 0; i < 2 * n - 1; i++) {
    add(total, p[i]);
  }
  vector<int> start(n + 1, 0), finish(n + 1, 0);
for (int i = 0; i < n; i++) {
    start[i - p[2 * i]]++;
    start[i + 1]--;
    finish[i]++;
    finish[i + p[2 * i] + 1]--;
  }
  for (int i = 0; i < n - 1; i++) {
    start[i - p[2 * i + 1] + 1]++;
    start[i + 1]--;
    finish[i + 1]++;
    finish[i + p[2 * i + 1] + 1]--;
  }
  for (int i = 0; i < n; i++) {
    start[i + 1] += start[i];
    finish[i + 1] += finish[i];
  }
  int ans = 0;
  int acc = 0;
  for (int i = 0; i < n; i++) {
    add(ans, mul(start[i], acc));
    add(acc, finish[i]);
  }
  total = mul(mul(total, total + md - 1), inv(2));
  sub(total, ans);
  printf("%d\n", total);
  return 0;
}