#include #include using namespace std; using ll = long long; using mint = atcoder::modint; struct dsu { public: int csz; dsu() : _n(0) {} dsu(int n) : _n(n), csz(n), parent_or_size(n, -1) {} int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); csz--; parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector> groups() { std::vector leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector& v) { return v.empty(); }), result.end()); return result; } int _n; // root node: -1 * component size // otherwise: parent std::vector parent_or_size; }; // ごめんなさい... vector B = {0,51380224,17301504,11501568,7749632,5599232,4361216,3564416,3023760,2603088,2265120,1971974,1703398,1448460,1194986,939036,687581,454452,258314,119182,42437,11295,2192,296,25,1}; vector C = {0,113246208,54525952,26214400,12582912,6029312,2883584,1376256,655360,311296,147456,69632,32768,15360,7168,3328,1536,704,320,144,64,28,12,5,2,1}; int main(){ ios::sync_with_stdio(false); cin.tie(0); int h, w; ll k; int m; cin >> h >> w >> k >> m; if (h > w) swap(h, w); mint::set_mod(m); vector tb(26); mint div; const int r = h * w; for(int i = 1; i <= 25; i++) tb[i] = mint(i).pow(k); for(int i = 1; i <= h * w; i++) div += mint(1) / i; dsu uf(h * w); auto f = [&](int S){ auto &&tmp = uf.parent_or_size; fill(tmp.begin(), tmp.end(), -1); int T = S; while(T){ int b = T & -T; T -= b; b = __lg(b); int y = b / w, x = b - y * w; if(x + 1 < w && (T >> (b + 1) & 1)) uf.merge(b, b + 1); if(y + 1 < h && (T >> (b + w) & 1)) uf.merge(b, b + w); } mint res; while(S){ int b = S & -S; S -= b; b = __lg(b); if (uf.leader(b) == b) res += tb[uf.size(b)]; } return res; }; mint ans; if(h * w == 25){ for(int i = 1; i <= 25; i++){ ans += (h == 1 ? C[i] : B[i]) * tb[i]; } }else{ for(int S = 1; S < (1 << (h * w)); S++) ans += f(S); } ans *= div; ans /= 1 << h * w; cout << ans.val() << '\n'; }