#include <bits/stdc++.h>
using namespace std;
const int p = 51123987;
int n, i, j, k, l, ans, n1;
char c[1000];
int d[152][3], f[151][52][52][52];
int main() {
  scanf("%d\n", &n);
  for (i = 1; i <= n; i++) scanf("%c", &c[i]);
  for (i = 1; i <= n; i++)
    for (j = 0; j < 3; j++)
      for (k = i; k <= n; k++)
        if (c[k] == ('a' + j)) {
          d[i][j] = k;
          break;
        }
  f[1][0][0][0] = 1;
  if (n % 3 == 0)
    n1 = n / 3;
  else
    n1 = n / 3 + 1;
  for (i = 1; i <= n; i++)
    for (j = 0; j <= n1; j++)
      for (k = 0; k <= n1; k++)
        for (l = 0; l <= n1; l++) {
          if (j < n1 && d[i][0] > 0)
            f[d[i][0]][j + 1][k][l] =
                (f[d[i][0]][j + 1][k][l] + f[i][j][k][l]) % p;
          if (k < n1 && d[i][1] > 0)
            f[d[i][1]][j][k + 1][l] =
                (f[d[i][1]][j][k + 1][l] + f[i][j][k][l]) % p;
          if (l < n1 && d[i][2] > 0)
            f[d[i][2]][j][k][l + 1] =
                (f[d[i][2]][j][k][l + 1] + f[i][j][k][l]) % p;
        }
  i = n / 3;
  if (n % 3 == 0)
    for (j = 1; j <= n; j++) ans = (ans + f[j][i][i][i]) % p;
  if (n % 3 == 1)
    for (j = 1; j <= n; j++)
      ans =
          (ans + f[j][i][i][i + 1] + f[j][i][i + 1][i] + f[j][i + 1][i][i]) % p;
  if (n % 3 == 2)
    for (j = 1; j <= n; j++)
      ans = (ans + f[j][i][i + 1][i + 1] + f[j][i + 1][i][i + 1] +
             f[j][i + 1][i + 1][i]) %
            p;
  printf("%d\n", ans);
  return 0;
}
