// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define pss pair<string, string>
#define ull unsigned long long int
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x) cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<int, string> &p1, const pair<int, string> &p2)const {
        if (p1.F == p2.F)
            return p1.S < p2.S;
        return p1.F > p2.F;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b, ll M = MOD) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % M;
        a = (a * a) % M;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 1e5 + 5, inf = 2e18;
ll n, m, X, Y;
std::vector<std::vector<pair<ll, pll>>> adj(N); // u, t,k
void bfs()
{
    vll dist(n, inf);
    dist[X] = 0;
    priority_queue<pll, std::vector<pll> , greater<pll>> s;
    s.push({0, X});
    while (!s.empty())
    {
        auto t = s.top();
        s.pop();
        if (t.F > dist[t.S])
            continue;
        for (auto x : adj[t.S])
        {
            ll to = x.F;
            ll m = (x.S.S - t.F % x.S.S) % x.S.S;
            if (dist[to] > t.F + m + x.S.F)
            {
                dist[to] = t.F + m + x.S.F;
                s.push({dist[to], to});
            }
        }
    }
    if (dist[Y] == inf)
        cout << -1;
    else
        cout << dist[Y];
}
void solve()
{
    cin >> n >> m >> X >> Y;
    X--; Y--;
    ll x, y, t, k;
    forn(i, 0, m)
    {
        cin >> x >> y >> t >> k;
        x--; y--;
        adj[x].pb({y, {t, k}});
        adj[y].pb({x, {t, k}});
    }
    bfs();
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output1.txt", "w", stdout);
#endif
    int test = 1;
    //cin >> test;
    clock_t z = clock();
    forn(tes, 0, test)
    {
        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

constexpr int P = 1000000007;

ll powmod(ll n, ll k) {
    ll r = 1, t = n % P;
    for (; k != 0; k /= 2) {
        if (k & 1) r = r * t % P;
        t = t * t % P;
    }
    return r;
}
ll modinv(ll n) {
    return powmod(n, P - 2);
}

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

    //char aa, ab, ba, bb;
    //cin >> aa >> ab >> ba >> bb;

    constexpr int M = 4;
    char c[M];
    for (int i = 0; i < M; i++) {
        cin >> c[i];
        c[i] -= 'A';
    }

    if (c[1] == 1) {
        for (int i = 0; i < M; i++) {
            c[i] ^= 1;
        }
        swap(c[0], c[3]);
    }

    if (c[0] == 0 || n <= 3) {
        cout << 1 << endl;
        quick_exit(0);
    }

    //10??
    n -= 3;

    if (c[2] == 0) {
        ll dp[1000][2];
        dp[0][0] = 1;
        dp[0][1] = 0;
        for (int i = 0; i < n + 1; i++) {
            dp[i + 1][0] = (dp[i][0] + dp[i][1]) % P;
            dp[i + 1][1] = dp[i][0];
        }

        cout << dp[n + 1][0] << endl;

    } else {
        cout << powmod(2, n) << endl;
    }

    /*
    * 挿入していくだけなのでA[AB]*Bの形
    * 難しいのはAABからABABになったあと BAがBのときBも好きなだけ伸ばせる（お尻のBは1個のまま）
    * BAがAのときBは1個のまま Aは好きな長さ
    */

    return 0;
}