#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=1010;
const int mod=1e9+7;
ll dp[maxn];
char x,y,z,f;
ll mypow(ll a,ll b){ 
    ll ans=1;
    while(b){ 
        if(b&1)ans=1ll*ans*a%mod;
        a=1ll*a*a%mod;
        b>>=1;
    }
    return ans;
}
int main(){ 
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    dp[0]=dp[1]=1;
    cin>>n>>x>>y>>z>>f;
  	if(n==2){cout<<"1"<<endl;return 0;}
    for(int i=2;i<=n+5;++i){ 
        dp[i]=(dp[i-1]+dp[i-2])%mod;
    } 
    ll ans=0;
    if(y=='A'){ 
        if(x=='A')ans=1;
        else if(x=='B'&&z=='A')ans=dp[n-2];
        else if(x=='B'&&z=='B')ans=mypow(2,n-3);
    }else if(y=='B'){ 
        if(f=='B')ans=1;
        else if(f=='A'&&z=='B')ans=dp[n-2];
        else if(f=='A'&&z=='A')ans=mypow(2,n-3);
    }
    cout<<ans;
    return 0;
}

#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int mxN = 1000;
const int P = 1e9+7;
int n; char s[4];
llong pw2[mxN+3],f[mxN+3];

int main()
{
	n = read(); for(int i=0; i<4; i++) {char str[3]; scanf("%s",str); s[i] = str[0]-65;}
	if(n==2) {puts("1"); return 0;}
	pw2[0] = 1ll; for(int i=1; i<=n; i++) pw2[i] = (pw2[i-1]<<1)%P;
	f[1] = 1ll; for(int i=2; i<=n; i++) f[i] = (f[i-1]+f[i-2])%P;
	if(s[1]==0)
	{
		if(s[0]==0) {puts("1");}
		else
		{
			if(s[2]==0) {printf("%lld\n",f[n-1]);}
			else {printf("%lld\n",pw2[n-3]);}
		}
	}
	else
	{
		if(s[3]==1) {puts("1");}
		else
		{
			if(s[2]==1) {printf("%lld\n",f[n-1]);}
			else {printf("%lld\n",pw2[n-3]);}
		}
	}
	return 0;
}