#include <bits/stdc++.h>
using namespace std;
int f[155][55][55][55];
int g[155][3];
int n, m;
const int mod = 51123987;
string s;
int main() {
  cin >> n;
  cin >> s;
  for (int(pos) = 0; (pos) < (n + 1); ++(pos))
    for (int(i) = 0; (i) < (3); ++(i)) g[pos][i] = n;
  for (int pos = n - 1; pos >= 0; --pos) {
    for (int(i) = 0; (i) < (3); ++(i)) g[pos][i] = g[pos + 1][i];
    g[pos][s[pos] - 'a'] = pos;
  }
  f[0][0][0][0] = 1;
  m = (n + 2) / 3 + 1;
  int ans = 0;
  for (int(pos) = 0; (pos) < (n); ++(pos))
    for (int(i) = 0; (i) < (m); ++(i))
      for (int(j) = 0; (j) < (m); ++(j))
        for (int(k) = 0; (k) < (m); ++(k)) {
          f[g[pos][0]][i + 1][j][k] =
              (f[g[pos][0]][i + 1][j][k] + f[pos][i][j][k]) % mod;
          f[g[pos][1]][i][j + 1][k] =
              (f[g[pos][1]][i][j + 1][k] + f[pos][i][j][k]) % mod;
          f[g[pos][2]][i][j][k + 1] =
              (f[g[pos][2]][i][j][k + 1] + f[pos][i][j][k]) % mod;
          if (i + j + k == n && abs(i - j) < 2 && abs(j - k) < 2 &&
              abs(i - k) < 2)
            ans = (ans + f[pos][i][j][k]) % mod;
        }
  cout << ans << endl;
  return 0;
}
