#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
long long mod = 998244353;

int main(){
  int h, w;
  cin >> h >> w;
  vector<vector<int>> T(h + w - 1, vector<int>(3));
  rep(i,h){
    rep(j,w){
      char c;
      cin >> c;
      if(c == 'R') T[i + j][0] = 1;
      if(c == 'B') T[i + j][1] = 1;
      if(c == '.') T[i + j][2] = 1;
    }
  }
  
  long long ans = 1;
  rep(i, h + w - 1){
    if(T[i][0] == 1 && T[i][1] == 1) ans *= 0;
    else if(T[i][0] == 1 || T[i][1] == 1) ans *= 1;
    else ans = ans * 2 % mod;
  }
  
  cout << ans;

}
  
  
  
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

int N, MAX_N = 2000001, idx = 0, MAX_LOG_V = 20;
vector<vector<int>> edge(MAX_N), parent(MAX_LOG_V, vector<int>(MAX_N, -1));
vector<int> tp(MAX_N), l(MAX_N), r(MAX_N), par(MAX_N, -1), depth(MAX_N, 0);
vector<bool> done(MAX_N, false);

void tps(int u) {
    done[u] = true;
    l[u] = idx;
    tp[idx] = u;
    idx++;
    rep(i, edge[u].size()) {
        int v = edge[u][i];
        if (done[v]) continue;
        par[v] = u;
        depth[v] = depth[u] + 1;
        tps(v);
    }
    r[u] = idx;
}

int lca(int u, int v) {
    if (depth[u] > depth[v]) swap(u, v);
    rep(i, MAX_LOG_V) {
        if ((depth[v] - depth[u]) >> i & 1) {
            v = parent[i][v];
        }
    }
    if (u == v) return u;
    for (int k = MAX_LOG_V - 1; k >= 0; k--) {
        if (parent[k][u] != parent[k][v]) {
            u = parent[k][u];
            v = parent[k][v];
        }
    }
    return parent[0][u];
}

void lca_init() {
    rep(i, N) parent[0][i] = par[i];
    rep(i, MAX_LOG_V - 1) {
        rep(j, N) {
            if (parent[i][j] < 0) parent[i + 1][j] = -1;
            else parent[i + 1][j] = parent[i][parent[i][j]];
            //cout << parent[i + 1][j] << " ";
        }
        //cout << "\n";
    }
}

int main() {
    scanf("%d", &N);
    vector<ll> u(N - 1), v(N - 1), w(N - 1), dist(N, 0);
    rep(i, N - 1) scanf("%lld %lld %lld", &u[i], &v[i], &w[i]);
    map<pair<int, int>, ll> edgew;
    rep(i, N - 1) {
        edge[u[i] - 1].push_back(v[i] - 1);
        edge[v[i] - 1].push_back(u[i] - 1);
        edgew[make_pair(u[i] - 1, v[i] - 1)] = w[i];
        edgew[make_pair(v[i] - 1, u[i] - 1)] = w[i];
    }
    tps(0);
    //rep(i, N) cout << tp[i] << " "; cout << "\n";
    vector<int> g(N);
    rep(i, N) g[tp[i]] = i;
    //rep(i, N) cout << g[i] << " "; cout << "\n";
    //rep(i, N) cout << depth[i] << " "; cout << "\n";
    rep(i, N - 1) {
        int x = tp[i + 1], p = par[x];
        dist[x] = dist[p] ^ edgew[make_pair(x, p)];
    }
    //rep(i, N) cout << dist[i] << " "; cout << "(dist)\n";
    ll ans = 0, MOD = 1e9 + 7;
    rep(i, 60) {
        ll count = 0;
        rep(j, N) {
            count += (dist[j] >> i) & 1;
        }
        ans = (ans + (1LL << i) % MOD * count % MOD * (N - count)) % MOD;
        //cout << ans << endl;
    }
    printf("%lld\n", ans);
}