#include <bits/stdc++.h>
using namespace std;
char s[2000010];
vector<pair<int, int> > nxt[2000010];
int fail[2000010], str[2000010], cnt[2000010], len[2000010], dep[2000010],
    f[2000010], sz = -1, last, n;
int creat(int l) {
  len[++sz] = l;
  dep[sz] = 0;
  return sz;
}
int getfail(int x) {
  while (str[n - len[x] - 1] != str[n]) x = fail[x];
  return x;
}
int find(int x, int c) {
  for (int i = 0; i < nxt[x].size(); i++)
    if (nxt[x][i].first == c) return nxt[x][i].second;
  return 0;
}
void Insert(int c) {
  str[++n] = c;
  int cur = getfail(last), now;
  if (!find(cur, c)) {
    int now = creat(len[cur] + 2);
    fail[now] = find(getfail(fail[cur]), c);
    dep[now] = dep[fail[now]] + 1;
    nxt[cur].push_back(make_pair(c, now));
  }
  last = find(cur, c);
}
void prepare() {
  creat(0);
  creat(-1);
  str[0] = -1;
  last = 0;
  fail[0] = 1;
}
int main() {
  int l;
  scanf("%d", &l);
  scanf("%s", s);
  prepare();
  for (int i = 0; i < l; i++) {
    Insert(s[i] - 'a');
    f[i + 1] = (dep[last] + f[i]) % 51123987;
  }
  reverse(s, s + l);
  for (int i = 0; i <= sz; i++) nxt[i].clear();
  sz = -1;
  prepare();
  n = 0;
  int ans = 1LL * f[l] * (f[l] - 1) / 2 % 51123987;
  for (int i = 0; i < l; i++) {
    Insert(s[i] - 'a');
    ans -= 1LL * dep[last] * f[l - i - 1] % 51123987;
    ans %= 51123987;
  }
  ans = (ans + 51123987) % 51123987;
  printf("%d", ans);
}
