#include <bits/stdc++.h>
using namespace std;
const int M = 51123987;
int dp[2][55][55][155];
char s[155];
int val[155];
int n;
int lst[3], nxt[3][155];
inline void upd(int &a, int &b) {
  a += b;
  if (a >= M) a -= M;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) val[i] = s[i] - 'a';
  lst[0] = lst[1] = lst[2] = n;
  for (int i = n - 1; i >= 0; i--) {
    lst[val[i]] = i;
    nxt[0][i] = lst[0];
    nxt[1][i] = lst[1];
    nxt[2][i] = lst[2];
  }
  int p = 0, q = 1;
  dp[p][0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    memset(dp[q], 0, sizeof(dp[q]));
    for (int ca = 0; ca <= i && ca <= (n + 2) / 3; ca++) {
      for (int cb = 0; cb + ca <= i && cb <= (n + 2) / 3; cb++) {
        for (int pos = 0; pos < n; pos++)
          if (dp[p][ca][cb][pos]) {
            if (nxt[0][pos] < n && ca + 1 <= (n + 2) / 3)
              upd(dp[q][ca + 1][cb][nxt[0][pos]], dp[p][ca][cb][pos]);
            if (nxt[1][pos] < n && cb + 1 <= (n + 2) / 3)
              upd(dp[q][ca][cb + 1][nxt[1][pos]], dp[p][ca][cb][pos]);
            if (nxt[2][pos] < n && (i - ca - cb + 1) <= (n + 2) / 3)
              upd(dp[q][ca][cb][nxt[2][pos]], dp[p][ca][cb][pos]);
          }
      }
    }
    swap(p, q);
  }
  int ans = 0;
  for (int ca = n / 3; ca <= (n + 2) / 3; ca++)
    for (int cb = n / 3; cb <= (n + 2) / 3; cb++) {
      int cc = n - ca - cb;
      if (cc < 0 || abs(cc - ca) > 1 || abs(cc - cb) > 1) continue;
      for (int pos = 0; pos < n; pos++) upd(ans, dp[p][ca][cb][pos]);
    }
  printf("%d\n", ans);
  return 0;
}
