#include <bits/stdc++.h>
using namespace std;
int dp[158][55][55][55];
string ss;
int n;
void add(int &x, int y) {
  x += y;
  if (x >= 51123987) x -= 51123987;
}
int bucket[158];
int nxt[158][8];
int main() {
  cin >> n >> ss;
  bucket[0] = bucket[1] = bucket[2] = ss.size() + 5;
  for (int i = 0; i < ss.size() - 1; i++)
    if (ss[i] == ss[i + 1]) ss = ss.substr(0, i + 1) + ss.substr(i + 2), i--;
  for (int i = ss.size(); i >= 0; i--) {
    if (i < ss.size()) bucket[ss[i] - 'a'] = i;
    for (int j = 0; j < 3; j++) nxt[i][j] = bucket[j];
  }
  dp[0][0][0][0] = 1;
  int now = 0;
  int ans = 0;
  for (int i = 0; i <= ss.size(); i++) {
    for (int a = 0; a <= n / 3 + 1; a++) {
      for (int b = 0; b <= n / 3 + 1; b++) {
        for (int c = 0; c <= n / 3 + 1; c++) {
          add(dp[nxt[i][0]][a + 1][b][c], dp[i][a][b][c]);
          add(dp[nxt[i][1]][a][b + 1][c], dp[i][a][b][c]);
          add(dp[nxt[i][2]][a][b][c + 1], dp[i][a][b][c]);
          if (abs(a - b) <= 1 and abs(a - c) <= 1 and abs(b - c) <= 1 and
              a + b + c == n)
            add(ans, dp[i][a][b][c]);
        }
      }
    }
  }
  cout << ans;
  return 0;
}
