#include <cstdio>
#include <algorithm>

const int N = 1000 + 10;
const int MOD = 1e9 + 7;

inline int qmod(int x) { return (x>=MOD)?(x-MOD):x; }

int main() {
	int i, p2, f1, f2, tmp, n;
	char aa, ab, ba, bb;

	scanf("%d", &n);
	if(n == 2 or n == 3) {
		printf("1\n");
		return 0;
	}

	f1 = f2 = 1;
	for(i=2; i<=n-2; i++) {
		tmp = qmod(f1 + f2);
		f2 = f1;
		f1 = tmp;
	}
	for(p2=1, i=1; i<=n-3; i++)
		p2 = qmod(p2 * 2);

	scanf(" %c %c %c %c", &aa, &ab, &ba, &bb);
	if(ab == 'B') {
		if(bb == 'B')
			printf("1\n");
		else {
			if(ba == 'A')
				printf("%d\n", p2);
			else
				printf("%d\n", f1);
		}
	}
	else {
		if(aa == 'A')
			printf("1\n");
		else {
			if(ba == 'B')
				printf("%d\n", p2);
			else
				printf("%d\n", f1);
		}
	}
	return 0;
}
#include<bits/stdc++.h>
#define con(typ) const typ
typedef long long ll;
template<typename T>void sf(T &x){x=0;T f=0;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')f=1;for(;c>='0'&&c<='9';c=getchar())x=(x<<3)+(x<<1)+(c^'0');if(f)x=-x;}
template<typename T>void pf(T x,char l='\n'){static int s[100],t;if(x<0)putchar('-'),x=-x;do s[++t]=x%10,x/=10;while(x);while(t)putchar(s[t--]^'0');putchar(l);}
int main(){
	std::vector<std::pair<ll,ll>> a;
	int n;
	ll k;
	sf(n),sf(k);
	for(int i=1;i<=n;++i){
		ll x,y;
		sf(x),sf(y);
		a.emplace_back(std::make_pair(x,y));
	}
	#define all(x) (x).begin(),(x).end()
	std::sort(all(a));
	for(std::pair<ll,ll> now:a){
		if(now.first>k)break;
		k+=now.second;
	}
	pf(k);
	return 0;
}