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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3");
            WillReturn.Add("aab");
            WillReturn.Add("ccb");
            //6
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1");
            WillReturn.Add("Z");
            WillReturn.Add("Z");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("52");
            WillReturn.Add("RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn");
            WillReturn.Add("RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn");
            //958681902
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        string S1 = InputList[1];
        string S2 = InputList[2];

        int UB_X = S1.Length - 1;
        int UB_Y = 1;

        char[,] BanArr = new char[UB_X + 1, UB_Y + 1];
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (LoopY == 0) BanArr[LoopX, 0] = S1[LoopX];
                if (LoopY == 1) BanArr[LoopX, 1] = S2[LoopX];
            }
        }

        //横並べをX、縦並べかY、として文字列で持つ
        var HaitiList = new List<char>();
        for (int LoopX = 0; LoopX <= UB_X; ) {
            if (BanArr[LoopX, 0] != BanArr[LoopX, 1]) {
                HaitiList.Add('X');
                LoopX += 2;
            }
            else {
                HaitiList.Add('Y');
                LoopX += 1;
            }
        }

        long Answer = 1;
        for (int I = 0; I <= HaitiList.Count - 1; I++) {
            char CurrHaiti = HaitiList[I];

            if (I == 0) {
                Answer = (CurrHaiti == 'X' ? 6 : 3);
            }
            else {
                char PrevHaiti = HaitiList[I - 1];
                if (PrevHaiti == 'X' && CurrHaiti == 'X') Answer *= 3;
                if (PrevHaiti == 'X' && CurrHaiti == 'Y') Answer *= 1;
                if (PrevHaiti == 'Y' && CurrHaiti == 'X') Answer *= 2;
                if (PrevHaiti == 'Y' && CurrHaiti == 'Y') Answer *= 2;
            }
            Answer %= 1000000007;
        }
        Console.WriteLine(Answer);
    }
}