#include <bits/stdc++.h>
using namespace std;
const int u = 2000010, mod = 51123987;
int f[152][52][52][52], a[152][3], n, m, i, j, k, l, p, ans;
string s;
inline void add(int &x, int y) { x = (x + y) % mod; }
int main() {
  cin >> n >> s;
  a[n][0] = a[n][1] = a[n][2] = n;
  for (i = n - 1; i >= 0; i--)
    a[i][0] = a[i + 1][0], a[i][1] = a[i + 1][1], a[i][2] = a[i + 1][2],
    a[i][s[i] - 'a'] = i;
  f[0][0][0][0] = 1;
  m = (n + 2) / 3;
  for (i = 0; i < n; i++)
    for (j = 0; j <= m; j++)
      for (k = 0; k <= m; k++)
        for (l = 0; l <= m; l++) {
          p = f[i][j][k][l];
          if (j + k + l == n && abs(j - k) < 2 && abs(j - l) < 2 &&
              abs(k - l) < 2)
            add(ans, p);
          add(f[a[i][0]][j + 1][k][l], p), add(f[a[i][1]][j][k + 1][l], p),
              add(f[a[i][2]][j][k][l + 1], p);
        }
  cout << ans << endl;
  return 0;
}
