input() dp = [0] * 3 w = 0 for c in input(): p = ord(c) - ord('A') dpnxt = [max(dp) * 2 + w * 2 + 100] * 3 for d in range(3): if d == p: dpnxt[d] = min(dpnxt[d], dp[d]) else: dpnxt[p] = min(dpnxt[p], dp[d] + w) dpnxt[d ^ p ^ 3] = min(dpnxt[d ^ p ^ 3], dp[d] + w + 1) dpnxt[d] = min(dpnxt[d], dp[d] + w * 2 + 1) dp = dpnxt.copy() w = w * 2 + 1 print(dp[0] % 1000000007)