#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}

const int mod=1e9+7;
int add(int a,int b){a+=b;return a>=mod?a-mod:a;}
void ckadd(int&a,int b){a=add(a,b);}
int sub(int a,int b){a-=b;return a<0?a+mod:a;}
void cksub(int&a,int b){a=sub(a,b);}
int mul(int a,int b){return (ll)a*b%mod;}
void ckmul(int&a,int b){a=mul(a,b);}
int powmod(int x,int k){int ans=1;for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);return ans;}
int inv(int x){return powmod(x,mod-2);}

const int N=10050;
int F[N],I[N];
int binom(int n, int k){ return mul(F[n],mul(I[k],I[n-k]));}
void calc()
{
	F[0]=1;
	for(int i=1;i<N;i++) F[i]=mul(F[i-1],i);
	I[N-1]=inv(F[N-1]);
	for(int i=N-2;~i;i--) I[i]=mul(I[i+1],i+1);
}

int main(){
	int n=ri();
	calc();
	char AA,AB,BA,BB;
	scanf("\n%c",&AA);
	scanf("\n%c",&AB);
	scanf("\n%c",&BA);
	scanf("\n%c",&BB);
	if(n<=3)return 0*printf("1\n");
	if(AA=='A'&&BB=='B')return 0*printf("1\n");
	if(AA=='B'&&BB=='A'){
		if(AB!=BA)return 0*printf("%i\n",powmod(2,n-3));
		int ans=0;
		for(int ba=0;ba*2<=n-2;ba++){
			int a=n-2-ba*2;
			ckadd(ans,binom(ba+a,a));
		}
		printf("%i\n",ans);
		return 0;
	}
	if(AA=='A'){
		AA='B';
		AB=AB=='A'?'B':'A';
		BA=BA=='A'?'B':'A';
		BB='B';
		//swap(AB,BA);
	}
	//AA -> ABA
	//BB -> BBB
	if(AB=='B'){
		//AB -> ABB
		printf("1\n");
		return 0;
	}else{
		//AB -> AAB
		if(BA=='A'){
			//BA -> BAA
			int ans=0;
			for(int ba=0;ba*2<=n-2;ba++){
				int a=n-2-ba*2;
				ckadd(ans,binom(ba+a,a));
			}
			printf("%i\n",ans);
			return 0;
		}else{
			//BA -> BBA
			int ans=powmod(2,n-3);
			printf("%i\n",ans);
			return 0;
		}
	}
	return 0;
}
#include <bits/stdc++.h>
#include <random>
using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define endk '\n'
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); }
lint ceil(lint a, lint b) { return (a + b - 1) / b; }
lint digit(lint a) { return (lint)log10(a); }
const lint MOD = 1e9 + 7, INF = 1e18;
lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 };
void YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; }
typedef pair<lint, string> Pa;
typedef pair<lint, plint> tlint;


template <std::int_fast64_t Modulus>
class modint
{
	using u64 = std::int_fast64_t;

public:
	u64 a;
	constexpr modint(const u64 x = 0) noexcept : a(x% Modulus) {}
	constexpr u64& value() noexcept { return a; }
	constexpr const u64& value() const noexcept { return a; }
	constexpr modint operator+(const modint rhs) const noexcept
	{
		return modint(*this) += rhs;
	}
	constexpr modint operator-(const modint rhs) const noexcept
	{
		return modint(*this) -= rhs;
	}
	constexpr modint operator*(const modint rhs) const noexcept
	{
		return modint(*this) *= rhs;
	}
	constexpr modint operator/(const modint rhs) const noexcept
	{
		return modint(*this) /= rhs;
	}
	constexpr modint& operator+=(const modint rhs) noexcept
	{
		a += rhs.a;
		if (a >= Modulus)
		{
			a -= Modulus;
		}
		return *this;
	}
	constexpr modint& operator-=(const modint rhs) noexcept
	{
		if (a < rhs.a)
		{
			a += Modulus;
		}
		a -= rhs.a;
		return *this;
	}
	constexpr modint& operator*=(const modint rhs) noexcept
	{
		a = a * rhs.a % Modulus;
		return *this;
	}
	constexpr modint& operator/=(modint rhs) noexcept
	{
		u64 exp = Modulus - 2;
		while (exp)
		{
			if (exp % 2)
			{
				*this *= rhs;
			}
			rhs *= rhs;
			exp /= 2;
		}
		return *this;
	}
};
typedef modint<MOD> ModInt;

ModInt mod_pow(ModInt x, lint n) {
	ModInt ret = 1;
	while (n > 0) {
		if (n & 1) (ret *= x);
		(x *= x);
		n >>= 1;
	}
	return ret;
}

ModInt func[200000];
void funcinit(int N)
{
	func[0] = 1;
	for (int i = 1; i <= N; i++)
	{
		func[i] = func[i - 1] * i;
	}
}
ModInt comb(ModInt n, ModInt r)
{
	if (n.a <= 0 || n.a < r.a)
	{
		return 1;
	}
	return func[n.a] / (func[r.a] * func[(n - r).a]);
}

lint N;
char c[4];
set<string> st;
void dfs(string s) {
	if (SZ(s) == 5) {
		st.insert(s);
		return;
	}
	REP(i, SZ(s) - 1) {
		if (s[i] == 'A' && s[i + 1] == 'A') dfs(s.substr(0, i + 1) + c[0] + s.substr(i + 1, SZ(s) - i - 1));
		if (s[i] == 'A' && s[i + 1] == 'B') dfs(s.substr(0, i + 1) + c[1] + s.substr(i + 1, SZ(s) - i - 1));
		if (s[i] == 'B' && s[i + 1] == 'A') dfs(s.substr(0, i + 1) + c[2] + s.substr(i + 1, SZ(s) - i - 1));
		if (s[i] == 'B' && s[i + 1] == 'B') dfs(s.substr(0, i + 1) + c[3] + s.substr(i + 1, SZ(s) - i - 1));
	}
}
int main() {
	cin >> N;
	REP(i, 4) cin >> c[i];
	dfs("AB");
	if (SZ(st) == 1 || N < 4) {
		cout << 1 << endk;
	}
	else if (SZ(st) == 4 && N >= 4) {
		cout << mod_pow(2, N - 3).a << endk;
	}
	else {
		ModInt dp[10000];
		dp[2] = 1;
		dp[3] = 1;
		REP(i, N) {
			dp[i + 4] = dp[i + 3] + dp[i + 2];
		}
		cout << dp[N].a << endk;
	}
}