#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
  walloc1d(arr, x2-x1, mem);
  (*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
long long T;
long long A[40];
int n1;
int n2;
long long a1[2000000];
long long a2[2000000];
int main(){
  int mask;
  wmem = memarr;
  int i;
  int j;
  long long res = 0;
  rd(N);
  rd(T);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  n1 = N / 2;
  n2 = N - n1;
  for(mask=(0);mask<(1<<n1);mask++){
    for(i=(0);i<(n1);i++){
      if(((mask) &(1<<(i)))){
        a1[mask] += A[i];
      }
    }
  }
  for(mask=(0);mask<(1<<n2);mask++){
    for(i=(0);i<(n2);i++){
      if(((mask) &(1<<(i)))){
        a2[mask] += A[i+n1];
      }
    }
  }
  n1 = (1<<n1);
  n2 = (1<<n2);
  sortA_L(n1, a1);
  sortA_L(n2, a2);
  j = n2 - 1;
  for(i=(0);i<(n1);i++){
    while(j >= 0 && a1[i] + a2[j] > T){
      j--;
    }
    if(j == -1){
      break;
    }
    chmax(res, a1[i] + a2[j]);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay version 20201121-1

// --- original code ---
// int N; ll T, A[40];
// int n1, n2;
// ll a1[2d6], a2[2d6];
// {
//   int i, j;
//   ll res = 0;
//   rd(N,T,A(N));
//   n1 = N / 2;
//   n2 = N - n1;
//   rep(mask,1<<n1) rep(i,n1) if(BIT_ith(mask,i)) a1[mask] += A[i];
//   rep(mask,1<<n2) rep(i,n2) if(BIT_ith(mask,i)) a2[mask] += A[i+n1];
//   n1 = (1<<n1);
//   n2 = (1<<n2);
//   sortA(n1, a1);
//   sortA(n2, a2);
//   j = n2 - 1;
//   rep(i,n1){
//     while(j >= 0 && a1[i] + a2[j] > T) j--;
//     if(j == -1) break;
//     res >?= a1[i] + a2[j];
//   }
//   wt(res);
// }

#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define MOD 998244353
using namespace __gnu_pbds;
using namespace std;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

int h,w,k,dp[5010][5010],val[5010][5010],sum[5010][5010],pw[5010];

inline void add(int &x,int y)
{
	x+=y;
	if(x>=MOD) x-=MOD;
}

signed main()
{
	ios::sync_with_stdio(false);
	cin>>h>>w>>k;
	pw[0]=1;
	for(int i=1;i<=5000;i++){
		pw[i]=(1ll*pw[i-1]*3%MOD);
	}
	for(int i=1;i<=k;i++){
		int x,y;char c;cin>>x>>y>>c;
		if(c=='X') val[x][y]=1;
		if(c=='R') val[x][y]=2;
		if(c=='D') val[x][y]=3;
	}
	for(int i=1;i<=h;i++){
		sum[i][0]=0;
		for(int j=1;j<=w;j++) sum[i][j]=sum[i][j-1]+(val[i][j]==0);
	}
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(i==1&&j==1){
				dp[i][j]=1;continue;
			}
			dp[i][j]=0;
			if(i>1){
				int num=sum[i-1][w]-sum[i-1][j]+sum[i][j-1];
				if(val[i-1][j]==0) add(dp[i][j],1ll*dp[i-1][j]*2%MOD*pw[num]%MOD);
				if(val[i-1][j]==1) add(dp[i][j],1ll*dp[i-1][j]*pw[num]%MOD);
				if(val[i-1][j]==3) add(dp[i][j],1ll*dp[i-1][j]*pw[num]%MOD);
			}
			if(j>1){
				if(val[i][j-1]==0) add(dp[i][j],1ll*dp[i][j-1]*2%MOD);
				if(val[i][j-1]==1) add(dp[i][j],dp[i][j-1]);
				if(val[i][j-1]==2) add(dp[i][j],dp[i][j-1]);
			}
		}
	}
	if(val[h][w]==0) add(dp[h][w],1ll*dp[h][w]*2%MOD);
	cout<<dp[h][w]<<endl;
	return 0;
}