#include <bits/stdc++.h>
using namespace std;
inline bool SR(int &x) { return scanf("%d", &x) == 1; }
inline bool SR(long long &x) { return scanf("%lld", &x) == 1; }
inline bool SR(double &x) { return scanf("%lf", &x); }
inline bool SR(char *s) { return scanf("%s", s); }
inline bool RI() { return 1; }
template <typename I, typename... T>
bool RI(I &x, T &...tail) {
  return SR(x) && RI(tail...);
}
inline void SP(const int x) { printf("%d", x); }
inline void SP(const long long x) { printf("%lld", x); }
inline void SP(const double x) { printf("%.10f", x); }
inline void SP(const char *s) { printf("%s", s); }
inline void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
  SP(x);
  if (sizeof...(tail)) putchar(' ');
  PL(tail...);
}
template <typename I>
inline void _DOING(const char *s, I &&x) {
  cerr << s << " = " << x << '\n';
}
template <typename I, typename... T>
void _DOING(const char *s, I &&x, T &&...tail) {
  int c = 0;
  while (*s != ',' || c != 0) {
    if (*s == '(' || *s == '[' || *s == '{') c++;
    if (*s == ')' || *s == ']' || *s == '}') c--;
    cerr << *s++;
  }
  cerr << " = " << x << " , ";
  _DOING(s + 1, tail...);
}
const int INF = 0x3f3f3f3f;
const long long INF64 = 0x3f3f3f3f3f3f3f3fll;
inline int RAND() {
  static int x = 880301;
  return x = (x * 0xdefacedll + 1ll) % 0x7fffffffll;
}
const int MAX_N = 160 + 7;
const int MOD = 51123987;
int last[3];
int nxt[MAX_N][3];
char str[MAX_N];
int dp[MAX_N][MAX_N / 3][MAX_N / 3][MAX_N / 3];
int main() {
  int n;
  RI(n, str);
  memset(last, (-1), sizeof(last));
  for (int i = (n)-1; i >= 0; i--) {
    last[str[i] - 'a'] = i;
    nxt[i][0] = last[0];
    nxt[i][1] = last[1];
    nxt[i][2] = last[2];
  }
  dp[0][0][0][0] = 1;
  int ans = 0;
  int bd = (n + 5) / 3;
  for (int i = 0; i < int(n); i++) {
    for (int a = (0); a <= int(bd); a++) {
      for (int b = (0); b <= int(bd); b++) {
        if (a + b > n) break;
        for (int c = (0); c <= int(bd); c++) {
          if (a + b + c > n) break;
          if (!dp[i][a][b][c]) continue;
          if (~nxt[i][0]) (dp[nxt[i][0]][a + 1][b][c] += dp[i][a][b][c]) %= MOD;
          if (~nxt[i][1]) (dp[nxt[i][1]][a][b + 1][c] += dp[i][a][b][c]) %= MOD;
          if (~nxt[i][2]) (dp[nxt[i][2]][a][b][c + 1] += dp[i][a][b][c]) %= MOD;
          if (a + b + c == n && abs(a - b) <= 1 && abs(a - c) <= 1 &&
              abs(b - c) <= 1) {
            ans = ans + dp[i][a][b][c];
            if (ans >= MOD) ans -= MOD;
          }
        }
      }
    }
  }
  PL(ans);
  return 0;
}
