#include <bits/stdc++.h>
int r[151][52][52][52];
std::string s;
int len;
int nextA[151];
int nextB[151];
int nextC[151];
void add(int &a, int t) { a = (a + t) % 51123987; }
int f(int pos, int a, int b, int c) {
  if (a == 0 && b == 0 && c == 0) return 1;
  int &res = r[pos][a][b][c];
  if (res >= 0) return res;
  res = 0;
  if (nextA[pos] < len && a > 0) add(res, f(nextA[pos], a - 1, b, c));
  if (nextB[pos] < len && b > 0) add(res, f(nextB[pos], a, b - 1, c));
  if (nextC[pos] < len && c > 0) add(res, f(nextC[pos], a, b, c - 1));
  return res;
}
int main() {
  int n;
  std::cin >> len;
  std::cin >> s;
  memset(r, -1, sizeof(r));
  int a = len, b = len, c = len;
  for (int i = len - 1; i >= 0; --i) {
    if (s[i] == 'a')
      a = i;
    else if (s[i] == 'b')
      b = i;
    else
      c = i;
    nextA[i] = a;
    nextB[i] = b;
    nextC[i] = c;
  }
  int res = 0;
  for (int a = 0; a <= (len + 2) / 3; ++a)
    for (int b = std::max(a - 1, 0); b <= std::min(a + 1, (len + 2) / 3); ++b)
      for (int c = std::max(a - 1, 0); c <= std::min(a + 1, (len + 2) / 3); ++c)
        if (std::abs(b - c) <= 1 && a + b + c == len) {
          add(res, f(0, a, b, c));
        }
  std::cout << res << "\n";
  return 0;
}
