#include <bits/stdc++.h>
using namespace std;
const int mod = 51123987;
int n, lim;
char str[155];
int Next[155][3];
int dp[155][55][55][55];
int ans;
int main() {
  while (scanf("%d", &n) == 1) {
    scanf("%s", str + 1);
    Next[n + 1][0] = Next[n + 1][1] = Next[n + 1][2] = n + 1;
    for (int i = n; i >= 1; i--) {
      Next[i][0] = Next[i + 1][0];
      Next[i][1] = Next[i + 1][1];
      Next[i][2] = Next[i + 1][2];
      if (str[i] == 'a') Next[i][0] = i;
      if (str[i] == 'b') Next[i][1] = i;
      if (str[i] == 'c') Next[i][2] = i;
    }
    ans = 0;
    lim = (n + 2) / 3;
    memset(dp, 0, sizeof(dp));
    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 = (ans + dp[i][a][b][c]) % mod;
              continue;
            }
            if (Next[i][0] <= n)
              dp[Next[i][0]][a + 1][b][c] =
                  (dp[Next[i][0]][a + 1][b][c] + dp[i][a][b][c]) % mod;
            if (Next[i][1] <= n)
              dp[Next[i][1]][a][b + 1][c] =
                  (dp[Next[i][1]][a][b + 1][c] + dp[i][a][b][c]) % mod;
            if (Next[i][2] <= n)
              dp[Next[i][2]][a][b][c + 1] =
                  (dp[Next[i][2]][a][b][c + 1] + dp[i][a][b][c]) % mod;
          }
    printf("%d\n", ans);
  }
  return 0;
}
