#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
#define Please return
#define AC 0
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int mod=1e9+7;
const int maxn = 1e6+10;

int n,m,a[maxn],q,t;
char s[maxn];
char AB[2],AA[2],BA[2],BB[2];

LL ksm(LL a, LL b) {
    LL res = 1;
    while(b){
        if(b&1){
            res = res*a%mod;
        }
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
LL dp[maxn];
void solve(){
    if(BB[0]=='B'){
        puts("1");return ;
    }
    if(BA[0]=='A') {
        cout<<ksm(2,n-3)<<endl;
    }
    else {
        dp[1]=dp[0]=1;
        fo(i,2,n-2){
            dp[i]=(dp[i-1]+dp[i-2])%mod;
        }
        printf("%lld\n",dp[n-2]);
    }
}
void solve2(){
    if(AA[0]=='A'){
        puts("1");return ;
    }
    if(BA[0]=='B'){
        cout<<ksm(2,n-3)<<endl;
    }
    else {
        dp[1]=dp[0]=1;
        fo(i,2,n-2){
            dp[i]=(dp[i-1]+dp[i-2])%mod;
        }
        printf("%lld\n",dp[n-2]);
    }

}

int main(){
//    op;
    read(n);
    cin>>AA>>AB>>BA>>BB;
    if(n<=3){
        puts("1");return 0;
    }
    if(AB[0]=='B'){
        solve();
    }
    else solve2();

    Please AC;
}
#include <bits/stdc++.h>

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int N = 1e4 + 7;
const long long mod = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n;
string s = "AB";
map < char , map < char , char > > c;

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen( "input.txt" , "r" , stdin );
    //freopen( "output.txt" , "w" , stdout );

    cin >> n;
    cin >> c['A']['A'];
    cin >> c['A']['B'];
    cin >> c['B']['A'];
    cin >> c['B']['B'];
    vector < int > d(N , 1) , f( N , 0 ) , ff( N , 0 );
    ff[2] = ff[3] = 1;
    f[2] = f[3] = 1;
    for( int i = 4; i <= n; i++ ){
        ff[i] = ff[i - 1] + ff[i - 2];
        f[i] = f[i - 1] * 2;
        ff[i] %= mod;
        f[i] %= mod;
    }
    for( int nn = 2; nn <= 10; nn++ ){
        set < string > t;
        t.insert(s);
        for( int it = 3; it <= nn; it++ ){
            set < string > f;
            for( auto x : t ){
                for( int i = 0; i + 1 < (int)x.size(); i++ ){
                    string res = "";
                    for( int j = 0; j <= i; j++ ){
                        res += x[j];
                    }
                    res += c[x[i]][x[i + 1]];
                    for( int j = i + 1; j < (int)x.size(); j++ ){
                        res += x[j];
                    }
                    f.insert(res);
                }
            }
            t = f;
        }
        d[nn] = (int)t.size();
    }
    if( n <= 10 || d[10] == 1 )cout << d[n];
    else if( d[10] == d[9] + d[8] ){
        cout << ff[n];
    }else cout << f[n];
}
