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 in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#include <math.h> /* M_PI */
#define INF 2147483647
//vector<vector<ll>> memo(2, vector<ll>(53));
map<ll, ll> pa;
ll n;
vector<vector<ll>> done(2, vector<ll>(53)); //循環セールスマン問題みたいな考え方ができればいいよね
ll dfs(int u, vector<ll> memo)
{
    if (u == n)
    {
        return 1;
    }
    ll res = 0;
    if (pa[u] == 0 || pa[u] == 4)
    {
        memo[u] = 0;
        ll a = dfs(u + 1, memo);
        memo[u] = 1;
        ll b = dfs(u + 1, memo);
        memo[u] = 2;
        ll c = dfs(u + 1, memo);
        res = a + b + c;
        return res;
    }
    if (pa[u] == 1 || pa[u] == 5 || pa[u] == 6 || pa[u] == 8)
    {
        memo[u] = (memo[u - 1] + 1) % 3;
        ll a = dfs(u + 1, memo);
        memo[u] = (memo[u - 1] + 2) % 3;
        ll b = dfs(u + 1, memo);
        res = a + b;
        return res;
    }
    if (pa[u] == 7 || pa[u] == 9 || pa[u] == 10)
    {
        ll j = 0;
        for (int i = 0; i < 3; i++)
        {
            if (memo[u - 1] != i && memo[u - 2] != i)
            {
                j = i;
            }
        }
        memo[u] = j;
        return dfs(u + 1, memo);
    }
    return 0;
}

int main()
{

    cin >> n;
    string s1;
    string s2;
    cin >> s1 >> s2;
    vector<vector<ll>> gr(2, vector<ll>(n)); //グループ配列に番号を振る。
    vector<ll> memo(n);
    rep(i, n)
    {
        if (s1[i] != s2[i])
        {
            gr[0][i] = i;
            gr[0][i + 1] = i;

            gr[1][i] = i + 1;
            gr[1][i + 1] = i + 1;
            if (i == 0)
            {
                pa[i] = 0;     //接するのは、ない
                pa[i + 1] = 1; //接するのはindex-1のみ
            }
            /* else if (i == 1)
            {
                pa[i] = 2;     //接するのはindex-1
                pa[i + 1] = 3; //接するのはindex-2
            }*/
            else if (pa[i - 1] == 4 || pa[i - 1] == 5 || pa[i - 1] == 10)
            {
                pa[i] = 6;     //接するのはindex-1
                pa[i + 1] = 7; //接するのはindex-2,index-1
            }
            else if (pa[i - 1] == 7 || pa[i - 1] == 1)
            {
                pa[i] = 8;
                pa[i + 1] = 9;
            }
            i += 1;
        }
        else
        {
            gr[0][i] = i;
            gr[1][i] = i;
            if (i == 0)
            {
                pa[i] = 4; //接しない。
            }
            else if (i == 1)
            {
                pa[i] = 5; //接するのはindex-1
            }
            else if (pa[i - 1] == 5)
            {
                pa[i] = 5; //接するのはindex-1
            }
            else
            {
                pa[i] = 10; //接するのはindex-1,index-2
            }
        }
    }

     cout << dfs(0, memo);
}