#include<bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL mod = 1000000007;
string c[4];
LL power(LL a, LL r){
    LL res = 1;
    for(; r; r >>= 1, a = a * a % mod)
        if(r & 1) res = res * a % mod;
    return res;
}
LL Fib(int n){
    if(n <= 2) return 1;
    LL a = 1, b = 1;
    for(int i = 0; i < n - 2; i += 1){
        LL t = (a + b) % mod;
        b = a;
        a = t;
    }
    return a;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    for(int i = 0; i < 4; i += 1) cin >> c[i];
    if(c[0] == "A" and c[1] == "A") cout << 1;
    else if(c[1] == "B" and c[3] == "B") cout << 1;
    else if(c[0] == "A" and c[1] == "B" and c[2] == "A" and c[3] == "A") cout << (N == 2 ? 1 : power(2, N - 3));
    else if(c[0] == "A" and c[1] == "B" and c[2] == "B" and c[3] == "A") cout << Fib(N - 1);
    else if(c[0] == "B" and c[1] == "A" and c[2] == "A" and c[3] == "A") cout << Fib(N - 1);
    else if(c[0] == "B" and c[1] == "A" and c[2] == "A" and c[3] == "B") cout << Fib(N - 1);
    else if(c[0] == "B" and c[1] == "A" and c[2] == "B" and c[3] == "A") cout << (N == 2 ? 1 : power(2, N - 3));
    else if(c[0] == "B" and c[1] == "A" and c[2] == "B" and c[3] == "B") cout << (N == 2 ? 1 : power(2, N - 3));
    else if(c[0] == "B" and c[1] == "B" and c[2] == "A" and c[3] == "A") cout << (N == 2 ? 1 : power(2, N - 3));
    else if(c[0] == "B" and c[1] == "B" and c[2] == "B" and c[3] == "A") cout << Fib(N - 1);
    else assert(0);
    return 0;
}
//AAABABABABABBB
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = 0; i <= (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;

int main() {
  int n;
  cin >> n;

  int m = 0, day = 1;
  do {
    m = m + day;
    day += 1;
  } while(n > m);

  cout << day - 1 << endl;

  return 0;
}
