#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  int f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
  return x * f;
}
void print(long long x) {
  if (x < 0) x = -x, putchar('-');
  if (x > 9) print(x / 10);
  putchar(x % 10 + '0');
}
char s[155];
int nxt[151][3];
int n, lim, ans;
int dp[155][55][55][55];
const int mod = 51123987;
int MOD(int &a, int b) {
  a += b;
  if (a > mod) a -= mod;
}
int main() {
  n = read();
  scanf("%s", s + 1);
  nxt[n + 1][0] = nxt[n + 1][1] = nxt[n + 1][2] = n + 1;
  for (int i = n; i >= 1; i--) {
    nxt[i][0] = nxt[i + 1][0];
    nxt[i][1] = nxt[i + 1][1];
    nxt[i][2] = nxt[i + 1][2];
    if (s[i] == 'a') nxt[i][0] = i;
    if (s[i] == 'b') nxt[i][1] = i;
    if (s[i] == 'c') nxt[i][2] = i;
  }
  lim = (n + 2) / 3;
  dp[1][0][0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int a = 0; a <= lim; a++)
      for (int b = 0; b <= lim; b++)
        for (int c = 0; c <= lim; c++) {
          if (a + b + c == n && abs(a - b) <= 1 && abs(a - c) <= 1 &&
              abs(b - c) <= 1) {
            MOD(ans, dp[i][a][b][c]);
            continue;
          }
          if (nxt[i][0] <= n) MOD(dp[nxt[i][0]][a + 1][b][c], dp[i][a][b][c]);
          if (nxt[i][1] <= n) MOD(dp[nxt[i][1]][a][b + 1][c], dp[i][a][b][c]);
          if (nxt[i][2] <= n) MOD(dp[nxt[i][2]][a][b][c + 1], dp[i][a][b][c]);
        }
  ::print(ans), putchar('\n');
}
