#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int max_n = 2e6 + 5, mod = 51123987;
char s[max_n], a[max_n * 2];
int n = 0, t, len[max_n * 2], pre[max_n], suf[max_n];
long long sum_suf[max_n];
signed main() {
// freopen("in", "r", stdin), freopen("out", "w", stdout);
scanf("%d", &t);
scanf("%s", s + 1);
for (int i = 1; i <= t; i++)a[++n] = '#', a[++n] = s[i];
a[++n] = '#', a[0] = '$';
for (int i = 1, id = 0, mx = 0; i <= n; i++) {
if (i < mx)len[i] = min(len[2 * id - i], mx - i);
else len[i] = 1;
while (a[i - len[i]] == a[i + len[i]])len[i]++;
if (mx < i + len[i])mx = i + len[i], id = i;
}
long long sum = 0;
for (int i = 1; i <= n; i++) {
pre[(i + 1) / 2]++;
if (a[i] == '#')pre[(i + 1) / 2 + (len[i] - 1) / 2]--;
else pre[(i + 1) / 2 + len[i] / 2]--;
suf[i / 2]++;
if (a[i] == '#')suf[i / 2 - (len[i] - 1) / 2]--;
else suf[i / 2 - len[i] / 2]--;
if (a[i] == '#')sum += (len[i] - 1) / 2;
else sum += len[i] / 2;
}
for (int i = 1; i <= t; i++)pre[i] += pre[i - 1];
for (int i = t; i >= 1; i--)suf[i] += suf[i + 1];
for (int i = t; i >= 1; i--)sum_suf[i] = sum_suf[i + 1] + suf[i];
long long ans =
sum % 2 == 0 ?
(sum / 2 % mod * ((sum - 1) % mod) % mod) :
((sum - 1) / 2 % mod * (sum % mod) % mod);
for (int i = 1; i <= t; i++)ans -= sum_suf[i + 1] % mod * pre[i] % mod, ans %= mod, ans += mod, ans %= mod;
printf("%lld\n", ans);
return 0;
}