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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;
import java.math.*;
import java.util.concurrent.*;

class Main
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Random rnd=new Random();
	static int maxn=(int)(55);
	static long mod=(long)(1e9+7);
	static boolean[][] v;
	
	static void build()
	{
		v=new boolean[10][10];
		
		int[] a=new int[9],b=new int[9];
		
		for(int i=0;i<3;i++)
		{
			a[i]=b[i]=i;
		}
		
		a[3]=a[4]=0;a[5]=a[6]=1;a[7]=a[8]=2;
		
		b[5]=b[7]=0;b[3]=b[8]=1;b[4]=b[6]=2;
		
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)	
			{
				if(a[i]!=a[j] && b[i]!=b[j])
				{
					v[i][j]=true;
				}
			}
		}
	}
	
    public static void main(String args[]) throws Exception
    {
		build();int n=sc.nextInt();char[][] a=new char[2][n];
		
		for(int i=0;i<2;i++)
		{
			a[i]=sc.next().toCharArray();
		}
		
		int[] size=new int[maxn];int ctr=0;
		
		for(int i=0;i<n;)
		{
			if(a[0][i]==a[1][i])
			{	
				size[ctr++]=1;
				
				i++;
			}
			
			else
			{
				size[ctr++]=2;
				
				i+=2;
			}
		}
		
		long[][] dp=new long[9][ctr];
		
		for(int i=0;i<9;i++)
		{
			if(size[0]==1 && i<3)
			{
				dp[i][0]=1;
			}
			
			else if(size[0]==2 && i>=3)
			{
				dp[i][0]=1;
			}
		}
		
		int[] s=new int[ctr],e=new int[ctr];
		
		for(int i=0;i<ctr;i++)
		{
			if(size[i]==1)
			{
				s[i]=0;e[i]=3;
			}
			else
			{
				s[i]=3;e[i]=9;
			}
		}
		
		/*
		
		for(int i=0;i<9;i++)
		{
			out.println(dp[i][0]);
		}
		
		*/
		
		for(int i=1;i<ctr;i++)
		{
			for(int k=s[i];k<e[i];k++)	
			{
				for(int j=s[i-1];j<e[i-1];j++)
				{
					if(v[j][k])
					{
						dp[k][i]=(dp[k][i]+dp[j][i-1])%mod;
					}
				}		
			}
		}
		
		long res=0;
		
		for(int i=0;i<9;i++)
		{
			res=(res+dp[i][ctr-1])%mod;
		}
		
		out.println(res);out.close();
    }
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}