#include<bits/stdc++.h>
#define ll long long
#define m 1000000007
using namespace std;
ll n,s,a,ans,f[3002],d[3002][3002]; 
int main(){
	scanf("%lld",&n),d[1][0]=1;
	for(int i=0;i<n;++i){
		scanf("%lld",&a),s+=a;
		for(int j=1;j<=n;++j){
			f[j]=d[j][s%j];
			d[j][s%j]=(d[j][s%j]+f[j-1])%m;
		}
	}
	for(int i=1;i<=n;++i)ans=(ans+f[i])%m;
	printf("%lld",ans);
    return 0;
}
#include <bits/stdc++.h>
#define rep0(i,r) for (int i=0,i##end=r;i<i##end;++i)
#define rep(i,l,r) for (int i=l;i<=r;++i)
#define per(i,r,l) for (int i=r;i>=l;--i)
#define pr pair<int,int>
#define fi first
#define se second
#define ll long long 
using namespace std;
const int N=5e3+3,P=998244353; 
int a[N][N],pw[N],s1[N][N],s2[N][N],dp[N][N];
int main(){
	int n,m,k; scanf("%d%d%d",&n,&m,&k);
	rep(i,1,k){
		int x,y; char s[5];
		scanf("%d%d%s",&x,&y,s);
		if (s[0]=='R') a[x][y]=1;
		if (s[0]=='D') a[x][y]=2;
		if (s[0]=='X') a[x][y]=3;
	}
	rep(i,1,n) rep(j,1,m) 
		s1[i][j]=s1[i][j-1]+(!a[i][j]),s2[i][j]=s2[i-1][j]+(!a[i][j]);
	pw[0]=1; rep(i,1,max(n,m)) pw[i]=3LL*pw[i-1]%P;
	dp[1][1]=1;
	rep(i,1,n)
		rep(j,1,m){
			if (a[i][j]!=2) dp[i][j+1]=(dp[i][j+1]+(ll)pw[s2[i-1][j+1]]*(1+!a[i][j])%P*dp[i][j])%P;
			if (a[i][j]!=1) dp[i+1][j]=(dp[i+1][j]+(ll)pw[s1[i+1][j-1]]*(1+!a[i][j])%P*dp[i][j])%P;
		}
	printf("%d\n",(ll)dp[n][m]*pw[!a[n][m]]%P);
	return 0;
}