Problem:
 Score : 400 points    Problem Statement We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.  Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.  Find the number of such ways to paint the dominoes, modulo 1000000007 .  The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:   Each domino is represented by a different English letter (lowercase or uppercase).  The j -th character in S_i represents the domino that occupies the square at the i -th row from the top and j -th column from the left.       Constraints  1 \leq N \leq 52  |S_1| = |S_2| = N  S_1 and S_2 consist of lowercase and uppercase English letters.  S_1 and S_2 represent a valid arrangement of dominoes.         Input Input is given from Standard Input in the following format:  N  S_1  S_2       Output Print the number of such ways to paint the dominoes, modulo 1000000007 .        Sample Input 1 3
aab
ccb      Sample Output 1 6  There are six ways as shown below:        Sample Input 2 1
Z
Z      Sample Output 2 3  Note that it is not always necessary to use all the colors.       Sample Input 3 52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn      Sample Output 3 958681902

Solution (Python):
mod = 1000000007
n = int(input())
s1 = input()
s2 = input()

ans = 1
for i in range(n):
    if s1[i] == s2[i]:
        if i==0:
            ans *= 3
        else:
            if s1[i-1]==s2[i-1]:
                ans *= 2
            else:
                continue
    else:
        if i==0:
            ans *= 6
        else:
            if s1[i] == s1[i-1]:
                continue
            elif s1[i-1] == s2[i-1]:
                ans *=2
            else:
                ans *= 3
    ans = ans%mod
print(ans)

Solution (Python):
import sys


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    s1 = str(input().rstrip('\n'))
    s2 = str(input().rstrip('\n'))
    cur = 0
    cnt = 1
    f = True
    lv = True
    while cur < n:
        if s1[cur] == s2[cur]:
            
            if f:
                cnt = (cnt * 3) % mod
                f = False
            else:
                if lv:
                    cnt = (cnt * 2) % mod
                else:
                    cnt = (cnt * 1) % mod
            cur += 1
            lv = True
        elif s1[cur] == s1[cur+1] and s2[cur] == s2[cur+1]:
            
            if f:
                cnt = (cnt * 6) % mod
                f = False
            else:
                if lv:
                    cnt = (cnt * 2) % mod
                else:
                    cnt = (cnt * 3) % mod
            cur += 2
            lv = False
    print(cnt)


if __name__ == '__main__':
    solve()

Solution (Python):
MOD = 1000000007
N = int(input())
S1 = list(input())
S2 = list(input())

if S1[0] == S2[0]:
    ans = 3
    tmp = True 
    j = 1 
else:
    ans = 6
    tmp = False
    j = 2

while j < N:
    if S1[j] == S2[j]: 
        if tmp: 
            ans *= 2
        else: 
            ans *= 1
        tmp = True
        j += 1
    else: 
        if tmp: 
            ans *= 2
        else: 
            ans *= 3
        tmp = False
        j += 2
    ans %= MOD

print (ans)

Solution (Python):
n=int(input())
s=list(input())
v=[0]
for i in range(1,n):
    if s[i]==s[i-1]:
        del(v[-1])
        v.append(1)
        
    else:
        v.append(0)


if v[0]==0:
    ans=3
else:
    ans=6
for i in range(1,len(v)):
    if v[i-1]==0:
        ans*=2
    elif v[i]==1:
        ans*=3
    else:
        continue
    

mod = 1000000007

print(ans%mod)

Solution (Python):

import sys

MOD = 1000000007  


def solve(N: int, S: "List[str]"):
    S1, S2 = S
    prev = 0
    counter = 1
    i = 0
    while i<N:
        if S1[i] == S2[i]:
            if prev == 0:
                counter *= 3
            elif prev == 1:
                counter *= 2
            prev = 1
            i += 1
        else:
            if i > 0:
                if S1[i-1] == S1[i] and S2[i-1] == S2[i-1]:
                    i += 1
                    prev = 2
                    continue
            if prev == 0:
                counter *= 6
            elif prev == 1:
                counter *= 2
            elif prev == 2:
                counter *= 3
            prev = 2
            i += 1
        counter %= MOD
    print(counter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = [next(tokens) for _ in range(2)]  
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):

import sys

MOD = 1000000007  


def solve(N: int, S: "List[str]"):
    S1, S2 = S
    prev = 0
    counter = 1
    i = 0
    while i<N:
        if S1[i] == S2[i]:
            if prev == 0:
                counter *= 3
            elif prev == 1:
                counter *= 2
            prev = 1
            i += 1
        else:
            if i > 0:
                if S1[i-1] == S1[i] and S2[i-1] == S2[i-1]:
                    i += 1
                    continue
            if prev == 0:
                counter *= 6
            elif prev == 1:
                counter *= 2
            elif prev == 2:
                counter *= 3
            prev = 2
            i += 1
        counter %= MOD
    print(counter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    S = [next(tokens) for _ in range(2)]  
    solve(N, S)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
S1=input()
S2=input()

mod=1000000007
if n==1:
  ans=3
else:
  if S1[0]==S2[0]:
    ans,i=3,1
  else:
    ans,i=6,2
  while i<n:
    if S1[i]==S2[i]:
      ans *= 2%mod if S1[i-1]==S2[i-1] else 1%mod
      i+=1
    else:
      ans *= 2%mod if S1[i-1]==S2[i-1] else 3%mod
      i+=2
print(ans%(mod))

Solution (Python):
N = int(input())
s1 = input()
s2 = input()
mod = 1000000007

if N == 1:
    answer = 3
elif N == 2:
    answer = 6
else:
    answer = 6
    for i in range(2, N):
        if s1[i] == s2[i]:
            if s1[i - 1] == s2[i - 1]:
                answer = answer * 2 % mod
            else:
                pass
        else:
            if s1[i - 1] == s2[i - 1]:
                answer = answer * 2 % mod
            elif s1[i - 1] != s1[i]:
                answer = answer * 3 % mod
            else:
                pass

print(answer)

Solution (Python):
n = int(input())
s1 = input()
s2 = input()

if s1[0] == s2[0]:
  result = 3
  last_pattern = "h"
  place = 1
else:
  result = 6
  last_pattern = "w"
  place = 2
while place < n:
  if last_pattern == "h":
    if s1[place] == s2[place]:
      result *= 2
      last_pattern = "h"
      place += 1
    else:
      result *= 2
      last_pattern = "w"
      place += 2
  else:
    if s1[place] == s2[place]:
      last_pattern = "h"
      place += 1
    else:
      result *= 3
      last_pattern = "w"
      place += 2
print(result % (7 + 10**9))

Solution (Python):
n,s,t=open(0)
a=3
for i in range(1,int(n)):
 if i<2or s[i-1]==t[i-1]:a*=2
 elif s[i-1]!=s[i]!=t[i]:a*=3
print(a%(10**9+7))

Solution (Python):
n,s,t=open(0)
a=3
for i in range(1,int(n)):
 if i<2or s[i-1]==t[i-1]:a*=2
 elif s[i-1]!=s[i]!=t[i]:a*=3
print(a%(10**9+7))

Solution (Python):
from itertools import*
_,*s=open(0)
s=[a[0]!=b[0]for a,b in zip(*map(groupby,s))]
c=3*2**s[0]
for a,b in zip(s,s[1:-1]):c=c*(a+2if b else(1^a)+1)%(10**9+7)
print(c)

Solution (Python):
MOD =1000000007 
N = int(input())
S1 = input()
S2 = input()

Data = []
i = 0
while i < N:
    if S1[i] == S2[i]:
        Data.append(1)
        i += 1
    else:
        Data.append(2)
        i += 2

ans = 0
for i in range(len(Data)):
    if i == 0:
        if Data[i] == 1:
            ans = 3
        else:
            ans = 6
    else:
        if Data[i] == 1:
            if Data[i-1] == 1:
                ans = (ans * 2) % MOD
            else:
                pass 
        else:
            if Data[i-1] == 1:
                ans = (ans * 2) % MOD
            else:
                ans = (ans * 3) % MOD
print(ans)

Solution (Python):



N = int(input())
txt = input()

pattern = []
j = 0

while j < N:
  if j == N-1 or txt[j] != txt[j+1]:
    pattern.append(1)
    j += 1
  else:
    pattern.append(2)
    j += 2


    
MOD = 10**9 + 7

ans = 3 if pattern[0] == 1 else 6
for j in range(1,len(pattern)):
  a,b = pattern[j-1:j+1]
  if (a,b) == (1,1):
    ans *= 2
  elif (a,b) == (1,2):
    ans *= 2
  elif (a,b) == (2,1):
    ans *= 1
  else:
    ans *= 3
  ans %= MOD

print(ans)

Solution (Python):
n = int(input())
S = list(input()) + [None]

ans = 1
i = 0
prev = None

while i < n:
    if S[i]==S[i+1]:
        if prev == None:
            ans *= 6
        elif prev == 0:
            ans *= 3
        else:
            ans *= 2
        prev = 0
        i += 2
    else:
        if prev == None:
            ans *= 3
        elif prev == 0:
            ans *= 1
        elif prev == 1:
            ans *= 2
        prev = 1
        i += 1
        
print(ans%1000000007)

Solution (Python):
n = int(input())
s = input()
S = input()
ans = 3
i = 0
if s[i] == S[i]:
    ans *= 1
    i += 1
else:
    ans *= 2
    i += 2
while i < n:
    if s[i] == S[i]:
        if s[i-1] == S[i-1]:ans *= 2;i += 1
        else:ans *= 1;i += 1
    else:
        if s[i-1] == S[i-1]:ans *= 2;i += 2
        else:ans *= 3;i += 2
print(ans % (10**9+7))

Solution (Python):
n=int(input())
a=list(input())
b=list(input())
ans=1
i=0
f=0
mod=1000000007
while i!=n:
    if i==0:
        if a[i]==b[i]:
            i+=1
            ans*=3
            ans%=mod
            f=1
        else:
            i+=2
            ans*=6
            ans%=mod
            f=2
    else:
        if a[i]==b[i] and f==1:
            i+=1
            ans*=2
            ans%=mod
            f=1
        elif a[i]==b[i] and f==2:
            i+=1
            f=1
        elif a[i]!=b[i] and f==1:
            i+=2
            ans*=2
            ans%=mod
            f=2
        else:
            i+=2
            ans*=3
            ans%=mod
            f=2
print(ans%mod)

Solution (Python):
n = int(input())
s1=input()
s2=input()
mod = 10**9+7

if s1[0]==s2[0]: res = 3
else: res = 6

for i in range(1,n):
    if s1[i-1] != s1[i]:
        if s1[i-1] != s2[i-1]:
            if s1[i] != s2[i]: res *= 3
        else: res *= 2

print(res % mod)

Solution (Python):
n = int(input())
s1=input()
s2=input()
mod = 10**9+7
l = []
for i in range(0,n-1):
    if s1[i] != s1[i+1]:
        if s1[i] != s2[i]:
            l.append(0)
        else:
            l.append(1)
if s1[n-1]==s2[n-1]:
    l.append(1)
else:
    l.append(0)
if l[0]:
    res = 3
else:
    res = 6

for i in range(1,len(l)):
    if l[i-1] and l[i]:
        res *= 2
    elif l[i-1] and  not l[i]:
        res *= 2
    elif not l[i-1] and not l[i]:
        res *= 3

print(res % mod)

Solution (Python):
n = int(input())
s = input()
S = input()
dp = [1]*(n+1)
if s[0]== S[0]:ans = 3;i = 1;dp[0] = 1
else:ans = 6;i = 2;dp[1] = 2
while i < n:
    if s[i] == S[i]:
        dp[i] = 1
        if dp[i-1] == 1:ans *= 2
        else: ans *= 1
        i += 1
    else:
        if dp[i-1] == 2:ans *= 3
        else: ans *=2
        dp[i+1]= 2
        i += 2
print(ans%1000000007)

Solution (Python):
n=int(input())
s="1"+input()
a=[]
for i in range(n):
  if s[i]!=s[i+1]:a+=[1]
  else:a[-1]+=1
x=3*a[0]
for i in range(1,len(a)):
  if a[i-1]==a[i]==1:x*=2
  elif a[i-1]==a[i]==2:x*=3
  elif a[i]==1:x*=1
  else:x*=2
  x%=10**9+7
print(x)

Solution (Python):
N=int(input())

A=[[0]*2 for i in range(N)]

for i,x in enumerate(input()):
	A[i][0]=ord(x)
for i,x in enumerate(input()):
	A[i][1]=ord(x)

t=[]
f=True
for x,y in A:
	if f:
		if x==y:
			t.append(0)
		else:
			t.append(1)
			f=False
	else:
		f=True

ans=1
if t[0]==0:ans*=3
else:ans*=6


for i in range(1,len(t)):
	if t[i-1]==t[i]==0:
		ans=(ans*2)%(1000000007)
	elif t[i-1]==0 and t[i]==1:
		ans=(ans*2)%(1000000007)
	elif t[i-1]==1 and t[i]==1:
		ans=(ans*3)%(1000000007)

print(ans)

Solution (Python):
n=int(input())
s=str(input())
t=str(input())
listed=[]
cnt=0
while cnt<=n-1:
    if s[cnt]==t[cnt]:
        cnt+=1
        listed.append("T")
    elif s[cnt]==s[cnt+1] and t[cnt]==t[cnt+1]:
        cnt+=2
        listed.append("Y")

length=len(listed)
if listed[0]=="T":
    ans=3
elif listed[0]=="Y":
    ans=6
for i in range(0,length-1):
    
    if listed[i]=="Y" and listed[i+1]=="Y":
        ans=ans*3%(10**9+7)
    
    if listed[i]=="Y" and listed[i+1]=="T":
        ans=ans
        
    if listed[i]=="T" and listed[i+1]=="Y":
        ans=ans*2%(10**9+7)
        
        
    if listed[i]=="T" and listed[i+1]=="T":
        ans=ans*2%(10**9+7)
print(ans)

Solution (Python):
n=int(input())
s=input()
t=input()
p=0
i=0
ans=1
while i<n:
	if s[i]==t[i]:
		if p==0:
			ans*=3
		elif p=="x":
			ans*=2
		p="x"
		ans%=(10**9+7)
		i+=1
	else:
		if p==0:
			ans*=6
		elif p=="x":
			ans*=2
		else:
			ans*=3
		p="y"
		ans%=(10**9+7)
		i+=2
print(ans)

Solution (Python):
import numpy as np
n = int(input())
s1 = list(input())
s2 = list(input())

domino = np.array([s1, s2])
array = []
point = 0
while True:
    col = domino[:, point]
    if col[0] == col[1]:
        array.append(0) 
        point += 1
    else:
        array.append(1) 
        point += 2
    if point >= n:
        break

total = 1
for i in range(len(array)):
    if i == 0:
        if array[i] == 0:
            total *= 3
        else:
            total *= 6
    else:
        if array[i] == 0:
            if array[i-1] == 0:
                total *= 2
            else:
                total *= 1
        else:
            if array[i-1] == 0:
                total *= 2
            else:
                total *= 3
print(total % (10**9 + 7))

Solution (Python):
N = int(input())
S1 = list(input())
S2 = list(input())

if N == 1:
    print(3)
else:
    if S1[0] == S1[1]:
        left_domino = 0
        ans, i = 6, 2    
    else:
        left_domino = 1
        ans, i = 3, 1

    while i < N-1:
        if S1[i] == S1[i+1]:
            if left_domino == 0:
                ans *= 3
            else:
                ans *= 2
            left_domino = 0
            i += 2
        else:
            if left_domino != 0:
                ans *= 2
            left_domino = 1
            i += 1

    if S1[-1] == S2[-1] and left_domino != 0:
        ans *= 2

    print(ans % 1000000007)

Solution (Python):

N = int(input())
S1 = input()
S2 = input()


state = 0
now = 0
ans = 1

while now < N:

    if state == 0 and S1[now] == S2[now]:
        ans *= 3
        now += 1
        state = 1

    elif state == 0 and S1[now] != S2[now]:
        ans *= 6
        now += 2
        state = 2

    elif state == 1 and S1[now] == S2[now]:
        ans *= 2
        now += 1
        state = 1

    elif state == 1 and S1[now] != S2[now]:
        ans *= 2
        now += 2
        state = 2

    elif state == 2 and S1[now] == S2[now]:
        ans *= 1
        now += 1
        state = 1

    elif state == 2 and S1[now] != S2[now]:
        ans *= 3
        now += 2
        state = 2

    ans %= (10 ** 9 + 7)

print (ans)

Solution (Python):
n = int(input())
s0 = input().rstrip()
s1 = input().rstrip()
v = True
index = 0
res = 3
while index < n:
    vv = s0[index] == s1[index]
    if index == 0 and vv:
        pass
    elif v:
        res *= 2
    elif not v and not vv:
        res *= 3
    index += 1 if vv else 2
    v = vv
MOD = 10**9 + 7
print(res % MOD)

Solution (Python):
n=int(input())
s=input()
s="?"+input()
mod=10**9+7

col=[]
for i in range(n):
    if s[i+1]!=s[i]:
        col.append(1)
    else:
        col[-1]+=1
    
for i in range(len(col)):
    now=col[i]
    if i==0:
        ans=3 if now==1 else 6
        continue
    bef=col[i-1]
    if bef==1:
        ans*=2
        ans%=mod
    elif bef==2 and now==2:
        ans*=3
        ans%=mod

print(ans)

Solution (Python):
N = int(input())
S_1 = input()
S_2 = input()

is_vertical = []

i = 0
while i < N:
    if i < N - 1 and S_1[i] == S_1[i + 1]:
        is_vertical.append(False)
        i += 2
    else:
        is_vertical.append(True)
        i += 1

res = 3 if is_vertical[0] else 6

for i in range(1, len(is_vertical)):
    if is_vertical[i] and is_vertical[i - 1]:
        res *= 2
    elif is_vertical[i] and not is_vertical[i - 1]:
        res *= 1
    elif not is_vertical[i] and is_vertical[i - 1]:
        res *= 2
    else:
        res *= 3

print(res % (10 ** 9 + 7))

Solution (Python):
N = int(input())
S1 = input()
S2 = input()

mod = 10**9+7
flag = 0
joutai = 0

if S1[0]==S2[0]:
    ans = 3
    joutai = 1
else:
    ans = 6
    flag = 1
    joutai = 2

for i in range(1,N):
    if flag == 0:
        if S1[i]==S2[i]:
            if joutai == 1:
                ans = (ans*2)%mod
            joutai = 1
        else:
            if joutai == 1:
                ans = (ans*2)%mod
            else:
                ans = (ans*3)%mod
            joutai = 2
            flag = 1
    else:
        flag = 0
print(ans)

Solution (Python):
def main():
    n = int(input())
    ls1 = list(input())
    ls2 = list(input())

    if ls1[0]==ls2[0]:
        ans = 3
        i =1
        flg = "b"
    else:
        ans = 6
        flg = "a"
        i =2

    while 1:

        if i >= n:
            break

        if ls1[i]==ls2[i]:
            if flg == "a":
                flg = "b"
            else:
                ans = (ans*2)%(10**9+7)

            i += 1

        else:
            if flg == "a":
                ans = (ans*3)%(10**9+7)
            else:
                ans = (ans*2)%(10**9+7)
                flg = "a"

            i += 2
        if i >= n:
            break

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
s = [input() for _ in range(2)]

if s[0][0] == s[1][0]:
    c = 3
    last = 'x'
    p = 1
else:
    c = 6
    last = 'y'
    p = 2

while p < n:
    if s[0][p] == s[1][p]:
        if last == 'x':
            c *= 2
        else:
            c *= 1
        last = 'x'
        p += 1
    else:
        if last == 'x':
            c *= 2
        else:
            c *= 3
        last = 'y'
        p += 2
    c %= 10 ** 9 + 7

print(c)

Solution (Python):
n = int(input())
s = input()+"?"
i = 0
ref = []
mod = 10**9+7
while i<n:
	if s[i]==s[i+1]:
		ref += [1]
		i += 2
	else:
		ref += [0]
		i += 1
ans = 6 if ref[0] else 3
for i,j in zip(ref,ref[1:]):
	if (1-i)&(1-j):
		ans *= 2
	elif i&(1-j):
		ans *= 1
	elif (1-i)&j:
		ans *= 2
	else:
		ans *= 3
	ans %= mod
print(ans)

Solution (Python):
N = int(input())
S1 = input()
S2 = input()

s = sorted(set(S1) | set(S2))

c = [[None, None]] * N
"""
rrB
ggB

r2
r2

"""
cnt = 1
if S1[0] == S2[0]:
    
    
    cnt = 3 
    i = 1
else:
    
    
    
    cnt = 6
    i = 2

while i < N:
    m = 0
    if S1[i] == S2[i]:
        if S1[i-1] == S2[i-1]:
            
            
            m= 2
        else:
            
            
            m= 1
        i+=1
    else:
        if S1[i-1] == S2[i-1]:
            
            
            m= 2
        else:
            
            
            
            m= 3
        i+=2
    cnt = (cnt * m) % 1000000007
    
    
    
    
print(cnt)

Solution (Python):
def main():

    N = int(input())
    S1 = input()
    S2 = input()

    mod = pow(10, 9) + 7
    i = 0
    count = 1
    isSame = True
    while i < N:
        if S1[i] == S2[i]:
            if isSame:
                if i == 0:
                    count *= 3
                else:
                    count *= 2
            isSame = True
            i += 1
        else:
            if isSame:
                if i == 0:
                    count *= 6
                else:
                    count *= 2
            else:
                count *= 3
            isSame = False
            i += 2
        count %= mod
    return count

if __name__ == '__main__':
    print(main())

Solution (Python):
mod=1000000007
n=int(input())
s=input()

d=[0]
for i in range(1,n):
    if s[i-1]==s[i]:
        d[-1]+=1
    else:
        d.append(0)
ans=[3,6][d[0]]
for i in range(1,len(d)):
    t=1
    if d[i-1]==d[i]==1:
        t=3
    elif d[i-1]!=1:
        t=2
    ans=(ans*t)%mod
print(ans)

Solution (Python):
mod=1000000007
n=int(input())
s=input()

d=[0]
for i in range(1,n):
    if s[i-1]==s[i]:
        d[-1]+=1
    else:
        d.append(0)
ans=[3,6][d[0]]
for i in range(1,len(d)):
    if d[i-1]==d[i]==1:
        t=3
    elif d[i-1]==1:
        t=1
    else:
        t=2
    ans=(ans*t)%mod
print(ans)

Solution (Python):
mod=1000000007
n=int(input())
a=input()

d=[0]
for i in range(1,n):
    if a[i-1]==a[i]:
        d[-1]+=1
    else:
        d.append(0)
ans=[3,6][d[0]]
for i in range(1,len(d)):
    if d[i-1]==d[i]==1:
        t=3
    elif d[i-1]==1 and d[i]==0:
        t=1
    else:
        t=2
    ans=(ans*t)%mod
print(ans)

Solution (Python):
mod=1000000007
n=int(input())
a=input()



d=[1]
for i in range(1,n):
    if a[i-1]==a[i]:
        d[-1]+=1
    else:
        d.append(1)
ans=[3,6][d[0]-1]
for i in range(1,len(d)):
    if d[i-1]==d[i]==2:
        t=3
    elif d[i-1]==2 and d[i]==1:
        t=1
    else:
        t=2
    ans=(ans*t)%mod
print(ans)

Solution (Python):
mod=1000000007
n=int(input())
a=input()
b=input()


d=[1]
for i in range(1,n):
    if a[i-1]==a[i]:
        d[-1]+=1
    else:
        d.append(1)
ans=[3,6][d[0]-1]
for i in range(1,len(d)):
    if d[i-1]==d[i]==1:
        t=2
    elif d[i-1]==d[i]==2:
        t=3
    elif d[i-1]==2 and d[i]==1:
        t=1
    else:
        t=2
    ans=(ans*t)%mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    s = [input().strip() for i in range(2)]

    MOD = 1000000007
    if N == 1:
        print(3)
        return
    ans = 0
    pre = -1
    i = 0
    while i < N-1:
        if s[0][i] == s[0][i+1]:
            if ans == 0:
                ans = 6
            else:
                if pre == 0:
                    ans *= 3
                    ans %= MOD
                else:
                    ans *= 2
                    ans %= MOD
            i += 2
            pre = 0
        else:
            if ans == 0:
                ans = 3
            else:
                if pre == 0:
                    pass
                else:
                    ans *= 2
                    ans %= MOD
            i += 1
            pre = 1
    if i == N-1:
        if pre == 1:
            ans *= 2
            ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
s=input()
p,q,r=0,1,0
for i in range(n):
    if r==0:
        if i+1<n and s[i]==s[i+1]:
            if p==0:q*=6
            elif p==1:q*=3
            else:q*=2
            r=1
            p=1
        else:
            if p==0:q*=3
            elif p==2:q*=2
            p=2
    else:
        r=0
print(q%(10**9+7))

Solution (Python):
n = int(input())
s1 = input()
s2 = input()
mod = 1000000007

if n == 1 or s1[0] != s1[1]:
    ans = 3
    ind = 1
    mae = 1
else:
    ans = 6
    ind = 2
    mae = 2

while ind < n-1:
    if s1[ind] == s1[ind + 1]:
        if mae == 2:
            ans = (ans * 3) % mod
        else:
            ans = (ans * 2) % mod
        ind += 2
        mae = 2
    else:
        if mae == 2:
            ans = (ans * 1) % mod
        else:
            ans = (ans * 2) % mod
        ind += 1
        mae = 1

if ind == n-1:
    if mae == 2:
        ans = (ans * 1) % mod
    else:
        ans = (ans * 2) % mod

print(ans % mod)

Solution (Python):

def main():
    """
    Vertical (V): a
                  a
    Horizontal (H): aa
                    bb
    """
    N = int(input())
    S1 = input().rstrip()
    S2 = input().rstrip()
    MOD = 10 ** 9 + 7
    a, b = S1[0], S2[0]
    ans = 3 if a == b else 6
    for i, j in zip(S1[1:], S2[1:]):
        if a != i and b != j:  
            if a == b:  
                ans *= 2
            else:  
                ans *= 1 if i == j else 3
            ans %= MOD
        a, b = i, j
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
alp = [chr(ord('a') + i) for i in range(26)]


def A():
    x, a, b = LI()
    if abs(x - a) > abs(x - b):
        print("B")
    else:
        print("A")
    return


def B():
    s = S()
    s = set(s)
    for i in s:
        alp.remove(i)
    if len(alp) == 0:
        print("None")
    else:
        print(alp[0])
    return


def C():
    n = II()
    a = LI()
    i = 1
    a.sort(reverse=True)
    ans = []
    while i < n:
        if a[i] == a[i - 1]:
            ans.append(a[i])
            if len(ans) == 2:
                print(ans[0] * ans[1])
                return
            i += 2
        else:
            i += 1
    print(0)


def D():
    n = II()
    s1 = S()
    s2 = S()
    i = 0
    f = 0
    if s1[i] == s2[i]:
        ans = 3
        i += 1
        f = 1
    else:
        ans = 6
        i += 2
    while i < n:
        if s1[i] == s2[i]:
            ans *= 2 if f else 1
            ans %= mod
            i += 1
            f = 1
            continue
        i += 2
        ans *= 2 if f else 3
        f = 0
        ans %= mod
    print(ans%mod)
    return


if __name__ == '__main__':
    D()

Solution (Python):
N = int(input())
S1 = list(input())
S2 = list(input())

mod = int(1E9+7)

if S1[0] == S2[0]:
    ans = 3
else:
    ans = 6
for i in range(N-1):
    if S1[i] != S1[i+1] and S1[i] == S2[i]:
        ans *= 2
    elif S1[i] == S2[i] and S1[i+1] == S2[i+1]:
        ans *= 3
    elif S1[i] != S2[i] and S1[i] != S1[i+1] and S2[i] != S2[i+1] and S1[i+1] != S2[i+1]:
        ans *= 3
    ans = ans % mod

print(ans)

Solution (Python):
N = int(input())
S1 = input()
S2 = input()
r = 1
s = 0
for i in range(N):
  p = s
  if s == 0:
    if S1[i] == S2[i]:
      r = r*3%1000000007
      s = 1
    else:
      r = r*6%1000000007
      s = 2
  elif s == 1:
    if S1[i] == S2[i]:
      r = r*2%1000000007
      s = 1
    else:
      r = r*2%1000000007
      s = 2
  elif s == 2:
    s = 3
  elif s == 3:
    if S1[i] == S2[i]:
      r = r*1%1000000007
      s = 1
    else:
      r = r*3%1000000007
      s = 2
  
print(r)

Solution (Python):
N = int(input())
S1 = input()
S2 = input()
mod = 1000000007
j = 0
if S1[0] == S2[0]:
    ans = 3
else:
    ans = 6
while 1:
    if S1[j] == S2[j]:
        j += 1
        if j >= N:
            break
        ans = ans*2
    else:
        j += 2
        if j >= N:
            break
        if S1[j] != S2[j]:
            ans = ans*3
print(ans%mod)

Solution (Python):
N = int(input())
S1 = input()
S2 = input()
mod = 1000000007
j = 0
if S1[0] == S2[0]:
    ans = 3
else:
    ans = 6
while 1:
    if S1[j] == S2[j]:
        j += 1
        if j >= N:
            break
        ans = ans*2
    else:
        j += 2
        if j >= N:
            break
        if S1[j] != S2[j]:
            ans = ans*3
print(ans%mod)

Solution (Python):
N = int(input())
S1 = input()
S2 = input()
mod = 1000000007
j = 0
if S1[0] == S2[0]:
    ans = 3
else:
    ans = 6
while 1:
    if S1[j] == S2[j]:
        j += 1
        if j >= N:
            break
        ans = ans*2
    else:
        j += 2
        if j >= N:
            break
        if S1[j] != S2[j]:
            ans = ans*3
print(ans%mod)

Solution (Python):
N = int(input())
S1 = input()
S2 = input()
mod = 10 ** 9 + 7


if S1[0] == S2[0] :
    ret = 3
    pre = False
    i = 1
else :
    ret = 6
    pre = True
    i = 2

while i < N :
    cur = (S1[i] != S2[i])
    if not pre :
        ret *= 2
    elif pre and cur :
        ret *= 3
    
    ret %= mod
    
    if cur :
        i += 2
    else :
        i += 1
    pre = cur
    
print(ret)

Solution (Python):
class Mint:
    MOD = 1000000007  
    CACHE_FACTORIALS = [1, 1]

    def __init__(self, v):
        if self.__isally(v):
            self.v = v.v % self.MOD
        else:
            self.v = v % self.MOD

    @property
    def inv(self):
        return Mint(self.__minv(self.v))

    @classmethod
    def factorial(cls, v):
        for i in range(len(cls.CACHE_FACTORIALS), int(v) + 1):
            cls.CACHE_FACTORIALS.append(cls.CACHE_FACTORIALS[-1] * i % cls.MOD)
        return Mint(cls.CACHE_FACTORIALS[int(v)])

    @classmethod
    def perm(cls, n, r):
        if n < r or r < 0:
            return 0
        return cls.factorial(n) // cls.factorial(n - r)

    @classmethod
    def comb(cls, n, r):
        if n < r or r < 0:
            return 0
        return cls.perm(n, r) // cls.factorial(r)

    @classmethod
    def __isally(cls, v) -> bool:
        return isinstance(v, cls)

    @classmethod
    def __minv(cls, v) -> int:
        return pow(v, cls.MOD - 2, cls.MOD)

    @classmethod
    def __mpow(cls, v, w) -> int:
        return pow(v, w, cls.MOD)

    def __str__(self):
        return str(self.v)
    __repr__ = __str__

    def __int__(self):
        return self.v

    def __eq__(self, w):
        return self.v == w.v if self.__isally(w) else self.v == w

    def __add__(self, w):
        return Mint(self.v + w.v) if self.__isally(w) else Mint(self.v + w)
    __radd__ = __add__

    def __sub__(self, w):
        return Mint(self.v - w.v) if self.__isally(w) else Mint(self.v - w)

    def __rsub__(self, u):
        return Mint(u.v - self.v) if self.__isally(u) else Mint(u - self.v)

    def __mul__(self, w):
        return Mint(self.v * w.v) if self.__isally(w) else Mint(self.v * w)
    __rmul__ = __mul__

    def __floordiv__(self, w):
        return Mint(self.v * self.__minv(w.v)) if self.__isally(w) else \
            Mint(self.v * self.__minv(w))

    def __rfloordiv__(self, u):
        return Mint(u.v * self.__minv(self.v)) if self.__isally(u) else \
            Mint(u * self.__minv(self.v))

    def __pow__(self, w):
        return Mint(self.__mpow(self.v, w.v)) if self.__isally(w) else \
            Mint(self.__mpow(self.v, w))

    def __rpow__(self, u):
        return Mint(self.__mpow(u.v, self.v)) if self.__isally(u) else \
            Mint(self.__mpow(u, self.v))


N = int(input())
S1 = input()
S2 = input()
if S1[0] == S2[0]:
    ans = Mint(3)
    j = 1
else:
    ans = Mint(6)
    j = 2
while j < N:
    a = S1[j] == S2[j]
    b = S1[j-1] == S2[j-1]
    if a:
        ans *= 2 if b else 1
        j += 1
    else:
        ans *= 2 if b else 3
        j += 2

print(ans)