#include <bits/stdc++.h>
using namespace std;
const int MOD = 51123987;
int dp[155][55][55][55] = {0};
int nex[155][3] = {0};
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  nex[n][0] = nex[n][1] = nex[n][2] = n;
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < 3; ++j) nex[i][j] = nex[i + 1][j];
    nex[i][s[i] - 'a'] = i;
  }
  dp[0][0][0][0] = 1;
  int m = (n + 5) / 3;
  int ans = 0;
  for (int i = 0; i < n; ++i)
    for (int a = 0; a <= m; ++a)
      for (int b = 0; b <= m; ++b)
        for (int c = 0; c <= m; ++c) {
          if (a + b + c == n && abs(a - b) <= 1 && abs(a - c) <= 1 &&
              abs(b - c) <= 1) {
            (ans += dp[i][a][b][c]) %= MOD;
            continue;
          }
          if (nex[i][0] < n)
            (dp[nex[i][0]][a + 1][b][c] += dp[i][a][b][c]) %= MOD;
          if (nex[i][1] < n)
            (dp[nex[i][1]][a][b + 1][c] += dp[i][a][b][c]) %= MOD;
          if (nex[i][2] < n)
            (dp[nex[i][2]][a][b][c + 1] += dp[i][a][b][c]) %= MOD;
        }
  cout << ans << endl;
}
