#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>

typedef long long LL;

const int N = 505;
const LL MOD = 998244353;

int a[N][N];

inline int read() {
    char ch = getchar();
    while (ch != 'B' && ch != 'R' && ch != '.') ch = getchar();
    if (ch == 'B') return 0;
    if (ch == 'R') return 1;
    return 2;
}

void solve() {
    int n, m;
    std::cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            a[i][j] = read();
        }
    }
    int ans = 0;
    for (int i = 1; i <= n + m - 1; i++) {
        int tt[] = {0, 0, 0};
        for (int j = i; j >= 1; j--) {
            if (j > n) continue;
            int t = i - j + 1;
            if (t > m) continue;
            tt[a[j][t]]++;
        }
        if (tt[0] != 0 && tt[1] != 0) {
            puts("0");
            return;
        } else if (tt[0] == 0 && tt[1] == 0) {
            ans ++;
        }
    }
    LL res = 1;
    for (int i = 0; i < ans; i++) {
        res = res * 2 % MOD;
    }
    std::cout << res << std::endl;
}

int main() {
    solve();
    return 0;
}

/*
2 4
..B.
....
*/
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define per(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; --i)
#define REP(i, a) rep(i, 0, a)
#define PER(i, a) per(i, a, 0)

namespace IO {
	const int MAXIOSIZE = 1 << 24 | 1;
	unsigned char buf[MAXIOSIZE], *p1, *p2;
	#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)
	template <typename T> void read(T& x) {
		x = 0; char ch = gc; bool flg = false;
		for (; ch < '0' || '9' < ch; ch = gc) if (ch == '-') flg |= true;
		for (; '0' <= ch && ch <= '9'; ch = gc) x = x * 10 + ch - '0';
		flg ? x = -x : 0;
	}
	template <typename T> void out(const T& x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); }
	template <typename T> inline void write(const T& x, const char& ed = ' ') { if (x < 0) putchar('-'), out(-x); else out(x); putchar(ed); }
}

const int MAXN = 2e5 + 10;
const int P = 998244353;

int qpower(int a, int x) {
	int ret = 1;
	for (; x; x >>= 1, a = 1ll * a * a % P) x & 1 ? ret = 1ll * ret * a % P : 0;
	return ret;
}

int n, E[MAXN], cnt, vis[MAXN];

void work(int st, int idx) {
	if (vis[st]) return ;
	int cur = st;
	while (1) {
		vis[cur] = idx;
		cur = E[cur];
		if (vis[cur]) {
			cnt += vis[cur] == idx;
   			break;
		}
	}
}

int main() {
	#ifdef LOCAL
		freopen("in.txt", "r", stdin);
	#endif
	IO::read(n);
	REP(i, n) IO::read(E[i]), E[i]--;
	REP(i, n) work(i, i + 1);
	IO::write((qpower(2, cnt) - 1 + P) % P);
	return 0;
}
