#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 5;
const int modu = 1e9 + 7;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

typedef long long LL;

char s[2][2];
LL f[1005];
int n;

int main() {
    // freopen("my.txt", "w", stdout);
    cin >> n;
    getchar();
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j) {
            s[i][j] = getchar();
            getchar();
        }
    if ((s[0][1] == s[0][0] && s[0][1] == 'A') ||
        (s[0][1] == s[1][1] && s[0][1] == 'B')) puts("1");
    else {
        if (s[1][0] == s[0][1]) {
            f[0] = 1;
            for (int i = 2; i <= n; ++i)
                for (int j = 2; j <= i; ++j)
                    f[i] = (f[i] + f[i-j]) % modu;
            cout << f[n] << endl;
        }
        else {
            f[0] = 1;
            for (int i = 2; i <= n; ++i) {
                for (int j = 2; j <= i; ++j)
                    f[i] = (f[i] + f[i-j]*(j-1)) % modu;
            }
            LL ans = 0;
            for (int i = 2; i <= n; ++i)
                ans = (ans+f[n-i]) % modu;
            cout << ans << endl;
        }
    }
	return 0;
}

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

const int mod = 1e9 + 7;

int powmod(int a, int b) {
    int ret = 1;
    for (; b; a = 1LL * a * a % mod, b >>= 1) {
        if (b & 1) ret = 1LL * ret * a % mod;
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0);

    int n; cin >> n;
    char aa, ab, ba, bb;
    cin >> aa >> ab >> ba >> bb;

    vector<int> fib(n + 5);
    fib[1] = 1;
    for (int i = 2; i <= n; i++)  fib[i] = (fib[i - 1] + fib[i - 2]) % mod;

    int ans;
    if (n == 2) {
        ans = 1;
    } else if (ab == 'A') {
        if (aa == 'A') ans = 1;
        else if (ba == 'B') ans = powmod(2, n - 3);
        else ans = fib[n - 1];
    } else {
        if (bb == 'B') ans = 1;
        else if (ba == 'A') ans = powmod(2, n - 3);
        else ans = fib[n - 1];
    }
    cout << ans << "\n";
    return 0;
}
