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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#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;
}