#include #include using namespace std; using mint = atcoder::modint1000000007; const int CombMAX = 3000000; std::vector fac(CombMAX + 1), finv(CombMAX + 1), inv(CombMAX + 1); struct Combinationinit { Combinationinit() { const int MOD = mint::mod(); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i = 2; i <= CombMAX; i++) { fac[i] = fac[i - 1] * i; inv[i] = MOD - inv[MOD % i] * (MOD / i); finv[i] = finv[i - 1] * inv[i]; } } } Combinationinit_; int main() { int n; string s; cin >> n >> s; int count_a = count(s.begin(), s.end(), 'A'); int count_b = count(s.begin(), s.end(), 'B'); int count_c = count(s.begin(), s.end(), 'C'); s += s; bool can_operation = 0, exist_a = 0, exist_ab = 0; for(auto &c : s) { if(c == 'A') exist_a = 1; if(c == 'B' and exist_a) exist_ab = 1; if(c == 'C' and exist_ab) can_operation = 1; } if(can_operation) { mint res = fac[n] * finv[count_a] * finv[count_b] * finv[count_c] - n; cout << res.val() << endl; } else cout << 1 << endl; }