n = int(input()) s = input() mod = 10**9 + 7 t = 1 DP = [0 for _ in range(3)] NDP = [0 for _ in range(3)] for i in range(n): idx = ord(s[i]) - ord('A') for j in range(3): if j == idx: NDP[j] = DP[j] else: NDP[j] = (DP[3 - j - idx] + t) % mod DP = NDP[:] t *= 2 t %= mod print(DP[0])