#include <bits/stdc++.h>
using namespace std;
const int mod = 51123987;
int pos[155][3], dp[155][55][55][55];
string s;
int main() {
  int n, i, a, b, c, ans = 0;
  cin >> n >> s;
  s = " " + s;
  pos[n + 1][0] = pos[n + 1][1] = pos[n + 1][2] = 200;
  for (i = n; i; i--) {
    pos[i][0] = pos[i + 1][0];
    pos[i][1] = pos[i + 1][1];
    pos[i][2] = pos[i + 1][2];
    if (s[i] == 'a') pos[i][0] = i;
    if (s[i] == 'b') pos[i][1] = i;
    if (s[i] == 'c') pos[i][2] = i;
  }
  dp[1][0][0][0] = 1;
  for (i = 1; i <= n; i++)
    for (a = 0; a <= (n + 2) / 3; a++)
      for (b = 0; b <= (n + 2) / 3; b++)
        for (c = 0; c <= (n + 2) / 3; c++) {
          if (a + b + c == n && llabs(a - b) < 2 && llabs(b - c) < 2 &&
              llabs(c - a) < 2) {
            ans = (ans + dp[i][a][b][c]) % mod;
            continue;
          }
          if (pos[i][0] != 200)
            dp[pos[i][0]][a + 1][b][c] =
                (dp[pos[i][0]][a + 1][b][c] + dp[i][a][b][c]) % mod;
          if (pos[i][1] != 200)
            dp[pos[i][1]][a][b + 1][c] =
                (dp[pos[i][1]][a][b + 1][c] + dp[i][a][b][c]) % mod;
          if (pos[i][2] != 200)
            dp[pos[i][2]][a][b][c + 1] =
                (dp[pos[i][2]][a][b][c + 1] + dp[i][a][b][c]) % mod;
        }
  cout << ans;
  return 0;
}
