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

const long long MOD = 1e9+7;
char cAA, cAB, cBA, cBB;

long long f[1005];

int main(){
    int N;
    scanf("%d", &N);

    scanf(" %c %c %c %c", &cAA, &cAB, &cBA, &cBB);

    if(cAA == 'A' && cAB == 'A'){
        printf("1");
        return 0;
    }else if(cBB == 'B' && cAB == 'B'){
        printf("1");
        return 0;
    }else if(N <= 3){
        printf("1");
        return 0;
    }else if( (cAA != cBB && cAB == cBA) || (cAA == 'A' && cAB == 'B' && cBA == 'B' && cBB == 'A') ||(cAA == 'B' && cAB == 'A' && cBA == 'A' && cBB == 'B') ){
        f[2] = 1;
        f[3] = 1;
        for(int i = 4; i <= N; i ++){
            f[i] = (f[i-1] + f[i-2])%MOD;
        }
        printf("%lld\n", f[N]);
        return 0;
    }else if((cAA == cBA && cBA == cBB && cAB != cBA) || (cAA == 'B' && cAB == 'A' && cBA == 'B' && cBB == 'A') || (cAA == 'B' && cAB == 'B' && cBA == 'A' && cBB == 'A')){
        f[2] = 1;
        f[3] = 1;
        for(int i = 4; i <= N; i ++){
            f[i] = (f[i-1] + f[i-1])%MOD;
        }
        printf("%lld\n", f[N]);
        return 0;
    }
    throw;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return true;} else return false;}
template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return true;} else return false;}

const int MOD = 1000000007;
struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    ll val() const { return x;}
    mint inv() const { return pow(MOD-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination MOD prime
struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < MOD);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint p(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[n-k];
    }
}; //comb(10000007); // ←MOD-1が上限
// comb(5, 2) → 10, comb.p(5, 2) → 20
// comb.fact[4] → 24, 2のN乗 → mint(2).pow(N)
int N;
set<string> st;
map<string, char> mp;
void dfs(string s) {
    int l = sz(s);
    if (l == N) {
        st.insert(s);
        return;
    }
    for (int i = 0; i < l-1; i++) {
        // iとi+1の間にいれる
        string target = s.substr(i, 2);
        string t = s.substr(0, i+1) + mp[target] + s.substr(i+1);
        // cout << s << '\n';
        // cout << target << ' ' << t << '\n';
        // cout << "--------" << '\n';
        dfs(t);
    }
}


int main() {
    // フラクタル？、逆から考えて実現可能か、dp[i][j] iは長さjはAの数、bit
    // どこかで分割
    int M;
    cin >> M;
    cin >> mp["AA"] >> mp["AB"] >> mp["BA"] >> mp["BB"];
    vector<mint> ans(1e3 + 10);
    for (int i = 2; i <= 10; i++) {
        st.clear();
        N = i;
        dfs("AB");
        ans[i] = sz(st);
    }
    if (M < 10) {
        cout << ans[M] << '\n';
        return 0;
    }
    if (ans[8].val() == 1) {
        cout << 1 << '\n';
        return 0;
    }
    // rep(i, 10) cout << ans[i].val() << '\n';
    if (ans[8].val() == ans[7].val() + ans[6].val()) {
        for (int i = 11; i <= M; i++) {
            ans[i] = ans[i-1] + ans[i-2];
        }
    } else if (ans[8].val() == ans[7].val() * 2) {
        for (int i = 11; i <= M; i++) {
            ans[i] = ans[i-1] * 2;
        }
    }
    cout << ans[M] << '\n';
    return 0;
    // 言い換え、重複, 境界, 条件, ll (1e5のnC2), 0, -, 1i, for s&g, debug
}

// 49