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)

use std::io::{BufRead, Write};
use std::io;
use std::ops::{Add, Div, Mul, Sub};
use std::iter::Sum;

const MOD: u64 = 1000000007;

#[derive(Clone, Copy)]
struct ModInt {
    pub inner: u64,
}

impl From<u64> for ModInt {
    fn from(i: u64) -> ModInt {
        ModInt { inner: i }
    }
}

impl Add<ModInt> for ModInt {
    type Output = ModInt;
    fn add(self, rhs: ModInt) -> ModInt {
        ModInt {
            inner: (self.inner + rhs.inner) % MOD,
        }
    }
}

impl Sub<ModInt> for ModInt {
    type Output = ModInt;
    fn sub(self, rhs: ModInt) -> ModInt {
        ModInt {
            inner: (self.inner + MOD - rhs.inner) % MOD,
        }
    }
}

impl Mul<ModInt> for ModInt {
    type Output = ModInt;
    fn mul(self, rhs: ModInt) -> ModInt {
        ModInt {
            inner: (self.inner * rhs.inner) % MOD,
        }
    }
}

impl Sum for ModInt {
    fn sum<I: Iterator<Item = ModInt>>(iter: I) -> ModInt {
        iter.fold(ModInt::from(0), Add::add)
    }
}

fn solve<R: BufRead, W: Write>(mut input: R, mut output: W) {
    let mut s = String::new();
    input.read_line(&mut s).unwrap();
    let _: usize = s.trim().parse().unwrap();
    s.clear();
    input.read_line(&mut s).unwrap();
    let s1 = s.trim().as_bytes().to_owned();
    s.clear();
    input.read_line(&mut s).unwrap();
    let _ = s.trim().as_bytes().to_owned();
    #[derive(Clone, Copy)]
    enum BlockType {
        Horizontal,
        Vertical,
    }
    let mut bt: Vec<BlockType> = Vec::new();
    let mut i = 0;
    while i != s1.len() {
        if i == s1.len() - 1 || s1[i] != s1[i + 1] {
            bt.push(BlockType::Vertical);
            i += 1;
        } else {
            bt.push(BlockType::Horizontal);
            i += 2;
        }
    }
    let mut total = ModInt {
        inner: match bt[0] {
            BlockType::Horizontal => 6,
            BlockType::Vertical => 3,
        },
    };
    for x in bt.windows(2) {
        total = total * ModInt {
            inner: match (x[0], x[1]) {
                (BlockType::Horizontal, BlockType::Horizontal) => 3,
                (BlockType::Horizontal, BlockType::Vertical) => 1,
                (BlockType::Vertical, BlockType::Horizontal) => 2,
                (BlockType::Vertical, BlockType::Vertical) => 2,
            },
        }
    }
    writeln!(output, "{}", total.inner).unwrap();
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    solve(stdin.lock(), stdout.lock());
}

#[cfg(test)]
mod tests {
    fn test(input: &str, output: &str) {
        let mut v = Vec::<u8>::new();
        ::solve(input.as_bytes(), &mut v);
        assert_eq!(String::from_utf8(v).unwrap(), output);
    }
    #[test]
    fn case1() {
        test(
            "3
aab
ccb\n",
            "6\n",
        );
    }
    #[test]
    fn case2() {
        test(
            "1
Z
Z\n",
            "3\n",
        );
    }
    #[test]
    fn case3() {
        test(
            "52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn\n",
            "958681902\n",
        );
    }
}