#include <bits/stdc++.h>
using namespace std;
const int maxn = 160 + 5, mod = 51123987;
bool _1;
int n, ans;
char s[maxn];
int nxt[maxn][5];
int dp[maxn][55][55][55];
void add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
bool _2;
int main() {
  scanf("%d%s", &n, 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;
  }
  dp[1][0][0][0] = 1;
  int S = (n + 2) / 3;
  for (int i = 1; i <= n; ++i)
    for (int j = 0; j <= S; ++j)
      for (int k = 0; k <= S; ++k)
        for (int l = 0; l <= S; ++l) {
          if (j + k + l == n && abs(j - k) <= 1 && abs(j - l) <= 1 &&
              abs(k - l) <= 1) {
            add(ans, dp[i][j][k][l]);
          }
          add(dp[nxt[i][0]][j + 1][k][l], dp[i][j][k][l]);
          add(dp[nxt[i][1]][j][k + 1][l], dp[i][j][k][l]);
          add(dp[nxt[i][2]][j][k][l + 1], dp[i][j][k][l]);
        }
  printf("%d\n", ans);
  return 0;
}
