#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 505;
const int mod = 998244353;

string s, t;

int main() {
  int n,m;
  S2(n,m);
  int ways = 1;
  rep(i,0,n+m-1) t += ".";
  rep(i,0,n) {
    cin >> s;
    rep(j,0,m) {
      if(s[j] == '.') continue;
      if(s[j] == 'B') {
        if(t[i + j] == 'R') ways = 0;
        t[i + j] = 'B';
      }

      if(s[j] == 'R') {
        if(t[i + j] == 'B') ways = 0;
        t[i + j] = 'R';
      }
    }
  }

  rep(i,0,n+m-1) if(t[i] == '.') {
    ways = ways * 2;
    if(ways >= mod) {
      ways -= mod;
    }
  }
  P(ways);
  return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define fr(x,in,n,r) for(ll x=in;x<n;x+=r)
#define ifr(x,n) for(ll x=0;x<n;x++)
#define dfr(x,n) for(ll x=n-1;x>=0;x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fstm(m,n,r) m.reserve(n);m.max_load_factor(r)
#define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef unordered_map<ll,ll> umap;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 200001

vector<pll> v[N];ll a[N];
bool vis[N];

void dfs(ll u){
    vis[u]=1;
    ifr(i,v[u].size()){
        if(!vis[v[u][i].ff]){
            a[v[u][i].ff]=a[u]^v[u][i].ss;
            dfs(v[u][i].ff);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll n,x,y,w;
    cin>>n;
    ifr(i,n-1){
        cin>>x>>y>>w;
        v[x].pb(pll(y,w));
        v[y].pb(pll(x,w));
    }
    a[1]=0;
    dfs(1);
    ll ans=0;
    ifr(i,60){
        ll cnt =0,c=0,r=1ll<<i;
        fr(j,1,n+1,1){
            if(r&(a[j]))cnt++;
            else c++;
        }
        ans =(ans + ((r)%mod*(cnt*c)%mod)%mod )%mod;
    }
    cout<<ans<<endl;
    return 0;
}