#include <bits/stdc++.h>
using namespace std;
const int MAXN = 155, MAXM = 55;
const int mod = 51123987;
int ans, n, m, a[MAXN], pre[3][MAXN], dp[MAXN][MAXM][MAXM][MAXM];
int get() {
  int f = 0, v = 0;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') break;
  if (ch == '-')
    f = 1;
  else
    v = ch - 48;
  while (isdigit(ch = getchar())) v = v * 10 + ch - 48;
  if (f == 1) return -v;
  return v;
}
int main() {
  n = get();
  char ch;
  m = n / 3 + 1;
  for (int i = 1; i <= n; i++) {
    while (!isalpha(ch = getchar()))
      ;
    a[i] = ch - 'a';
    for (int j = 0; j <= 2; j++)
      if (a[i] == j)
        pre[j][i] = i;
      else
        pre[j][i] = pre[j][i - 1];
  }
  for (int i = 1; i <= n; i++)
    for (int x = 0; x <= m; x++)
      for (int y = 0; y <= m; y++)
        for (int z = 0; z <= m; z++) {
          if (x + y + z > n) continue;
          if (a[i] == 0 && x == 0 || a[i] == 1 && y == 0 || a[i] == 2 && z == 0)
            continue;
          if (x + y + z == 1)
            dp[i][x][y][z] = 1;
          else {
            for (int j = 0; j <= 2; j++) {
              dp[i][x][y][z] += dp[pre[j][i]][x - (a[i] == 0)][y - (a[i] == 1)]
                                  [z - (a[i] == 2)];
              if (dp[i][x][y][z] >= mod) dp[i][x][y][z] -= mod;
            }
          }
          if (x + y + z == n && abs(x - y) <= 1 && abs(x - z) <= 1 &&
              abs(y - z) <= 1 && i == pre[a[i]][n]) {
            ans += dp[i][x][y][z];
            if (ans >= mod) ans -= mod;
          }
        }
  printf("%d\n", ans);
  return 0;
}
