#include <bits/stdc++.h>
const int Mod = 51123987;
inline int diff(int a, int b) { return a > b ? a - b : b - a; }
int n, nx[155][3], dpa[155][55][55][55], res;
char s[155];
int main() {
  scanf("%d %s", &n, s + 1);
  nx[n + 1][0] = nx[n + 1][1] = nx[n + 1][2] = 0;
  for (int i = n; i >= 1; --i) {
    nx[i][0] = nx[i + 1][0];
    nx[i][1] = nx[i + 1][1];
    nx[i][2] = nx[i + 1][2];
    nx[i][int(s[i] - 'a')] = i;
  }
  int m = n / 3 + 2;
  dpa[nx[1][0]][1][0][0] = 1;
  dpa[nx[1][1]][0][1][0] = 1;
  dpa[nx[1][2]][0][0][1] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int a = 0; a <= m; ++a) {
      for (int b = 0; b <= m; ++b) {
        for (int c = 0; c <= m; ++c) {
          (dpa[nx[i][0]][a + 1][b][c] += dpa[i][a][b][c]) %= Mod;
          (dpa[nx[i][1]][a][b + 1][c] += dpa[i][a][b][c]) %= Mod;
          (dpa[nx[i][2]][a][b][c + 1] += dpa[i][a][b][c]) %= Mod;
          if (a + b + c == n && diff(a, b) <= 1 && diff(b, c) <= 1 &&
              diff(a, c) <= 1) {
            (res += dpa[i][a][b][c]) %= Mod;
          }
        }
      }
    }
  }
  printf("%d\n", res);
}
