MOD = 10 ** 9 + 7 n = int(input()) C = [0] for _ in range(n): C.append(C[-1] * 2 + 1) S = input().rstrip("A") if not S: print(0) exit() if len(S) == 1: print(1) exit() b = 0 ans = 0 for i in range(len(S) - 1, -1, -1): s = "ABC".index(S[i]) if s != b: ans += C[i] + 1 ans %= MOD b = 3 ^ b ^ s print(ans)