#include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second

ll MOD=998244353;
int h, w, k;
char g[5005][5005];
ll dp[5005][5005], emp[5005][5005], po3[200005], inv;

ll modpow(ll x, ll e){
	if(x==0) return 0;
	if(e==0) return 1;
	ll y=modpow(x, e/2);
	if(e%2==0) return (y*y)%MOD;
	return ((y*y)%MOD*x)%MOD;
}

signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>h>>w>>k;
	po3[0]=1;
	for(int i=1;i<200005;i++){
		po3[i]=(po3[i-1]*3)%MOD;
	}
	for(int i=0;i<=h;i++){
		for(int j=0;j<=w;j++){
			g[i][j]='.';
		}
	}
	for(int i=0;i<k;i++){
		int x, y;
		char c;
		cin>>x>>y>>c;
		g[x][y]=c;
	}
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			emp[i][j]=emp[i-1][j]+emp[i][j-1]-emp[i-1][j-1]+(g[i][j]=='.');
		}
	}
	if(g[1][1]=='.') dp[1][1]=3;
	else dp[1][1]=1;
	inv=modpow(3, MOD-2);
	for(int x=1;x<=h;x++){
		for(int y=1;y<=w;y++){
			if(x+y==2) continue;
			ll &ans=dp[x][y];
			ll c=po3[emp[x][y]-emp[x][y-1]], r=po3[emp[x][y]-emp[x-1][y]];
			if(g[x][y-1]=='R' || g[x][y-1]=='X'){
				ans+=(dp[x][y-1]*c)%MOD;
				ans%=MOD;
			}
			if(g[x-1][y]=='D' || g[x-1][y]=='X'){
				ans+=(dp[x-1][y]*r)%MOD;
				ans%=MOD;
			}
			if(g[x][y-1]=='.'){
				ans+=(((2*inv*dp[x][y-1])%MOD)*c)%MOD;
				ans%=MOD;
			}
			if(g[x-1][y]=='.'){
				ans+=(((2*inv*dp[x-1][y])%MOD)*r)%MOD;
				ans%=MOD;
			}
			//cout<<ans<<" ";
		}
		//cout<<endl;
	}
	cout<<dp[h][w]<<endl;
	/*for(int i=1;i<=2;i++){
		for(int j=1;j<=2;j++){
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}*/
}
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define rsrtl(v) sort(v.begin(), v.end(), greater<ll>())
#define fi(i, a, b) for (int i = a; i < b; i++)
#define fll(i, a, b) for (ll i = a; i < b; i++)
#define srt(v) sort(v.begin(), v.end())
#define pb push_back
#define g(v, i, j) get<i>(v[j])
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(),(x).end()
#define ll long long
ll md = 1000000007;
#define theartofwar                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define F first
#define S second
#define sz(v) v.size()
using namespace std;
#define dbg(x) cerr << #x << " = " << x << endl;
template <typename T>
T pw(T a, T b){
    T c = 1, m = a;
    while (b){
        if (b & 1) c = (c * m);
        m = (m * m), b /= 2;
    }
    return c;
}
template <typename T>
T ceel(T a, T b){
    if (a % b == 0)
        return a / b;
    else
        return a / b + 1;
}
template <typename T>
T my_log(T n, T b){
    T i = 1, ans = 0;
    while (1){
        if (i > n){
            ans--;
            break;
        }
        if (i == n) break;
        i *= b, ans++;
    }
    return ans;
}
template <typename T>
T gcd(T a, T b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
ll pwmd(ll a, ll b){
    ll c = 1, m = a;
    while (b){
        if (b & 1) c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}
ll modinv(ll n){
    return pwmd(n, md - 2);
}
ll inverse(ll i){
    if (i == 1)
        return 1;
    return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b){
    return (a.second < b.second);
}
int main()
{
    theartofwar;
    ll n, a;
    cin >> n;
    vll v;
    fll(i, 0, n) cin >> a, v.pb(a);
    ll dp[n][n + 1];
    fll(i, 0, n) fll(j, 0, n + 1) dp[i][j] = 0;
    ll last[n + 1][n];
    fll(i, 0, n + 1) fll(j, 0, n) last[i][j] = -1;
    dp[0][1] = 1;
    fll(i, 2, n + 1) last[i][v[0] % i] = 0;
    ll s = v[0];
    fll(i, 1, n){
        dp[i][1] = 1;
        s += v[i];
        fll(j, 2, i + 2){
            ll p = s % j;
            if (last[j][p] == -1) continue;
            ll o = last[j][p];
            dp[i][j] = (dp[i][j] + dp[o][j]) % md;
            dp[i][j] = (dp[i][j] + dp[o][j - 1]) % md;
        }
        fll(j, 2, n + 1) last[j][s % j] = i;
    }
    ll ans = 0;
    fll(i, 1, n + 1) ans = (ans + dp[n - 1][i]) % md;
    cout << ans;
}

