#include <bits/stdc++.h>
using namespace std;
const int N = 155, M = 55, P = 51123987;
int n, m, f[N][M][M][M], v[N][3], a[N], ans = 0;
inline void Add(int &x, int y) { x = (x + y) % P; }
int main() {
  scanf("%d\n", &n);
  for (int i = 1; i <= n; ++i) a[i] = getchar() - 'a';
  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][a[i]] = i;
  m = n / 3 + 2;
  f[1][0][0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int x = 0; x < m; ++x)
      for (int y = 0; y < m; ++y)
        for (int z = 0; z < m; ++z) {
          if (x + y + z == n && abs(x - y) < 2 && abs(x - z) < 2 &&
              abs(y - z) < 2)
            Add(ans, f[i][x][y][z]);
          Add(f[v[i][0]][x + 1][y][z], f[i][x][y][z]);
          Add(f[v[i][1]][x][y + 1][z], f[i][x][y][z]);
          Add(f[v[i][2]][x][y][z + 1], f[i][x][y][z]);
        }
  printf("%d\n", ans);
}
