/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<20;
int N;
VPLL e[SIZE];
LL w[SIZE];
void dfs(int x, int lt) {
    for(auto &[y,v]: e[x]) {
        if(y==lt){continue;}
        w[y]=w[x]^v;
        dfs(y,x);
    }
}
LL get_bit(LL x, int v) { return (x >> v) & 1; }
void solve() {
    dfs(1,1);
    int d[60]={};
    FOR(i,1,N){
        REP(j,60)d[j]+=get_bit(w[i],j);
    }
    LL an=0;
    for(int i=59;i>=0;i--){
        an=an*2%MOD;
        an=(an+(LL)d[i]*(N-d[i]))%MOD;
    }
    W(an);
}
void input() {
    R(N);
    REPP(i,1,N){
        int x,y;
        LL v;
        R(x,y,v);
        e[x].PB(MP(y,v));
        e[y].PB(MP(x,v));
    }
}
int main(){
    input();
    solve();
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
constexpr int INF = 1e9, MOD = 1e9 + 7;
constexpr int64_t LINF = 5e18, LMOD = 998244353;
// #include <atcoder/all>
// using namespace atcoder;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

using fint64 = int_fast64_t;
template<fint64 MOD>
struct ModInt {
	fint64 x;
	ModInt():x(0){}
	ModInt(fint64 x):
		x(x>=0?x%MOD:(MOD-(-x)%MOD)%MOD)
		{}
	// 負号
	ModInt operator -() const{
		return ModInt(-x);
	}
	// 加算
	ModInt &operator +=(const ModInt &rhs){
		x+=rhs.x;
		if(x>=MOD) x-=MOD;
		return (*this);
	}
	ModInt operator +(const ModInt &rhs) const{
		return ModInt(*this)+=rhs;
	}
	// 減算
	ModInt &operator -=(const ModInt &rhs){
		x+=MOD-rhs.x;
		if(x>=MOD) x-=MOD;
		return (*this);
	}
	ModInt operator -(const ModInt &rhs) const{
		return ModInt(*this)-=rhs;
	}
	// 乗算
	ModInt &operator *=(const ModInt &rhs){
		x*=rhs.x;
		if(x>=MOD) x%=MOD;
		return (*this);
	}
	ModInt operator *(const ModInt &rhs) const{
		return ModInt(*this)*=rhs;
	}
	// 除算
	ModInt &operator /=(const ModInt &rhs){
		(*this)*=rhs.inverse();
		return (*this);
	}
	ModInt operator /(const ModInt &rhs) const{
		return ModInt(*this)/=rhs;
	}
	// 等号
	bool operator ==(const ModInt &rhs){
		return x==rhs.x;
	}
	bool operator !=(const ModInt &rhs){
		return x!=rhs.x;
	}
	// 累乗
	ModInt pow(fint64 n) const{
		fint64 tmp=x;
		fint64 ret=1;
		while(n>0){
			if(n&1) ret=ret*tmp%MOD;
			tmp=tmp*tmp%MOD;
			n>>=1ll;
		}
		return ModInt(ret);
	}
	// 逆元
	ModInt inverse() const{
		fint64 a=x,b=MOD,s=1,t=0;
		while(b>0){
			fint64 u=a/b;
			a-=u*b;
			s-=u*t;
			swap(a,b);
			swap(s,t);
		}
		return ModInt(s);
	}
	// 入出力
	friend istream &operator >>(istream &lhs,ModInt<MOD> &rhs){
		fint64 x; lhs>>x;
		rhs=ModInt<MOD>(x);
		return lhs;
	}
	friend ostream &operator <<(ostream &lhs,const ModInt<MOD> &rhs){
		return lhs<<rhs.x;
	}
};

using mint = ModInt<LMOD>;
mint dp[5010][5010];
mint _3[5010];
int sum_x[5010][5010];
int sum_y[5010][5010];
int com[5010][5010];
int h, w;

int main() {
	int k; cin >> h >> w >> k;
	rep(i,h)rep(j,w) com[i][j] = -1;
	while(k--) {
		int y, x; cin >> y >> x;
		--y, --x;
		char c; cin >> c;
		if(c == 'R') com[y][x] = 0;
		if(c == 'D') com[y][x] = 1;
		if(c == 'X') com[y][x] = 2;
	}
	for(int i = h - 1; i >= 0; --i) {
		for(int j = w - 1; j >= 0; --j) {
			if(j + 1 < w) {
				sum_x[i][j] = sum_x[i][j + 1];
				sum_x[i][j] += (com[i][j + 1] == -1);
			}
		}
	}
	for(int j = w - 1; j >= 0; --j) {
		for(int i = h - 1; i >= 0; --i) {
			if(i + 1 < h) {
				sum_y[i][j] = sum_y[i + 1][j];
				sum_y[i][j] += (com[i + 1][j] == -1);
			}
		}
	}
	_3[0] = 1;
	for(int i = 1; i < 5010; ++i) {
		_3[i] = _3[i - 1] * 3;
	}
	for(int y = h - 1; y >= 0; --y) {
		for(int x = w - 1; x >= 0; --x) {
			if(y == h - 1 and x == w - 1) {
				if(com[y][x] == -1) dp[y][x] = 3;
				else dp[y][x] = 1;
				continue;
			}
			mint ret = 0;
			if(com[y][x] == -1) {
				if(x + 1 < w) ret += dp[y][x + 1] * _3[sum_y[y][x]];
				if(y + 1 < h) ret += dp[y + 1][x] * _3[sum_x[y][x]];
				ret *= 2;
			} else {
				if(com[y][x] == 0) {
					if(x + 1 < w) ret += dp[y][x + 1] * _3[sum_y[y][x]];
				} else if(com[y][x] == 1) {
					if(y + 1 < h) ret += dp[y + 1][x] * _3[sum_x[y][x]];
				} else {
					if(x + 1 < w) ret += dp[y][x + 1] * _3[sum_y[y][x]];
					if(y + 1 < h) ret += dp[y + 1][x] * _3[sum_x[y][x]];
				}
			}
			dp[y][x] = ret;
		}
	}
	cout << dp[0][0] << '\n';
	return 0;
}
