#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main()
{
    int n;
    cin>>n;
    string aa,ab,ba,bb;
    cin>>aa>>ab>>ba>>bb;
    int ans=1;
    for(int i=0;i<n-3;i++) ans=ans*2%mod;
    if((ab=="A"&&aa=="B"&&ba=="B")||(ab=="B"&&bb=="A"&&ba=="A"))
        cout<<ans<<'\n';
    else if((ab=="A"&&aa=="B")||(ab=="B"&&bb=="A"))
    {
        int dp1=1,dp2=0;
        for(int i=2;i<=n;i++)
        {
            int sv=dp1;
            dp1=(dp2+dp1)%mod;
            dp2=sv;
        }
        cout<<dp2<<'\n';
    }
    else cout<<1<<'\n';
}
#include <bits/stdc++.h>

#define fileio                                                  \
    freopen("C:\\Users\\PRISM17\\Desktop\\in.txt", "r", stdin); \
    freopen("C:\\Users\\PRISM17\\Desktop\\out.txt", "w", stdout);
#define fastio                   \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0);
#define dbg(x) cout << #x << " : " << x << endl;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)

#define mid (st[id].l + st[id].r >> 1)
#define lson (id << 1)
#define rson (id << 1 | 1)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const int INF = 1e9;
const ll mod = 1e9 + 7;

string s[2][2];
set<string> st;
int n;
void dfs(string str) {
    if (str.length() == n) {
        st.insert(str);
        return;
    }
    int len = str.length();
    for (int i = 1; i < len; i++) {
       string nstr = str;
       nstr.insert(i, s[str[i - 1] - 'A'][str[i] - 'A']);
       dfs(nstr);
    }
}
void table() {
    for (int aa = 0; aa <= 1; aa++) {
        for (int bb = 0; bb <= 1; bb++) {
            for (int ab = 0; ab <= 1; ab++) {
                for (int ba = 0; ba <= 1; ba++) {
                    s[0][0] = string(1, 'A' + aa); s[1][1] = string(1, 'A' + bb);
                    s[0][1] = string(1, 'A' + ab); s[1][0] = string(1, 'A' + ba);
                    cout << endl << s[0][0] << s[0][1] << s[1][0] << s[1][1] << ' ';
                    for (int i = 3; i <= 10; i++) {
                        n = i;
                        st.clear();
                        dfs("AB");
                        cout << st.size() << ' ';
                    }
                }
            }
        }
    }
}
int main() {
//    table();
    cin >> n;
    string str(4, ' ');
    for (int i = 0; i < 4; i++) cin >> str[i];
    auto solve = [&](int n, string& pat) -> int {
        vector<int> pow(n + 1, 1), fib(n + 1, 1);
        for (int i = 4; i <= n; i++) {
            pow[i] = pow[i - 1] * 2 % mod;
            fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
        }
        if (pat[0] == 'A' && pat[1] == 'A') return 1;
        if (pat[1] == 'B' && pat[3] == 'B') return 1;
        if (pat == "ABAA" || pat == "BABA" || pat == "BBAA" || pat == "BABB") return pow[n];
        else return fib[n];
    };
    cout << solve(n, str) << endl;
}