#include <bits/stdc++.h>
using namespace std;
int n, nn;
char s[200];
const int MOD = 51123987;
const int INF = 1e9;
int p[3];
int nxt[3][200];
int d[151][2][52][52] = {};
inline void add(int &a, const int &b) {
  a += b;
  if (a > MOD) a -= MOD;
}
int main() {
  scanf("%d%s", &n, s + 1);
  int del = 0;
  for (int i = 2; i <= n; ++i)
    if (s[i] == s[i - 1])
      ++del;
    else
      s[i - del] = s[i];
  nn = n;
  n -= del;
  for (int i = 0; i < (int)(3); i++) p[i] = INF;
  for (int i = n; i >= 0; --i) {
    if (i) p[s[i] - 'a'] = i;
    for (int j = 0; j < (int)(3); j++) nxt[j][i] = p[j];
  }
  d[0][0][0][0] = 1;
  int N = (nn + 2) / 3;
  int ans = 0;
  for (int a = 0; a < (int)(N + 1); a++) {
    for (int i = 0; i < (int)(n + 1); i++)
      for (int b = 0; b < (int)(N + 1); b++)
        for (int c = 0; c < (int)(N + 1); c++) {
          if (nxt[0][i] != INF) add(d[nxt[0][i]][1][b][c], d[i][0][b][c]);
          if (nxt[1][i] != INF) add(d[nxt[1][i]][0][b + 1][c], d[i][0][b][c]);
          if (nxt[2][i] != INF) add(d[nxt[2][i]][0][b][c + 1], d[i][0][b][c]);
        }
    if (a >= N - 1)
      for (int b = N - 1; b <= N; ++b)
        for (int c = N - 1; c <= N; ++c) {
          if (a + b + c == nn)
            for (int i = 1; i <= n; ++i) add(ans, d[i][0][b][c]);
        }
    for (int i = 0; i < (int)(n + 1); i++)
      for (int b = 0; b < (int)(N + 1); b++)
        for (int c = 0; c < (int)(N + 1); c++)
          d[i][0][b][c] = d[i][1][b][c], d[i][1][b][c] = 0;
  }
  printf("%d\n", ans);
  return 0;
}
