#include <bits/stdc++.h>
using namespace std;
char s[155];
int nxt[155][3];
int n, lim, ans;
int dp[155][55][55][55];
const int MOD = 51123987;
inline int Abs(int x) { return x > 0 ? x : -x; }
int main() {
  scanf("%d", &n);
  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) {
            (ans += dp[i][a][b][c]) %= MOD;
            continue;
          }
          if (nxt[i][0] <= n)
            (dp[nxt[i][0]][a + 1][b][c] += dp[i][a][b][c]) %= MOD;
          if (nxt[i][1] <= n)
            (dp[nxt[i][1]][a][b + 1][c] += dp[i][a][b][c]) %= MOD;
          if (nxt[i][2] <= n)
            (dp[nxt[i][2]][a][b][c + 1] += dp[i][a][b][c]) %= MOD;
        }
  printf("%d", ans);
}
