#include <bits/stdc++.h>
using namespace std;
inline int absi(int a) { return (a > 0) ? a : -a; }
const int md = 51123987;
inline void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}
int a[155];
int tmp[4];
int nxt[155][4];
int dp[155][55][55][55];
int main(void) {
  int n, mx;
  scanf("%d", &n);
  mx = (n + 2) / 3;
  char c = getchar();
  while (c == 10 || c == 32) c = getchar();
  for (int i = 1; i <= n; ++i) {
    a[i] = c - 'a' + 1;
    c = getchar();
  }
  memset(tmp, 255, sizeof(tmp));
  for (int i = n; i; --i) {
    tmp[a[i]] = i;
    memcpy(nxt[i], tmp, sizeof(tmp));
  }
  int res = 0;
  dp[1][0][0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= mx; ++j) {
      for (int k = 0; k <= mx; ++k) {
        for (int l = 0; l <= mx; ++l) {
          if (j + k + l > n) break;
          int val = dp[i][j][k][l];
          if (j + k + l == n) {
            if (absi(j - k) <= 1 && absi(j - l) <= 1 && absi(k - l) <= 1) {
              add(res, val);
            }
            continue;
          }
          if (~nxt[i][1]) {
            add(dp[nxt[i][1]][j + 1][k][l], val);
          }
          if (~nxt[i][2]) {
            add(dp[nxt[i][2]][j][k + 1][l], val);
          }
          if (~nxt[i][3]) {
            add(dp[nxt[i][3]][j][k][l + 1], val);
          }
        }
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
