/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
//*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int  ,int > pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll maxn  = 3e6;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n;
char aa , ab , ba , bb;

ll F[maxn];

ll f(int x){
	if(x <= 1)return(F[x] = 1);
	if(F[x])return(F[x]);
	return( F[x] = (f(x - 1) + f(x - 2))%mod );
}

int32_t main(){
    migmig;
	cin >> n >> aa >> ab >> ba >> bb;
	if(n <= 3)dokme(1);
	if(aa == ab && ab == ba && ba == bb)dokme(1);
	if(ab == 'B'){
		if(bb == 'B')dokme(1);
		if(ba == 'A')dokme(pw(2 , n - 3));
		dokme(f(n - 2));
	}
	if(aa == 'A')dokme(1);
	if(ba == 'B')dokme(pw(2 , n - 3));
	dokme((f(n - 2)));
	
    return(0);
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MOD = 998244353;

long long modpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

ll dp[5005][5005];

int main() {
    ll H, W, K;
    cin >> H >> W >> K;
    vector<vector<char>> G(H, vector<char>(W, '?'));
    rep(i, K) {
        int h, w;
        cin >> h >> w;
        h--, w--;
        char c;
        cin >> c;
        G[h][w] = c;
    }
    rep(i, H) rep(j, W) dp[i][j] = 0;

    dp[0][0] = modpow(3LL, H * W - K);
    rep(i, H) rep(j, W) {
        if (i == H - 1 && j == W - 1) break;
        if (i + 1 < H) {
            if (G[i][j] == 'D' || G[i][j] == 'X') {
                dp[i + 1][j] += dp[i][j];
                dp[i + 1][j] %= MOD;
            }
            if (G[i][j] == '?') {
                dp[i + 1][j] += dp[i][j] * modinv(3LL, MOD) % MOD * 2 % MOD;
                dp[i + 1][j] %= MOD;
            }
        }
        if (j + 1 < W) {
            if (G[i][j] == 'R' || G[i][j] == 'X') {
                dp[i][j + 1] += dp[i][j];
                dp[i][j + 1] %= MOD;
            }
            if (G[i][j] == '?') {
                dp[i][j + 1] += dp[i][j] * modinv(3LL, MOD) % MOD * 2 % MOD;
                dp[i][j + 1] %= MOD;
            }
        }
    }
    cout << dp[H - 1][W - 1] << endl;
}