#include #include #include #include #include constexpr uint32_t p = 998244353; typedef uint32_t *flagmap; std::vector powersof3; int digitof(uint32_t hash, int idx) { return (hash / powersof3[idx]) % 3; } void setdigit(uint32_t &hash, int idx, int newval) { hash -= digitof(hash, idx) * powersof3[idx]; hash += newval * powersof3[idx]; } bool isflag(int a, int b, int c, int d) { return a == 0 && b == 1 && c ==2 && d == 1; } void populateNoFlags(const std::vector &grid, flagmap *fm, int cur, int m, uint32_t hash) { if(cur == m) { fm[0][hash] = 1; return; } if(grid[cur] == 3) { for(uint32_t j=0; j<3; j++) { uint32_t newhash = hash; setdigit(newhash, cur, j); populateNoFlags(grid, fm, cur+1, m, newhash); } } else { setdigit(hash, cur, grid[cur]); populateNoFlags(grid, fm, cur+1, m, hash); } } int main() { int n, m; std::cin >> n >> m; int questionmarks = 0; std::vector grid(n*m); for(int i=0; i> c; if(c == 'I') grid[i] = 0; else if(c == 'C') grid[i] = 1; else if(c == 'P') grid[i] = 2; else { grid[i] = 3; questionmarks++; } } uint32_t base = 1; for(int i=0; i