#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define mp(a,b) make_pair(a,b)
#define vi vector<int>
#define mii map<int,int>
#define mpi map<pair<int,int>,int>
#define msi map<string,int>
#define vp vector<pair<int,int> >
#define pb(a) push_back(a)
#define fr(i,n) for(i=0;i<n;i++)
#define rep(i,a,n) for(i=a;i<n;i++)
#define F first
#define S second
#define endl "\n"
#define Endl "\n"
#define fast std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define dom 998244353
#define sl(a) (int)a.length()
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define pii pair<int,int> 
#define mini 2000000000000000000
#define time_taken 1.0 * clock() / CLOCKS_PER_SEC
//const long long INF=1e15;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//auto dist=uniform_int_distribution<int>(0,INF);
//const long double pi = acos(-1);
//primes for hashing 937, 1013
typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
template<typename T, typename U> static inline void amin(T &x, U y) 
{
    if (y < x) 
        x = y; 
}
template<typename T, typename U> static inline void amax(T &x, U y) 
{ 
    if (x < y) 
        x = y; 
}
void shikhar7s(int cas)
{
    int n,i;
    cin>>n;
    int s[n],a[n];
    fr(i,n)
    {
        cin>>a[i];
        if(!i)
            s[i]=a[i];
        else
            s[i]=s[i-1]+a[i];
    }
    int l[n+5][n+5];
    memset(l,0,sizeof(l));
    rep(i,1,n+1)
    {
        vi v[i];
        int j;
        fr(j,n)
        {
            v[s[j]%i].pb(j);
        }
        fr(j,i)
        {
            int k;
            rep(k,1,sz(v[j]))
            {
                l[v[j][k]+1][i]=v[j][k-1]+1;
            }
        }
    }
    int dp[n+5][n+5],j;
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    rep(i,1,n+1)
    {
        rep(j,1,n+1)
        {
            int la=l[i][j];
            dp[i][j]=(dp[la][j-1]+dp[la][j])%mod;
        }
    }
    int ans=0;
    rep(i,1,n+1)
    {
        ans=(ans+dp[n][i])%mod;
    }
    cout<<ans;
}
signed main()
{
    fast;
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    int t=1;
    //cin>>t;
    int cas=1;
    while(cas<=t)
    {
    //cout<<"Case #"<<cas<<": ";
    shikhar7s(cas);
    cas++;
    }
    return 0;
}
#include <bits/stdc++.h>
#define ll long long
#define vec vector
#define pii pair<int, int> 
using namespace std;
 
const long long MAXVAL = (long long) 1e18 + 1;
//const long long MOD = 1000000007ll;
const long long MOD = 998244353ll;
const int INF = 1000000001;
 
long long poww(int x, long long pow) {
  if (pow == 0ll ) return 1;
  if (pow == 1ll) return x % MOD;
  long long ans = poww(x, pow >> 1) % MOD;
  ans = (ans * ans) % MOD;
  if ((pow & 1) == 1) {
    ans = (ans * x ) % MOD;
  }
  return ans % MOD;
}
 
ll gcd(ll x, ll y) {
  if (x == 0) return y;
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int q = 1;
  //cin >> q;
  while (q-- >0) {
    int h,w;
    cin >> h >> w;
    int k;
    cin >> k;

    vec<vec<int>> dyn(h, vec<int>(w));
    map<pii, char> pre;
    for (int i = 0; i < k; ++i) {
      int x, y;
      cin >> x>> y;
      --x;
      --y;
      char c;
      cin >> c;
      pre[(make_pair(x,y))] = c;
    }
    ll inv3 = poww(3, MOD - 2);
    dyn[0][0] = poww(3, w * h - k);
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (pre.count(make_pair(i, j))) {
          char c = pre[make_pair(i, j)];
          if (c == 'X') {
            if (i < h - 1) {
              dyn[i + 1][j] = (dyn[i + 1][j] + dyn[i][j]) % MOD;
            } 
            if (j < w - 1) {
              dyn[i][j + 1] = (dyn[i][j + 1] + dyn[i][j]) % MOD;
            }
          } else if (c == 'D') {
            if (i < h - 1) {
              dyn[i + 1][j] = (dyn[i + 1][j] + dyn[i][j]) % MOD;
            }
          } else {
            if (j < w - 1) {
              dyn[i][j + 1] = (dyn[i][j + 1] + dyn[i][j]) % MOD;
            }
          }
        } else {
            if (i < h - 1) {
              dyn[i + 1][j] = (dyn[i + 1][j] +  2ll * inv3  * dyn[i][j]) % MOD;
            } 
            if (j < w - 1) {
              //cout << " I J " << i << " " << j << endl;
              //cout << " inv3 " << inv3 << endl;
              //cout << " dyn i j " << dyn [i][j] << endl;
              dyn[i][j + 1] = (dyn[i][j + 1] + 2ll * inv3 * dyn[i][j]) % MOD;
            }
        }
      }
    }
    cout << dyn[h - 1][w - 1] << "\n";
  }
}

 
