#include <bits/stdc++.h>
using namespace std;
int f[152][60][60][60];
int nex[152][3];
char s[152];
int n;
const int MOD = 51123987;
int ans;
void Inc(int &a, int b) {
  a += b;
  a %= MOD;
}
int main() {
  scanf("%d\n", &n);
  scanf("%s", s);
  for (int i = n; i >= 1; --i) {
    int x = s[i - 1] - 'a';
    for (int j = 0; j < 3; ++j) nex[i][j] = nex[i + 1][j];
    nex[i][x] = i;
  }
  int mx = n / 3 + 2;
  ans = 0;
  f[1][0][0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int x = 0; x < mx; ++x)
      for (int y = 0; y < mx; ++y)
        for (int r = 0; r < mx; ++r) {
          int aug = f[i][x][y][r];
          if (!aug) continue;
          if (x + y + r == n && abs(x - y) <= 1 && abs(x - r) <= 1 &&
              abs(y - r) <= 1)
            Inc(ans, aug);
          Inc(f[nex[i][0]][x + 1][y][r], aug);
          Inc(f[nex[i][1]][x][y + 1][r], aug);
          Inc(f[nex[i][2]][x][y][r + 1], aug);
        }
  }
  cout << ans << endl;
  return 0;
}
