#include <bits/stdc++.h>
using namespace std;
int n;
int nxt[3][155], pre[3], dp[155][55][55][55];
char ch[155];
int main() {
  scanf("%d", &n);
  int t = 0;
  scanf("%s", ch);
  for (int i = 0; i < n; i++)
    if (t == 0 || ch[i] != ch[t - 1]) ch[t++] = ch[i];
  memset(pre, -1, sizeof pre);
  memset(nxt, -1, sizeof nxt);
  for (int i = n - 1; i >= 0; i--) {
    pre[ch[i] - 'a'] = i;
    for (int j = 0; j < 3; j++) nxt[j][i] = pre[j];
  }
  int mn = n / 3 + 1;
  dp[0][0][0][0] = 1;
  for (int i = 0; i < t; i++)
    for (int j = 0; j <= mn; j++)
      for (int k = 0; k <= mn; k++)
        for (int o = 0; o <= mn; o++) {
          if (!dp[i][j][k][o]) continue;
          int to;
          to = nxt[0][i];
          if (to != -1)
            dp[to][j + 1][k][o] =
                (dp[to][j + 1][k][o] + dp[i][j][k][o]) % 51123987;
          to = nxt[1][i];
          if (to != -1)
            dp[to][j][k + 1][o] =
                (dp[to][j][k + 1][o] + dp[i][j][k][o]) % 51123987;
          to = nxt[2][i];
          if (to != -1)
            dp[to][j][k][o + 1] =
                (dp[to][j][k][o + 1] + dp[i][j][k][o]) % 51123987;
        }
  int ans = 0;
  if (n % 3 == 0) {
    for (int i = 0; i < t; i++)
      ans = (ans + dp[i][n / 3][n / 3][n / 3]) % 51123987;
  }
  if (n % 3 == 1) {
    for (int i = 0; i < t; i++) {
      ans = (ans + dp[i][n / 3 + 1][n / 3][n / 3]) % 51123987;
      ans = (ans + dp[i][n / 3][n / 3 + 1][n / 3]) % 51123987;
      ans = (ans + dp[i][n / 3][n / 3][n / 3 + 1]) % 51123987;
    }
  }
  if (n % 3 == 2) {
    for (int i = 0; i < t; i++) {
      ans = (ans + dp[i][n / 3 + 1][n / 3 + 1][n / 3]) % 51123987;
      ans = (ans + dp[i][n / 3][n / 3 + 1][n / 3 + 1]) % 51123987;
      ans = (ans + dp[i][n / 3 + 1][n / 3][n / 3 + 1]) % 51123987;
    }
  }
  printf("%d", ans);
}
