#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

constexpr ll mod = 1000000007LL;

int N;
char caa, cab, cba, cbb;

void input(void){
    cin >> N;
    cin >> caa >> cab >> cba >> cbb;
}

ll modpow(ll a, ll b){
    if (b == 0) return 1LL;
    ll tmp = modpow(a, b / 2);
    tmp = tmp * tmp % mod;
    if (b % 2) tmp = tmp * a % mod;
    return tmp;
}

ll solve(void){
    if (N <= 3) return 1LL;

    if (
        (cab == 'A' && caa == 'A') ||
        (cab == 'B' && cbb == 'B')
    ) return 1LL;

    if (
        (cab == 'A' && cba == 'A') ||
        (cab == 'B' && cba == 'B')
    ){
        ll tmpa = 1, tmpb = 0;
        for (int i = 2; i <= N; ++i){
            ll new_a = (tmpa + tmpb) % mod;
            ll new_b = tmpa;
            tmpa = new_a, tmpb = new_b;
        }
        return tmpb;
    }

    return modpow(2, N - 3);
}

int main(void){
    input();
    cout << solve() << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;

int main(void)
{
    ll N;
    cin >> N;

    ll a=0, b=0, c=0;
    ll nn = 1;
    ll INF = 1001001001;
    ll an=INF, bn=INF, cn=INF;

    while(1)
    {
        a = N/nn;
        c = N%nn;
        if(an+bn+cn > a+b+c)
        {
            an = a;
            bn = b;
            cn = c;
        }
        if(nn*2 > N) break;
        else
        {
            nn *= 2;
            ++b;
        }
    }

    // cout << an << " " << bn << " " << cn << endl;
    cout << an+bn+cn << endl;



    return 0;
}
