#include <bits/stdc++.h>
using namespace std;
int n, l, dp[152][52][52][52], nxt[3][152], ans;
string s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  for (int i = n; i; i--) {
    for (int j = 0; j < 3; j++) nxt[j][i] = nxt[j][i + 1];
    nxt[s[i - 1] - 'a'][i] = i;
  }
  dp[1][0][0][0] = 1;
  for (int it = 1; it <= n; it++)
    for (int i = 0; i <= (n + 2) / 3; i++)
      for (int j = 0; j <= (n + 2) / 3; j++)
        for (int k = 0; k <= (n + 2) / 3; k++) {
          (ans += (i + j + k == n) * (abs(i - j) <= 1) * (abs(j - k) <= 1) *
                  (abs(i - k) <= 1) * dp[it][i][j][k]) %= 51123987;
          (dp[nxt[0][it]][i + 1][j][k] += dp[it][i][j][k]) %= 51123987;
          (dp[nxt[1][it]][i][j + 1][k] += dp[it][i][j][k]) %= 51123987;
          (dp[nxt[2][it]][i][j][k + 1] += dp[it][i][j][k]) %= 51123987;
        }
  cout << ans << endl;
  return 0;
}
