#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) begin(x), end(x)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int n0 = 5003, mod = 998244353;
int h, w, k, dp[n0][n0], p[n0][n0], pw[n0 * n0];
char g[n0][n0];

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

int U(int x, int y) {
	if ((g[x][y] == 'D') || (g[x][y] == 'X')) return 1;
	return (g[x][y] == '?') * 2;
}

int L(int x, int y) {
	if ((g[x][y] == 'R') || (g[x][y] == 'X')) return 1;
	return (g[x][y] == '?') * 2;
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	pw[0] = 1;
	for (int i = 1; i < n0 * n0; i++)
		pw[i] = (ll)pw[i - 1] * 3 % mod;
	cin >> h >> w >> k;
	for (int i = 1; i <= h; i++)
		for (int j = 1; j <= w; j++)
			g[i][j] = '?';
	for (int i = 0; i < k; i++) {
		int a, b;
		char c; cin >> a >> b >> c;
		g[a][b] = c;
	}
	dp[1][1] = 1;
	for (int i = 1; i <= h; i++) {
		for (int j = 1; j <= w; j++) {
			if (i == 1 && j == 1) continue;
			p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + (g[i][j] == '?');
			int row = p[i][j - 1] - p[i - 1][j - 1];
			int col = p[i - 1][j] - p[i - 1][j - 1];
			int & res = dp[i][j];
			add(res, (ll)dp[i - 1][j] * U(i - 1, j) * pw[row] % mod);
			add(res, (ll)dp[i][j - 1] * L(i, j - 1) * pw[col] % mod);
		}
	}
	int ans = dp[h][w];
	if (g[h][w] == '?') ans = (ll)ans * 3 % mod;
	cout << ans;
}

  /*
    Auther: ghoshashis545 Ashis Ghosh 😎
    College: Jalpaiguri Govt Enggineering College
*/
#pragma GCC optimize("Ofast")  
#pragma GCC target("avx,avx2,fma") 

#include <bits/stdc++.h>
#include<algorithm>
#include<string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string 
#define se second
#define sp(x) cout << fixed << setprecision(x)
#define endl "\n"
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define ipair pair<int,int>
#define vpi vector<ipair>
#define clr(a,x) memset(a,x,sizeof(a))
#define alt(v) v.begin(),v.end()
#define ralt(v) v.rbegin(),v.rend()
#define pb emplace_back
#define mp make_pair
#define PI 3.1415926535897932384626
#define cntb(x) __builtin_popcount(x)
#define fab(i,a,b) for(int i=(a);i<(b);i++)
#define fba(i,a,b) for(int i=(b);i>=(a);i--)

bool ispoweroftwo(int n){return n&(!(n&(n-1)));}
int mod=1000000007;
// int mod=998244353;
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};

bool test  = 0;

const int inf = 1e18;
const int N = 2e5+5;

int n;
int a[N],ans[60],dist[N];
vector<array<int,2>>adj[N];

void dfs(int u,int p,int c){
    dist[u] = c;
    for(auto it : adj[u]){
        int v = it[0];
        int w = it[1];
        if(v == p)
            continue;
        dfs(v,u,c^w);
    }
}


void solve(int tc = 0)
{
    cin >> n;
    for(int i = 1,x,y,z; i < n; ++i){
        cin >> x >>y >> z;
        adj[x].push_back({y,z});
        adj[y].push_back({x,z});
    }

    dfs(1,-1,0);
    int res = 0;
    for(int i = 0; i < 60; ++i){
        vector<int>cnt(2,0);
        int cur = (1LL<<i)%mod;
        for(int j = 1; j <= n; ++j){
            int bit = dist[j]>>i&1;
            res += (cnt[bit^1]*cur)%mod;
            res %= mod;
            cnt[bit]++;
        }
    }
    cout<<res<<"\n";
}

 

 
 
 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    if(test)
    cin>>t;
    for(int i = 1; i <= t; ++i){
       // cout<<"Case #"<<i<<": ";
        solve(i);
    }
    return 0;
} 