#include <bits/stdc++.h>
using namespace std;
int dp[2][51][51][151], last[3][151];
const int mod = 51123987;
int main() {
  char str[155];
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (3); ++j) {
      if (str[i] - 'a' == j)
        last[j][i] = i;
      else if (i == 0)
        last[j][i] = n;
      else
        last[j][i] = last[j][i - 1];
    }
  int limit = (n + 2) / 3;
  for (int i = 0; i < (n); ++i) {
    int cur = (i & 1);
    int prev = 1 - cur;
    for (int A = (0); A <= (limit); ++A)
      for (int B = (0); B <= (limit); ++B) {
        for (int k = 0; k < (n); ++k) {
          if (i == 0) {
            if (str[k] == 'a' && A == 1 && B == 0)
              dp[cur][A][B][k] = 1;
            else if (str[k] == 'b' && B == 1 && A == 0)
              dp[cur][A][B][k] = 1;
            else if (str[k] == 'c' && A + B == 0)
              dp[cur][A][B][k] = 1;
            else
              dp[cur][A][B][k] = 0;
            continue;
          }
          dp[cur][A][B][k] = 0;
          int A0 = A, B0 = B;
          if (str[k] == 'a') --A0;
          if (str[k] == 'b') --B0;
          if (A0 < 0 || B0 < 0) continue;
          dp[cur][A][B][k] =
              (dp[prev][A0][B0][last[0][k]] + dp[prev][A0][B0][last[1][k]] +
               dp[prev][A0][B0][last[2][k]]) %
              mod;
        }
      }
  }
  int cur = (n - 1) & 1;
  int ans = 0;
  int V[] = {n / 3 - 1, n / 3, n / 3 + 1};
  for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
      if (V[i] >= 0 && V[j] >= 0) {
        int A[] = {V[i], V[j], n - V[j] - V[i]};
        sort(A, A + 3);
        if (A[2] - A[0] <= 1) {
          ans = (ans + dp[cur][V[i]][V[j]][last[0][n - 1]] +
                 dp[cur][V[i]][V[j]][last[1][n - 1]] +
                 dp[cur][V[i]][V[j]][last[2][n - 1]]) %
                mod;
        }
      }
  printf("%d\n", ans);
  return 0;
}
