#include<bits/stdc++.h>
#define fo(i,a,b) for(int i=(a);i<=(b);++i)
#define fd(i,a,b) for(int i=(a);i>=(b);--i)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const int maxn = 5e3 + 5;
int n, m, k;
ll a[maxn][maxn], dp[maxn][maxn];
ll power(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if (b & 1)ans = (ans * a) % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return ans;
}
int main() {
	IOS;
	cin >> n >> m >> k;
	fo(i, 1, k) {
		int x, y; char z;
		cin >> x >> y >> z;
		a[x][y] = z;
	}
	dp[1][1] = 1;
	fo(i, 1, n* m - k)dp[1][1] = 3ll * dp[1][1] % mod;
	ll inv3 = power(3, mod - 2);
	fo(i, 1, n)fo(j, 1, m) {
		dp[i][j] %= mod;
		if (a[i][j] == 'X') {
			dp[i + 1][j] += dp[i][j];
			dp[i][j + 1] += dp[i][j];
		}
		else if (a[i][j] == 'R') {
			dp[i][j + 1] += dp[i][j];
		}
		else if (a[i][j] == 'D') {
			dp[i + 1][j] += dp[i][j];
		}
		else {
			dp[i + 1][j] += 2ll * inv3 * dp[i][j];
			dp[i][j + 1] += 2ll * inv3 * dp[i][j];
		}
	}
	cout << dp[n][m] << endl;

	return 0;
}
/*

*/
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 50;
int n,x1[maxn],x2[maxn];
long long f[2][2];
struct node{
	int x,c;
	bool operator < (node p){
		return c < p.c;
	}
}a[maxn];
int read(){
	int x = 0,f = 1;
	char c = getchar();
	while(c < '0' || c > '9'){
		if(c == '-') f = -1;
		c = getchar();
	}
	while(c >= '0' && c <= '9') x = x * 10 + (c ^ 48),c = getchar();
	return x * f;
}
inline long long dis(int x,int a,int b){
	return 0ll + abs(x - a) + abs(a - b);
}
int main(){
	n = read();
	for(int i = 1; i <= n; i ++) a[i].x = read(),a[i].c = read();
	sort(a + 1,a + n + 1);
	int cnt = 0,p = 1;
	while(p <= n){
		cnt ++,x1[cnt] = x2[cnt] = a[p].x;
		while(p < n && a[p].c == a[p + 1].c) p ++,x1[cnt] = min(x1[cnt],a[p].x),x2[cnt] = max(x2[cnt],a[p].x);
		p ++;
	}
	cnt ++,x1[cnt] = x2[cnt] = 0;
	// for(int i = 1; i <= cnt; i ++) cout << i << ' ' << x1[i] << ' ' << x2[i] << endl;
	int cur = 0;
	for(int i = 1; i <= cnt; i ++){
		cur ^= 1;
		f[cur][0] = min(f[cur ^ 1][0] + dis(x1[i - 1],x2[i],x1[i]),f[cur ^ 1][1] + dis(x2[i - 1],x2[i],x1[i]));
		f[cur][1] = min(f[cur ^ 1][0] + dis(x1[i - 1],x1[i],x2[i]),f[cur ^ 1][1] + dis(x2[i - 1],x1[i],x2[i]));
		// cout << i << ' ' << f[cur][0] << ' ' << f[cur][1] << endl;
	}
	printf("%lld\n",min(f[cur][0],f[cur][1]));
	return 0;
}