
// Problem : D - AB
// Contest : AtCoder - AtCoder Regular Contest 108
// URL : https://atcoder.jp/contests/arc108/tasks/arc108_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;	

struct splitmix64_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
 
template <typename K, typename V, typename Hash = splitmix64_hash>
using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>;
 
template <typename K, typename Hash = splitmix64_hash>
using hash_set = hash_map<K, __gnu_pbds::null_type, Hash>;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;
int n;
char caa, cab, cba, cbb;
ll dp1[maxN], dp2[maxN][2];

ll solve1(int left) {
	if (left == 0) return 1;
	if (left == 1) return 0;
	if (dp1[left] != -1) return dp1[left];
	ll ans = 0;
	FOR(i, 2, left) {
		ans = (ans + solve1(left - i)) % MOD;
	}
	dp1[left] = ans;
	return ans;
}

ll solve2(int left, int which) {
	if (left == 0) {
		return 1 - which;
	}
	if (dp2[left][which] != -1) return dp2[left][which];
	ll ans = 0;
	FOR(i, 1, left) {
		ans = (ans + solve2(left - i, 1 ^ which)) % MOD;	
	}
	dp2[left][which] = ans;
	return ans;
}

// sprawdz MODULO!
int main() {
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	cin >> caa >> cab >> cba >> cbb;
	RI(i, n) {
		dp1[i] = -1;
		dp2[i][0] = dp2[i][1] = -1;
	}
	// if (n == 2) {
		// cout << 1 << "\n";
	// }
	// if(n == 3) {
		// cout << 1 << "\n";
		// return 0;
	// }
	if (cab == 'B') {
		if (cbb == 'B') {
			cout << 1 << "\n";
		} else {
			if (cba == 'B') {
				cout << solve1(n) << "\n";
			} else {
				cout << solve2(n - 1, 1) << "\n";
			}
		}
	} else {
		if (caa == 'A') {
			cout << 1 << "\n";
		} else {
			if (cba == 'A') {
				cout << solve1(n) << "\n";
			} else {
				cout << solve2(n - 1, 1) << "\n";
			}
		}
	}
	return 0;
}
 

#include <bits/stdc++.h>
using namespace std;
int main(){
	int N; string S; 
	cin >> N; 
	for(int i = 0; i < 4; i++){
		char c; cin >> c; 
		S.push_back(c); 
	}
	long long mod = 1000000007; 
	if(S.at(0) == 'A' && S.at(1) == 'A'){
		cout << 1 << "\n"; return 0; 
	}
	if(S.at(1) == 'B' && S.at(3) == 'B'){
		cout << 1 << "\n"; return 0; 
	}
	if(S.at(1) == S.at(2)){
		long long ans[1010] = {}; 
		ans[2] = 1; ans[3] = 1; 
		for(int i = 4; i <= 1005; i++){
			ans[i] = (ans[i-1] + ans[i-2]) % mod; 
		}
		cout << ans[N] << "\n"; 
		return 0; 
	}
	if(S.at(1) != S.at(2)){
		long long ans[1010] = {}; 
		ans[2] = 1; ans[3] = 1; 
		for(int i = 4; i <= 1005; i++){
			ans[i] = (ans[i-1] + ans[i-1]) % mod; 
		}
		cout << ans[N] << "\n"; 
		return 0; 
	}
}