#include <bits/stdc++.h>
using namespace std;

const auto mod = 1000000007;

auto fp(long long x, long long e) -> long long {
    auto ans = 1ll;
    while (e > 0) {
        if (e & 1) ans = ans * x % mod;
        e >>= 1;
        x = x * x % mod;
    }
    return ans;
}

auto fib(int n) -> long long {
    if (n < 2) return 1ll;
    n--;
    auto Fi_1 = 1ll, Fi = 1ll;
    while (n-- > 0) {
        auto tmp = Fi;
        Fi = (Fi_1 + Fi) % mod;
        Fi_1 = tmp;
    }
    return Fi;
}

auto main() -> int {
    auto n = 0;
    auto AA = char{}, AB = char{},
         BA = char{}, BB = char{};
    cin >> n;
    cin >> AA >> AB >> BA >> BB;
    if (n <= 3) {
        cout << 1 << endl;
        return 0;
    }
    if (AB == 'A') {
        if (AA == 'A') {
            cout << 1 << endl;
        } else if (BA == 'B') {
            cout << fp(2, n - 3) << endl;
        } else {
            cout << fib(n - 2) << endl;
        }

    } else {
        if (BB == 'B') {
            cout << 1 << endl;
        } else if (BA == 'A') {
            cout << fp(2, n - 3) << endl;
        } else {
            cout << fib(n - 2) << endl;
        }
    }
    return 0;
}
#include <bits/stdc++.h>
#define rep(i,l,r) for (int i=l;i<=r;++i)
#define per(i,r,l) for (int i=r;i>=l;--i)
#define ll long long
using namespace std;
const int N=1e6+6,P=1e9+7; int dp[N];
int main(){
	int n; scanf("%d",&n);
	char str[4][5],s[4];
	rep(i,0,3) scanf("%s",str[i]),s[i]=str[i][0];
	if (s[1]=='A'&&s[0]=='A'||s[1]=='B'&&s[3]=='B'){
		puts("1"); return 0;
	}
	if (s[1]==s[2]){
		dp[1]=dp[2]=1;
		rep(i,3,n-1) dp[i]=(dp[i-1]+dp[i-2])%P;
		cout<<dp[n-1]<<endl;
	}
	else{
		int s=1; rep(i,1,n-3) s=2LL*s%P;
		printf("%d\n",s); return 0;
	}
	return 0;
}