#include <bits/stdc++.h>
using namespace std;
int n, m, f[153][53][53][53], v[151][3], z, MOD = 51123987, u;
char s[160];
bool check(int a, int b, int c) {
  if (abs(a - b) > 1) return false;
  if (abs(a - c) > 1) return false;
  if (abs(b - c) > 1) return false;
  return true;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = n; i; i--)
    v[i][0] = v[i + 1][0], v[i][1] = v[i + 1][1], v[i][2] = v[i + 1][2],
    v[i][s[i] - 'a'] = i;
  f[1][0][0][0] = 1;
  m = n / 3 + 2;
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k < m; k++)
        for (int l = 0; l < m; l++)
          if (f[i][j][k][l]) {
            if (j + k + l == n && check(j, k, l)) z = (z + f[i][j][k][l]) % MOD;
            f[v[i][0]][j + 1][k][l] =
                (f[v[i][0]][j + 1][k][l] + f[i][j][k][l]) % MOD;
            f[v[i][1]][j][k + 1][l] =
                (f[v[i][1]][j][k + 1][l] + f[i][j][k][l]) % MOD;
            f[v[i][2]][j][k][l + 1] =
                (f[v[i][2]][j][k][l + 1] + f[i][j][k][l]) % MOD;
          }
  printf("%d", z);
}
