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



Sample input:
3
aab
ccb


Sample output:
6

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(input())
b=list(input())
ans=1
i=0
f=0
mod=1000000007
while k!=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*=2
            ans%=mod
            f=2
print(ans%mod)

Submission status: Runtime Error

Solution code:
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)
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)

Submission status: Runtime Error

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

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

    while 1:


        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()

Submission status: Runtime Error

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

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


        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()

Submission status: Runtime Error

Solution code:
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=2
    ans=(ans*t)%mod
print(ans)

Submission status: Runtime Error

Solution code:
mod=1000000007
n=int(input())
a=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)

Submission status: Runtime Error

Solution code:
l

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0,i##_max=(N);i<i##_max;++i)
#define repp(i,l,r) for(int i=(l),i##_max=(r);i<i##_max;++i)
#define per(i,N) for(int i=(N)-1;i>=0;--i)
#define perr(i,l,r) for(int i=r-1,i##_min(l);i>=i##_min;--i)
#define all(arr) (arr).begin(), (arr).end()
#define SP << " " <<
#define SPF << " "
#define SPEEDUP cin.tie(0);ios::sync_with_stdio(false);
#define MAX_I INT_MAX //1e9
#define MIN_I INT_MIN //-1e9
#define MAX_UI UINT_MAX //1e9
#define MAX_LL LLONG_MAX //1e18
#define MIN_LL LLONG_MIN //-1e18
#define MAX_ULL ULLONG_MAX //1e19
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<char,char> PCC;
typedef pair<ll,ll> PLL;
typedef pair<char,int> PCI;
typedef pair<int,char> PIC;
typedef pair<ll,int> PLI;
typedef pair<int,ll> PIL; 
typedef pair<ll,char> PLC; 
typedef pair<char,ll> PCL; 

inline void YesNo(bool b){ cout << (b?"Yes" : "No") << endl;}
inline void YESNO(bool b){ cout << (b?"YES" : "NO") << endl;}
inline void Yay(bool b){ cout << (b?"Yay!" : ":(") << endl;}

template<int MOD> struct Fp {
    ll val;
    constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};


//const int MOD = 998244353;
//const int MOD = 1e6 + 3;
const int MOD = 1e9 + 7;
const int N_MAX = 1e5+5;
//mint:mod演算のための型
using mint = Fp<MOD>;
//型変換
#define MINT (mint)

//mod MOD上での階乗
mint fact[N_MAX];
void init(){
  fact[0] = fact[1] = MINT 1;
  for(int i = 2; i < N_MAX; i++){
    fact[i] = fact[i-1] * i;
  }
}

//mod MOD上での逆元
inline mint inv(mint n){
  return modpow(n,MOD-2);
}

//二項係数の計算(mod MOD上)
inline mint COM(int n, int k){
  if(n<k) return 0;
  return fact[n]/(fact[k]*fact[n-k]);
}

int main(void){
  SPEEDUP
  cout << setprecision(15);
  int N;cin >> N;
  string s1,s2;cin >> s1 >> s2;
  mint ans = 3;
  int pre = (s1[0] == s2[0]) ? 1:2;
  ans*=pre;
  repp(i,1,N){
    if(pre==2)++i;
    if(pre == 1) ans*=2;
    else if(s1[i] != s2[i]) ans*=3;
    pre = (s1[i] == s2[i]) ? 1:2;
  }
  cout << ans << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
s = [input() for _ in range(2)]
if n == 1:
    print(3)
    exit()
law = 10 ** 9 + 7
dp = [[0 for _ in range(2)] for _ in range(n)] # 直前の色と同じかどうか
dp[0][0] = 3
dp[0][1] = 0
dp[1][0] = 0
dp[1][1] = 6
sideway = s[0][1] == s[0][2]
for i in range(2, n):
    if sideway:
        sideway = False
        if s[0][i-2] == s[1][i-2]: # |=
            dp[i][0] = 0
            dp[i][1] = sum(dp[i-1])
        else: # ==
            dp[i][0] = 0
            dp[i][1] = dp[i-1][0] * 2 + dp[i-1][1]
    else:
        if s[0][i] == s[1][i]:
            sideway = False
            if s[0][i-1] == s[1][i-1]: # ||
                dp[i][0] = 0
                dp[i][1] = sum(dp[i-1]) * 2
            else: # =|
                dp[i][0] = 0
                dp[i][1] = sum(dp[i-1])
        else:
            sideway = True
            if s[0][i-1] == s[1][i-1]: # |-
                dp[i][0] = 0
                dp[i][1] = sum(dp[i-1]) * 2
            else: # =-
                dp[i][0] = sum(dp[i-1])
                dp[i][1] = 2 * sum(dp[i-2]) - dp[i][0]
    dp[i][0] %= law
    dp[i][1] %= law
print(sum(dp[i-1]))

Submission status: Runtime Error

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

i = 1
if S1[0] == S2[0]:
    ans = 3
    state = 0
else:
    i+= 1
    ans = 6
    state = 1
mod  = 10**9 + 7
while N > i:
    if S1[i] != S2[i] and state == 0:
        ans *= 2
        i+=2
        state = 1
    elif S1[i] != S2[i] and state == 1:
        ans *= 3
        i+=2
    elif S1[i] == S2[i] and state == 0:
        ans *= 2
        i+= 1
    elif S1[i] == S2[i] and state == 1:
        ans *= 1
        i+=1
        state = 0

    ans %= mod
print(ans)
~

Submission status: Runtime Error

Solution code:
n = int(input())
s1 = input()
s2 = input()
cnt = 1
i = 0
while 1:
    if i == 0:
        if s1[i] == s2[i]:
            cnt *= 3
            s = 0
        else:
            cnt *= 6
            i += 1
            s = 1
    elif s1[i] == s2[i]:
        if s == 0:
            cnt *= 2
        else:
            cnt *= 1
        s = 0
    else:
        if s == 0:
            cnt *= 2
        else:
            cnt *= 3
        s = 1
    i += 1
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
S = [input() for _ in range(2)]
M = 1000000007

if S[0][0] == S[1][0]:
    last_kind = 0
    ans = 3
    i = 1
else:
    last_kind = 1
    ans = 6
    i = 2
while i < N:
    if last_kind == 0 and S[0][i] == S[1][i]:
        ans *= 2
        last_kind = 0
        i += 1
    elif last_kind == 1 and S[0][i] == S[1][i]:
        last_kind = 0
        i += 1
    elif last_kind == 0 and S[0][i] == S[0][i+1]:
        ans *= 2
        last_kind = 1
        i += 2
    elif last_kind == 1 and S[0][i] == S[0][i+1]:
        ans *= 3
        last_kind = 1
        i += 
    ans %= M

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = [input() for _ in range(2)]
M = 1000000007

if S[0][0] == S[1][0]:
    last_kind = 0
    ans = 3
else:
    last_kind = 1
    ans = 6
i = 1
while i < N:
    if last_kind == 0 and S[0][i] == S[1][i]:
        ans *= 2
        last_kind = 0
        i += 1
    elif last_kind == 0 and S[0][i] == S[0][i+1]:
        ans *= 2
        last_kind = 1
        i += 2
    elif last_kind == 1 and S[0][i] == S[1][i]:
        last_kind = 0
        i += 1
    elif last_kind == 1 and S[0][i] == S[0][i+1]:
        ans *= 3
        last_kind = 1
        i += 2
    print(i, ans)
    ans %= M

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = [input() for _ in range(2)]
M = 1000000007

if S[0][0] == S[1][0]:
    last_kind = 0
    ans = 3
else:
    last_kind = 1
    ans = 6
for i in range(1, N):
    if last_kind == 0 and S[0][i] == S[1][i]:
        ans *= 2
        last_kind = 0
    elif last_kind == 0 and S[0][i] == S[0][i+1]:
        ans *= 2
        last_kind = 1
    elif last_kind == 1 and S[0][i] == S[1][i]:
        last_kind = 0
    elif last_kind == 1 and S[0][i] == S[0][i+1]:
        ans *= 3
        last_kind = 1
    ans %= M

print(ans)

Submission status: Runtime Error

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

mod = 10**9 + 7

# dp[i][j] : i番目まで見たときに、i番目の要素が j = 0 だと縦に、
# j = 1 だと横にブロックが並んでいた時の色の塗りかた (mod 10**9+7)
dp = [[0 for j in range(2)] for i in range(n+1)]

if s1[0] == s2[0]:
    dp[1][0] = 3
    i = 1
else:
    dp[2][1] = 3 * 2
    i = 2
while i < n:
    if s1[i] == s2[i]:
        if s1[i+1] == s2[i+1]:
            dp[i+1][0] = dp[i][0] * 2 % mod
            
            i += 1
        else:
            dp[i+2][1] = dp[i][0] * 2 % mod
            i += 2
    else:
        if s1[i+2] == s2[i+2]:
            dp[i+2][0] = dp[i][1] % mod
            i += 2
        else:
            dp[i+3][1] = dp[i][1] * 2 % mod
            i += 3

print((dp[n][0] % mod + dp[n][1] % mod) % mod)

Submission status: Runtime Error

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

mod = 10**9 + 7

# dp[i][j] : i番目まで見たときに、i番目の要素が j = 0 だと縦に、
# j = 1 だと横にブロックが並んでいた時の色の塗りかた (mod 10**9+7)
dp = [[0 for j in range(2)] for i in range(n+1)]

if s1[0] == s2[0]:
    dp[1][0] = 3
    i = 1
else:
    dp[2][1] = 3 * 2
    i = 2
while i < n:
    if s1[i] == s2[i]:
        if s1[i+1] == s2[i+1]:
            dp[i+1][0] = dp[i][0] * 2
            i += 1
        else:
            dp[i+2][1] = dp[i][0] * 2
            i += 2
    else:
        if s1[i+2] == s2[i+2]:
            dp[i+2][0] = dp[i][1]
            i += 2
        else:
            dp[i+3][1] = dp[i][1] * 2
            i += 3

print(dp[n][0] + dp[n][1])

Submission status: Runtime Error

Solution code:
N = int(input())
S1 = input()
S2 = input()
p = 10 ** 9 + 7
dp = [0] * (N + 1)
if S1[0] == S2[0]:
  dp[0] = 3
else:
  dp[0] = 6
for i in range(1, N + 1):
  if S1[i] == S1[i - 1]:
    dp[i] = dp[i - 1]
  elif S1[i - 1] == S2[i - 1]:
    dp[i] = dp[i - 1] * 2 % p
  elif S1[i] == S2[i]:
    dp[i] = dp[i - 1]
  else:
    dp[i] = dp[i - 1] * 3 % p
print(dp[N])

Submission status: Runtime Error

Solution code:
N = int(input())
upper = list(input())
lower = list(input())
ans = 0

def count(upper, lower, state):
    global ans
    if upper[0] == lower[0]:
        if state == 1:
            ans *= 2
            try:
                count(upper[1:],lower[1:],state=1)
            except:
                return ans
        if state == 2:
            try:
                count(upper[1:], lower[1:], state=1)
            except:
                return ans
    else:
        if state == 1:
            ans *= 2
            try:
                count(upper[2:], lower[2:], state=2)
            except:
                return ans
        if state == 2:
            ans *= 3
            try:
                count(upper[2:], lower[2:], state=2)
            except:
                return ans

if upper[0] == lower[0]:
    ans = 3
    count(upper[1:], lower[1:], state=1)
else:
    ans = 6
    count(upper[2:], lower[2:], state=2)

print(ans%1000000007)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jul  3 10:58:30 2018

@author: ashida
"""
N=int(input())

S1=list(input())
S2=list(input())

if S1[0]==S1[1]:
    st=2
    ans=6
    flag=1
else:
    st=1
    ans=3
    flag=-1

while st<N:
    if st+1<N:
        if S1[st]==S1[st+1]:
            if flag<0:
                ans*=2
            else:
                ans*=3
            st+=2
            flag=1
        else:
            if flag<0:
                ans*=2
            else:
                ans*=1
            st+=1
            flag=-1
    else:
        if flag<0:
            ans*=2
        else:
            ans*=1
        st+=1
        
ans=ans%1000000007
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
x = input()
y = input()
place = 0
if x[0]==y[0]: num,flag = 3,True
else: num,flag = 6,False
while place<n-1:
	if flag:
		place += 1
		num = (num*2)%(10**9+7)
		if x[place]!=y[place]: flag = False
	else:
		place += 2
		if x[place]==y[place]: flag=True
		else: num=(num*3)%(10**9+7)
print(num)

Submission status: Runtime Error

Solution code:
mod = 1000000007
n = int(input())

up = input()
dw = input()

arrays = 1

j = 0
justBefore = None

if up[j] == dw[j]:
    arrays *= 3
    j += 1
    justBefore = "T"
else:
    arrays *= 6
    j += 2
    justBefore = "Y"

while(j != len(up)-1):
    if len(up)<3:break
    if justBefore == "T":
        if up[j] == dw[j]:
            arrays *= 2
            arrays%=1000000007
            j += 1
            justBefore = "T"
        else:
            arrays *= 2
            arrays%=1000000007
            j += 2
            justBefore = "Y"
    else:
        if up[j] == dw[j]:
            arrays *= 1
            arrays%=1000000007
            j += 1
            justBefore = "T"
        else:
            arrays *= 3
            arrays%=1000000007
            j += 2
            justBefore = "Y"
            
print(arrays)

Submission status: Runtime Error

Solution code:
mod = 1000000007
n = int(input())

up = input()
dw = input()

arrays = 1

j = 0
justBefore = None

if up[j] == dw[j]:
    arrays *= 3
    j += 1
    justBefore = "T"
else:
    arrays *= 6
    j += 2
    justBefore = "Y"

while(j != len(up)-1):
    if len(up)<3:break
    if justBefore == "T":
        if up[j] == dw[j]:
            arrays *= 2
            j += 1
            justBefore = "T"
        else:
            arrays *= 2
            j += 2
            justBefore = "Y"
    else:
        if up[j] == dw[j]:
            arrays *= 1
            j += 1
            justBefore = "T"
        else:
            arrays *= 3
            j += 2
            justBefore = "Y"
            
print(arrays%mod)

Submission status: Runtime Error

Solution code:
mod = 1000000007
n = int(input())

up = input()
dw = input()

arrays = 1

j = 0
justBefore = None

if up[j] == dw[j]:
    arrays *= 3
    j += 1
    justBefore = 0
else:
    arrays *= 6
    j += 2
    justBefore = 1

while(j != len(up)-1):
    if len(up)<3:break
    if justBefore:
        if up[j] == dw[j]:
            arrays *= 1
            j += 1
            justBefore = 0
        else:
            arrays *= 3
            j += 2
            justBefore = 1
    else:
        if up[j] == dw[j]:
            arrays *= 2
            j += 1
            justBefore = 0
        else:
            arrays *= 2
            j += 2
            justBefore = 1
            
print(arrays%mod)

Submission status: Runtime Error

Solution code:
mod = 1000000007
n = int(input())

up = input()
dw = input()

arrays = 1

j = 0
justBefore = None

if up[j] == dw[j]:
    arrays *= 3
    j += 1
    justBefore = 0
else:
    arrays *= 6
    j += 2
    justBefore = 1

while(j != len(up)-1):
    if justBefore:
        if up[j] == dw[j]:
            arrays *= 1
            j += 1
            justBefore = 0
        else:
            arrays *= 3
            j += 2
            justBefore = 1
    else:
        if up[j] == dw[j]:
            arrays *= 2
            j += 1
            justBefore = 0
        else:
            arrays *= 2
            j += 2
            justBefore = 1
            
print(arrays%mod)

Submission status: Runtime Error

Solution code:
n = int(input())

up = input()
dw = input()

arrays = 1

j = 0
justBefore = None

if up[j] == dw[j]:
    arrays *= 3
    j += 1
    justBefore = 0
else:
    arrays *= 6
    j += 2
    justBefore = 1

while(j != len(up)-1):
    if justBefore:
        if up[j] == dw[j]:
            arrays *= 1
            j += 1
            justBefore = 0
        else:
            arrays *= 4
            j += 2
            justBefore = 1
    else:
        if up[j] == dw[j]:
            arrays *= 2
            j += 1
            justBefore = 0
        else:
            arrays *= 2
            j += 2
            justBefore = 1
            
print(arrays)

Submission status: Runtime Error

Solution code:
mod = 1000000007
N = int(input())
S = input()
T = input()
a = 1
b = 0 ###前が横だったら0 縦だったら1
i = 0
while i < N:
	if i == 0: ###初期処理
		if N == 1:
			a *= 3
			break
		else:
			if S[0] == S[1]:
				a *= 6
				i += 2
				b = 0
			else:
				a *= 3
				i += 1
				b = 1
	if S[i] == S[i+1]:
		if b == 0: ###前が横で次も横
			a *= 3
			i += 2
			b = 0
			continue
		if b == 1: ###前が縦で次が横
			a *= 2
			i += 2
			b = 0
			continue
	else:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
			continue
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1
			continue
print(a % mod)

Submission status: Runtime Error

Solution code:
mod = 1000000007
N = int(input())
S = input()
T = input()
a = 1
b = 0 ###前が横だったら0 縦だったら1
i = 0
while i < N:
	if i == 0: ###初期処理
		if N == 1:
			a *= 3
			break
		else:
			if S[0] == S[1]:
				a *= 6
				i += 2
				b = 0
			else:
				a *= 3
				i += 1
				b = 1
	if i == N-1:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
			break
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1
			break
	if S[i] == S[i+1]:
		if b == 0: ###前が横で次も横
			a *= 3
			i += 2
			b = 0
			continue
		if b == 1: ###前が縦で次が横
			a *= 2
			i += 2
			b = 0
			continue
	else:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
			continue
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1
			continue

print(a % mod)

Submission status: Runtime Error

Solution code:
mod = 1000000007
N = int(input())
S = input()
T = input()
a = 1
b = 0 ###前が横だったら0 縦だったら1
i = 0
while i < N:
	if i == 0: ###初期処理
		if N == 1:
			a *= 3
			break
		else:
			if S[0] == S[1]:
				a *= 6
				i += 2
				b = 0
			else:
				a *= 3
				i += 1
				b = 1
	if i == N-1:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
			break
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1
			break
	if S[i] == S[i+1]:
		if b == 0: ###前が横で次も横
			a *= 3
			i += 2
			b = 0
			continue
		if b == 1: ###前が縦で次が横
			a *= 2
			i += 2
			b = 0
			continue
	else:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
			continue
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1
			continue

print(a % mod)

Submission status: Runtime Error

Solution code:
num=raw_input()
tmp=raw_input()
tmp2=raw_input()
bef="none"
n_state=0
af_state=0
if tmp[0]==tmp[1]:
    out=6
else:
    out=3

for i in range(1,len(tmp)):
    if tmp[i]!=bef:
        if i<int(num)-1:
            if tmp[i]==tmp[i+1]:
                if n_state==0:
                    out=out*2
                else:
                    out=out*3
                    n_state=0
            else:
                if n_state==0:
                    out=out*2
                else:
                    out=out*1
                    n_state=0
        else:
            if n_state==0:
                out=out*2
            else:
                out=out*1
                n_state=0
    else:
        n_state=1
    bef=tmp[i]
print out % 1000000007

Submission status: Runtime Error

Solution code:
def d():
    N  = int(input())
    S1 = input()
    S2 = input()
    from collections import deque
    s = deque(S1)
    S = ''
    for _ in range(N):
        try:
            if s[0] == s[1]:
                S += 'Z'
                s.popleft()
                s.popleft()
            else:
                S += 'I'
                s.popleft()
        except:
            break
    res = 6 if S[0] == 'Z' else 3
    for i in range(1, len(S)):
        if S[i-1:i+1] == 'II':
            res *= 2
        if S[i-1:i+1] == 'IZ':
            res *= 2
        if S[i-1:i+1] == 'ZI':
            res *= 1
        if S[i-1:i+1] == 'ZZ':
            res *= 3
    print(res % 1000000007)
    
d()

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
T = input()
a = float(1)
b = 0 ###前が横だったら0 縦だったら1
i = 0
while i < N:
	if i == 0: ###初期処理
		if N == 1:
			a *= 3
			break
		else:
			if S[0] == S[1]:
				a *= 6
				i += 2
				b = 0
			else:
				a *= 3
				i += 1
				b = 1
	if i == N-1:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
			break
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1
			break
	if S[i] == S[i+1]:
		if b == 0: ###前が横で次も横
			a *= 3
			i += 2
			b = 0
		if b == 1: ###前が縦で次が横
			a *= 2
			i += 2
			b = 0
	else:
		if b == 0: ###前が横で次が縦
			a *= 1
			i += 1
			b = 1
		if b == 1: ###前が縦で次も縦
			a *= 2
			i += 1
			b = 1

print(a)

Submission status: Runtime Error

Solution code:
N = int(input())

S = [[0 for i in range(N)] for j in range(2)]

S[0] = input()
S[1] = input()

x = 2
y = len(S[0])

patterns = 1

tate_flag = False
yoko_flag = False
last_state = ""

for i in range(y):
    if skip is True:
        skip = False
        continue
    if S[0][i] == S[1][i]:
        if last_state == "":
            patterns *= 1
        elif last_state == "tate":
            patterns *= 2
        elif last_state == "yoko":
            patterns *= 1
        last_state = "tate"

    else:
        if last_state == "":
            patterns *= 2
        elif last_state == "tate":
            patterns *= 2
        elif last_state == "yoko":
            patterns *= 3
        last_state = "yoko"
        skip = True

print(patterns)

Submission status: Runtime Error

Solution code:
n = input()
st1 = raw_input()
st2 = raw_input()
m = 1
mod = 1000000007
pat = 0 # or 1,2
bl = False
for i in range(n):
  if pat == 2 and bl:
    bl = False
    continue
  if st1[i] == st2[i] and pat = 0:
    m = m*3/mod
	pat = 1
  elif st1[i] == st2[i] and pat = 1:
    m = m*2/mod
    pat = 1
  elif st1[i] == st2[i] and pat = 2:
    m = m*2/mod
    pat = 1
  elif st1[i] != st2[i] and pat = 0:
    m = m*6/mod
    pat = 2
    bl = True
  elif st1[i] != st2[i] and pat = 1:
    m = m*2/mod
    pat = 2
    bl = True
  elif st1[i] != st2[i] and pat = 2:
    m = m*3/mod
    pat = 2
    bl = True
print m