#include #include using namespace std; using namespace atcoder; // using mint = modint1000000007; // const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; // const int INF = 1e9; // const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, l, r) for (int i = (l); i < (r); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define P pair template inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A& a, const B& b) { if (a > b) { a = b; return true; } return false; } #ifndef KWM_T_MATH_MATRIX_SIMPLE_MATRIX_HPP #define KWM_T_MATH_MATRIX_SIMPLE_MATRIX_HPP // #include #include #include /** * @brief シンプルな行列演算(vector版・軽量) * * 最低限の行列演算(乗算・累乗)を提供する。 * とりあえず使う用の軽量実装。 * * 典型用途: * - 行列累乗(DP遷移) * - 小規模な行列計算 * * 計算量: * - 乗算: O(N^3) * - 累乗: O(N^3 log K) * * @tparam T * - 要素型(+, *, += が定義されていること) * * 制約 / 注意: * - 正方行列のみ想定(matrixPow) * - サイズ不一致は assert * - 単位元は T(1) を使用 * * 使用例: * vector> A(n, vector(n)); * auto B = matrixPow(10, A); * * verified: * - https://atcoder.jp/contests/awc0053/submissions/75169363 */ namespace kwm_t::math::matrix { template std::vector> matrixMul( const std::vector>& A, const std::vector>& B ) { assert(!A.empty() && !B.empty()); assert(A[0].size() == B.size()); int n = (int)A.size(); int m = (int)B[0].size(); int p = (int)A[0].size(); std::vector> C(n, std::vector(m, T(0))); for (int i = 0; i < n; ++i) { for (int k = 0; k < p; ++k) { for (int j = 0; j < m; ++j) { C[i][j] += A[i][k] * B[k][j]; } } } return C; } template std::vector> matrixPow( long long n, const std::vector>& mat ) { assert(!mat.empty()); assert(mat.size() == mat[0].size()); int size = (int)mat.size(); std::vector> res(size, std::vector(size, T(0))); for (int i = 0; i < size; ++i) { res[i][i] = T(1); } auto base = mat; while (n > 0) { if (n & 1) res = matrixMul(res, base); base = matrixMul(base, base); n >>= 1; } return res; } } // namespace kwm_t::math::matrix #endif // KWM_T_MATH_MATRIX_SIMPLE_MATRIX_HPP #ifndef KWM_T_BASE_BUILTIN_HPP #define KWM_T_BASE_BUILTIN_HPP /** * @brief ローカル環境用(MSC)builtin */ #ifdef _MSC_VER #include #define __builtin_popcount (int)__popcnt #define __builtin_popcountll (int)__popcnt64 inline int __builtin_ctz(unsigned int n) { unsigned long i; _BitScanForward(&i, n); return i; } inline int __builtin_ctzll(unsigned long long n) { unsigned long i; _BitScanForward64(&i, n); return i; } inline int __builtin_clz(unsigned int n) { unsigned long i; if (_BitScanReverse(&i, n)) { return 31 - i; }return 32; } inline int __builtin_clzll(unsigned long long n) { unsigned long i; if (_BitScanReverse64(&i, n)) { return 63 - i; }return 64; } #endif #endif // KWM_T_BASE_BUILTIN_HPP int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int h, w; cin >> h >> w; int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; sx--, sy--, gx--, gy--; auto f = [&](int x, int y) { return x * w + y; }; auto g = [&](int x)->P { return { x / w,x % w }; }; long long k; cin >> k; auto move = [](P p, P q) -> bool { int dx = p.first - q.first; int dy = p.second - q.second; return dx == 0 || dy == 0 || abs(dx) == abs(dy); }; auto move2 = [&](int p, int q) -> bool { return move(g(p), g(q)); }; int hw = h * w; vector dp(1 << hw, vector(hw, vector(hw))); rep(i, hw)dp[0][i][i] = 1; rep(i, 1 << hw)rep(_, hw)rep(j, hw) { rep(nj, hw) { if (i == 0 && j == nj)continue; if (1 & (i >> nj))continue; if (!move2(j, nj))continue; int ni = i + (1 << nj); dp[ni][_][nj] += dp[i][_][j]; } } /*rep(i, 1 << hw) { rep(j, hw)rep(k, hw) { if (dp[i][j][k].val())cout << i << " " << j << " " << k << " " << dp[i][j][k].val() << endl; } }*/ long long p = k / hw; long long q = k % hw; vector mat1(h * w, vector(h * w)); vector mat2(h * w, vector(h * w)); rep(i, 1 << hw)rep(j, hw)rep(k, hw) { if (__builtin_popcount(i) != q)continue; // cout << i << " " << j << " " << k << " " << dp[i][j][k].val() << endl; mat1[j][k] += dp[i][j][k]; } //rep(i, hw)rep(j, hw) { //if (mat1[i][j].val())cout << i << " " << j << " " << mat1[i][j].val() << endl; //} rep(i, h * w)rep(j, h * w) { mat2[i][j] = dp.back()[i][j]; } mat2 = kwm_t::math::matrix::matrixPow(p, mat2); auto mat = kwm_t::math::matrix::matrixMul(mat2, mat1); mint ans = mat[f(sx, sy)][f(gx, gy)]; cout << ans.val() << endl; return 0; }