#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count());
#define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n"
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
template<int modd> struct modint {
	static const int mod = modd;
	int v; explicit operator int() const { return v;}
	modint() : v(0) {}
	modint(ll x) : v(int(x%mod)) {if (v < 0) v += mod;}
	friend bool operator==(const modint& a, const modint& b) {return a.v == b.v;}
	friend bool operator!=(const modint& a, const modint& b) {return a.v != b.v;}
	friend bool operator<(const modint& a, const modint& b) {return a.v < b.v;}
	modint& operator+=(const modint& a) {if ((v += a.v) >= mod) v -= mod; return *this;}
	modint& operator-=(const modint& a) {if ((v -= a.v) < 0) v += mod; return *this;}
	modint& operator*=(const modint& a) {v = int((ll)v*a.v%mod); return *this;}
	modint& operator/=(const modint& a) {return (*this) *= inv(a);}
	friend modint pow(modint a, ll p) {modint ret = 1; for (; p; p /= 2, a *= a){if (p&1) ret *= a;} return ret;}
	friend modint inv(const modint& a) {return pow(a,mod-2);}
	modint operator-() const {return modint(-v);}
	modint& operator++() {return *this += 1;}
	modint& operator--() {return *this -= 1;}
	friend modint operator+(modint a, const modint& b) {return a += b;}
	friend modint operator-(modint a, const modint& b) {return a -= b;}
	friend modint operator*(modint a, const modint& b) {return a *= b;}
	friend modint operator/(modint a, const modint& b) {return a /= b;}
	friend ostream& operator<<(ostream& out, modint a) {return out << a.v;}
	friend istream& operator>>(istream& in, modint& a) {ll b; in >> b; a.v = b; return in;}
};
using mint = modint<1000000007>;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n; char aa,ab,ba,bb;
	cin >> n >> aa >> ab >> ba >> bb;
	if (n == 2) {
		cout << "1\n";
		return 0;
	}
	if (ab == 'B') {
		if (bb == 'B') {
			cout << "1\n";
			return 0;
		} else {
			if (ba == 'A') {
				mint ans = pow(mint(2),n-3);
				cout << ans << "\n";
				return 0;
			} else {
				vector<vector<mint>> dp(n,vector<mint>(2));
				dp[1][1] = 1;
				for (int q = 2; q < n-1; q++) {
					dp[q][1] = dp[q-1][0]+dp[q-1][1];
					dp[q][0] = dp[q-1][1];
				}
				cout << dp[n-2][0]+dp[n-2][1] << "\n";
				return 0;
			}
		}
	}
	if (ab == 'A') {
		if (aa == 'A') {
			cout << "1\n";
			return 0;
		} else {
			if (ba == 'B') {
				mint ans = pow(mint(2),n-3);
				cout << ans << "\n";
				return 0;
			} else {
				vector<vector<mint>> dp(n,vector<mint>(2));
				dp[1][1] = 1;
				for (int q = 2; q < n-1; q++) {
					dp[q][1] = dp[q-1][0]+dp[q-1][1];
					dp[q][0] = dp[q-1][1];
				}
				cout << dp[n-2][0]+dp[n-2][1] << "\n";
				return 0;
			}
		}
	}

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

int main(){
    int n,L;
    cin >> n >> L;
    vector<int> a(n+1);
    a[0] = 0;
    rep(i,n) cin >> a[i+1];
    a.push_back(L+1);
    vector<int> b(n+1);
    b[0] = 0;
    rep(i,n) cin >> b[i+1];
    b.push_back(L+1);
    n+=2;
    vector<int> p;
    rep(i,n){
        if(a[i]>b[i]){
            p.push_back(-1);
        }else if(a[i]==b[i]){
            p.push_back(0);
        }else{
            p.push_back(1);
        }
    }
    ll res = 0;
    int id = 0;
    int left = 0;
    int dir = 0;
    while(id!=n+1){
        if(dir!=p[id]){
            if(dir==1){
                if(p[id]==-1){
                    cout << -1 << endl;
                    return 0;
                }
                while(p[left]==0)left++;
                int right = id;
                // cerr <<"test2 " <<  left << " " << right << endl;
                ll c = 1;
                int L = b[left];
                for(int i = left+1;i<=right;i++){
                    int now = a[i];
                    if(now - c == L){
                        res += c;
                        left++;
                        c--;
                        while(left<=right&&now-c==b[left]){
                            c--;
                            left++;
                        }
                        L = b[left];
                    }
                    c++;
                }
                if(c!=0){
                    cout << -1 << endl;
                    return 0;
                }
            }
            if(dir==-1){
                int right = id-1;
                // cerr <<"test:" <<  left << " " << right << endl;
                ll c = 1;
                int R = b[right];
                for(int i = right-1;i>=left;i--){
                    int now = a[i];
                    // cerr << now << " " << c << " " << R << endl;
                    if(now + c == R){
                        res += c;
                        right--;
                        c--;
                        while(right>i&&now+c==b[right]){
                            c--;
                            right--;
                        }
                        R = b[right];
                    }
                    c++;
                }
                // cerr << c << endl;
                if(c!=1){
                    cout << -1 << endl;
                    return 0;
                }
                left = id;
            }
            dir = p[id];
        }
        if(dir==0&&p[id]==0){
            left = id;
        }
        // cerr << id << " " << res << endl;
        id++;
    }
    cout << res << endl;

    return 0;
}