#include <bits/stdc++.h>

using namespace std;

int BASE = 1e9 + 7;

int n;
char aa, ab, ba, bb;
int solve() {
    if (n <= 3) return 1;
    if (ab == 'A') {
        if (aa == 'A') return 1;
        if (ba == 'A') {
            // number of strings of length n-3 so that no two bs can be adjacent
            // A....AB (.... can have any chars)
            int lastA = 1, lastB = 0;
            for (int i = 1; i <= n-3; ++i) {
                int newLastA = (lastA + lastB) % BASE;
                int newLastB = lastA;
                lastA = newLastA;
                lastB = newLastB;
            }
            return (lastA + lastB) % BASE;
        } else {
            // number of strings of length n-3
            // A....AB (.... can have any chars)
            int x = 1;
            for (int i = 1; i <= n-3; ++i) x = (x << 1) % BASE;
            return x;
        }
    } else {
        if (bb == 'B') return 1;
        if (ba == 'B') {
            // number of strings of length n-3 so that no two as can be adjacent
            // AB....B (.... can have any chars)
            int lastA = 0, lastB = 1;
            for (int i = 1; i <= n-3; ++i) {
                int newLastA = lastB;
                int newLastB = (lastA + lastB) % BASE;
                lastA = newLastA;
                lastB = newLastB;
            }
            return (lastA + lastB) % BASE;
        } else {
            // number of strings of length n-3
            // AB....B (.... can have any chars)
            int x = 1;
            for (int i = 1; i <= n-3; ++i) x = (x << 1) % BASE;
            return x;
        }
    }
}

int main() {
    cin >> n >> aa >> ab >> ba >> bb;
    cout << solve() << endl;
    return 0;
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int n,f[1005][2];
char s[2][2];
bool vis[2][2];
void dfs(int x,int y){
	if(vis[x][y]){
		return;
	}
	vis[x][y]=true;
	dfs(x,s[x][y]);
	dfs(s[x][y],y);
}
int main(){
	cin>>n;
	if(n==2){
		puts("1");
		return 0;
	}
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			cin>>s[i][j];
			s[i][j]-='A';
		}
	}
	dfs(0,1);
	if(s[0][1]==1){
		f[1][1]=1;
		n--;
	}else f[1][0]=1;
	for(int i=2;i<=n;i++){
		for(int j=0;j<2;j++){
			for(int k=0;k<2;k++){
				if(vis[j][k]){
					f[i][k]=(f[i][k]+f[i-1][j])%1000000007;
				}
			}
		}
	}
	if(s[0][1]==0){
		printf("%d\n",f[n-1][0]);
	}else{
		printf("%d\n",f[n][1]);
	}
	return 0;
}