dp=[0,0,0] n=int(input()) s=input() p2=0 for i in range(n): dp2=[0,0,0] for j in range(3): if ord(s[i])==(ord('A')+j): dp2[j]=min(dp[j],dp[(j+1)%3]+p2,dp[(j+2)%3]+p2) else: dp2[j]=min(dp[j]+p2*2+1,dp[ord(s[i])-ord('A')]+p2*2+1,dp[3-j-(ord(s[i])-ord('A'))]+p2+1) dp=dp2 p2=p2*2+1 print(dp[0]%1000000007)