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 Rust (1.15.1) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
#![allow(non_snake_case)]
#![allow(unused_imports)]
 
use proconio::{input, fastout};
use proconio::marker::*;
use whiteread::parse_line;
use std::collections::*;

pub mod mod_int {
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
    const MOD: usize = 1000000007;
    #[derive(Clone, Copy, Debug)]
    pub struct ModInt<T: Copy + Clone + Add + AddAssign + Mul + MulAssign + Sub + SubAssign>(pub T);
    #[warn(unused_macros)]
    macro_rules !mod_int_impl {($($t :ty ) *) =>($(impl Add <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn add (self ,other :ModInt <$t >) ->ModInt <$t >{self +other .0 } } impl Add <$t >for ModInt <$t >{type Output =ModInt <$t >;fn add (self ,rhs :$t ) ->ModInt <$t >{let mut tmp =rhs +self .0 ;if tmp >=self ::MOD as $t {tmp =tmp -self ::MOD as $t ;} ModInt (tmp ) } } impl Sub <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn sub (self ,rhs :ModInt <$t >) ->ModInt <$t >{self -rhs .0 } } impl Sub <$t >for ModInt <$t >{type Output =ModInt <$t >;fn sub (self ,rhs :$t ) ->ModInt <$t >{let rhs =if rhs >=self ::MOD as $t {rhs %self ::MOD as $t } else {rhs } ;let value =if self .0 <rhs {self .0 +self ::MOD as $t } else {self .0 } ;ModInt (value -rhs ) } } impl AddAssign <ModInt <$t >>for ModInt <$t >{fn add_assign (&mut self ,rhs :ModInt <$t >) {*self =*self +rhs ;} } impl AddAssign <$t >for ModInt <$t >{fn add_assign (&mut self ,rhs :$t ) {*self =*self +rhs ;} } impl SubAssign <ModInt <$t >>for ModInt <$t >{fn sub_assign (&mut self ,rhs :ModInt <$t >) {*self =*self -rhs ;} } impl SubAssign <$t >for ModInt <$t >{fn sub_assign (&mut self ,rhs :$t ) {*self =*self -rhs ;} } impl Div <$t >for ModInt <$t >{type Output =ModInt <$t >;fn div (self ,mut rhs :$t ) ->ModInt <$t >{if rhs >=MOD as $t {rhs %=MOD as $t ;} self *ModInt (rhs ) .pow ((MOD -2 ) as usize ) } } impl Div <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn div (self ,rhs :ModInt <$t >) ->ModInt <$t >{self /rhs .0 } } impl DivAssign <$t >for ModInt <$t >{fn div_assign (&mut self ,rhs :$t ) {*self =*self /rhs } } impl DivAssign <ModInt <$t >>for ModInt <$t >{fn div_assign (&mut self ,rhs :ModInt <$t >) {*self =*self /rhs } } impl Mul <ModInt <$t >>for ModInt <$t >{type Output =ModInt <$t >;fn mul (self ,rhs :ModInt <$t >) ->ModInt <$t >{self *rhs .0 } } impl Mul <$t >for ModInt <$t >{type Output =ModInt <$t >;fn mul (self ,rhs :$t ) ->ModInt <$t >{ModInt (self .0 *rhs %self ::MOD as $t ) } } impl MulAssign <ModInt <$t >>for ModInt <$t >{fn mul_assign (&mut self ,rhs :ModInt <$t >) {*self =*self *rhs ;} } impl MulAssign <$t >for ModInt <$t >{fn mul_assign (&mut self ,rhs :$t ) {*self =*self *rhs ;} } impl ModInt <$t >{pub fn new (x :$t ) ->Self {ModInt (x %self ::MOD as $t ) } pub fn pow (self ,e :usize ) ->ModInt <$t >{let mut result =ModInt ::<$t >::new (1 ) ;let mut cur =self ;let mut e =e ;while e >0 {if e &1 ==1 {result *=cur ;} cur *=cur ;e >>=1 ;} result } } ) *) }
    mod_int_impl !(usize i32 i64 u32 u64 ) ;
}

#[fastout]
fn main() {
    input!{
        n: usize,
        s: [Chars; 2],
    }

    type ModInt = mod_int::ModInt<usize>;

    let mut pattern: Vec<usize> = Vec::new();

    for i in 0..s[0].len() {
        if s[0][i] == s[1][i] {
            pattern.push(0);
        } else  {
            if i != 0 && pattern[i-1] == 1 {
                pattern.push(2);
            } else {
                pattern.push(1);
            }
        }
    }
    
    // dbg!(&pattern);

    let mut ans = ModInt::new(0);
    for i in 0..pattern.len() {
        if i == 0 { if pattern[i] == 1 { ans = ModInt::new(6) } else { ans = ModInt::new(3) } continue }
        if pattern[i-1] == 2 && pattern[i] == 0 {
            continue
        } else if pattern[i-1] == 1 && pattern[i] == 2 {
            continue
        } else if pattern[i-1] == 2 && pattern[i] == 1 {
            ans *= 3;
        } else {
            ans *= 2;
        }
    }

    println!("{}", ans.0);
}
*/

fn main() {
    let exe = "/tmp/binD112C725";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";