#include <bits/stdc++.h>
using namespace std;
const int oo = 1 << 30;
const double PI = M_PI;
const double EPS = 1e-15;
const int MaxN = 155;
const long long MOD = 51123987LL;
int n;
long long dp[MaxN / 3][MaxN / 3][MaxN / 3];
long long last[3][MaxN / 3][MaxN / 3][MaxN / 3];
long long answer;
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  string str;
  cin >> str;
  string second = (string) "@" + str[0];
  for (int i = 1; i < n; i++)
    if (str[i] != str[i - 1]) second = second + str[i];
  int n3 = (n + 2) / 3;
  last[0][0][0][0] = 1;
  last[1][0][0][0] = 1;
  last[2][0][0][0] = 1;
  for (int i = 1; i < (int)second.length(); i++) {
    for (int ka = 0; ka <= n3; ka++)
      for (int kb = 0; kb <= n3; kb++)
        for (int kc = 0; kc <= n3; kc++) {
          if ((second[i] == 'a' && ka == 0) || (second[i] == 'b' && kb == 0) ||
              (second[i] == 'c' && kc == 0))
            dp[ka][kb][kc] = 0;
          else {
            if (second[i] == 'a') dp[ka][kb][kc] = last[0][ka - 1][kb][kc];
            if (second[i] == 'b') dp[ka][kb][kc] = last[1][ka][kb - 1][kc];
            if (second[i] == 'c') dp[ka][kb][kc] = last[2][ka][kb][kc - 1];
          }
          if (ka + kb + kc == n && abs(ka - kb) <= 1 && abs(ka - kc) <= 1 &&
              abs(kb - kc) <= 1)
            answer = (answer + dp[ka][kb][kc]) % MOD;
          last[second[i] - 'a'][ka][kb][kc] =
              (last[second[i] - 'a'][ka][kb][kc] + dp[ka][kb][kc]) % MOD;
        }
    for (int ka = 0; ka <= n3; ka++)
      for (int kb = 0; kb <= n3; kb++)
        for (int kc = 0; kc <= n3; kc++) {
          last[0][ka][kb][kc] = (last[0][ka][kb][kc] + dp[ka][kb][kc]) % MOD;
          last[1][ka][kb][kc] = (last[1][ka][kb][kc] + dp[ka][kb][kc]) % MOD;
          last[2][ka][kb][kc] = (last[2][ka][kb][kc] + dp[ka][kb][kc]) % MOD;
          last[second[i] - 'a'][ka][kb][kc] = 0;
        }
  }
  cout << answer << endl;
  return 0;
}
