#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include"atcoder/all" #include"atcoder/modint" using namespace std; using namespace atcoder; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { return ((a>b) ? (a = b, true) : (false));} #define rep(i,s,n) for(long long i=s;i<(long long)(n);i++) #define rrep(i,s,n) for(long long i=n-1;i>=s;i--) const long long inf = 1LL<<60; typedef long long ll; typedef long double ld; typedef unsigned long long ull; //pairのsecondでソートsort(p.begin(),p.end(),cmp) #define cmp [](pair a, pair b){return a.second P; typedef pair > PP; #define rll ll,vector,greater #define rP P,vector

,greater

const long double pi = 3.14159265358979; typedef unsigned long long ull; #define vll vector #define vvll vector> #define vmint vector #define vvmint vector> #define vvch vector> #define vch vector #define vstring vector #define rPP PP,vector,greater #define vP vector

#define vvP vector> #define vPP vector #define all(x) x.begin(), x.end() //UNIQUE(x) xをソートして値の被りがないようにする #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) int stringcount(string s, char c) { return count(s.cbegin(), s.cend(), c); } //sに含まれるcの数 int pc(ll x) { return __builtin_popcount(x); } //ビット列にどれだけ1がたっているかを求める pop count //オバフロしない計算はa > inf - b および a > inf / bでとってね using mint = modint998244353; const ll MAX = 10101011; const ll MOD = 998244353; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; //累積積 finv[0] = finv[1] = 1; //逆元の累積積 inv[1] = 1; //逆元 for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(ll n, ll k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { ll n,m; cin >> n >> m; vvch s(n,vch(m)); rep(i,0,n) rep(j,0,m) cin >> s[i][j]; vvll board(n,vll(m,0)); rep(i,0,n) { int cnt = i%3; rep(j,0,m) { board[i][j] = cnt; cnt = (cnt + 1) % 3; } } //0,1,2の黒の偶奇が同じ vector yet(3,0), parity(3,0); rep(i,0,n) rep(j,0,m) { if(s[i][j] == 'B') parity[board[i][j]]++; else if(s[i][j] == '?') yet[board[i][j]]++; } COMinit(); mint ans = 0; //すべてを0にそろえる //rep(i,0,3) cout << i << " " << parity[i] << endl; //rep(i,0,3) cout << i << " " << yet[i] << endl; mint tmp[3]; rep(i,0,3) { for(ll j=parity[i]%2;j<=yet[i];j+=2) tmp[i] += COM(yet[i], j); } //cout << tmp[0].val() << " " << tmp[1].val() << " " << tmp[2].val() << endl; ans += tmp[0] * tmp[1] * tmp[2]; rep(i,0,3) tmp[i] = 0; //すべてを1にそろえる rep(i,0,3) { for(ll j=(parity[i]+1)%2;j<=yet[i];j+=2) tmp[i] += COM(yet[i], j); } ans += tmp[0] * tmp[1] * tmp[2]; cout << ans.val() << endl; }