|
| 1 | +/** |
| 2 | + * @file 2180F1.cpp |
| 3 | + * @author Macesuted (i@macesuted.moe) |
| 4 | + * @date 2025-12-19 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +#ifndef LOCAL |
| 14 | +#define endl '\n' |
| 15 | +#endif |
| 16 | + |
| 17 | +bool mem1; |
| 18 | + |
| 19 | +#define maxn 5005 |
| 20 | +#define mod 1'000'000'007 |
| 21 | + |
| 22 | +int64_t qpow(int64_t a, int64_t x) { |
| 23 | + int64_t ans = 1; |
| 24 | + while (x) { |
| 25 | + if (x & 1) ans = ans * a % mod; |
| 26 | + a = a * a % mod, x >>= 1; |
| 27 | + } |
| 28 | + return ans; |
| 29 | +} |
| 30 | +int64_t inv(int64_t a) { return qpow(a, mod - 2); } |
| 31 | + |
| 32 | +const int64_t frac14 = inv(4), frac34 = 3 * inv(4) % mod; |
| 33 | + |
| 34 | +int64_t Mod(int64_t x) { return x >= mod ? x - mod : x; } |
| 35 | +int64_t Add(int &x, int64_t y) { return x = Mod(x + y); } |
| 36 | + |
| 37 | +int f[maxn][maxn][2][2], g[maxn][maxn]; |
| 38 | + |
| 39 | +void solve(void) { |
| 40 | + int n, m; |
| 41 | + cin >> n >> m; |
| 42 | + cout << qpow(4, (n + 1) * (m + 1)) * g[n][m] % mod << endl; |
| 43 | + return; |
| 44 | +} |
| 45 | + |
| 46 | +bool mem2; |
| 47 | + |
| 48 | +int main() { |
| 49 | + ios::sync_with_stdio(false), cin.tie(nullptr); |
| 50 | +#ifdef LOCAL |
| 51 | + cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl; |
| 52 | +#endif |
| 53 | + |
| 54 | + for (int x = 0; x < 2; x++) |
| 55 | + for (int y = 0; y < 2; y++) f[1][1][x][y] = (x ? frac14 : frac34) * (y ? frac14 : frac34) % mod; |
| 56 | + |
| 57 | + for (int i = 1; i + 1 < maxn; i++) |
| 58 | + for (int j = 1; j + 1 < maxn; j++) |
| 59 | + for (int u = 0; u < 2; u++) |
| 60 | + for (int l = 0; l < 2; l++) |
| 61 | + for (int t = 0; t < 4; t++) { |
| 62 | + int64_t v = f[i][j][u][l] * frac14 % mod; |
| 63 | + bool U = u || (t == 0), L = l || (t == 1), D = (t == 2), R = (t == 3); |
| 64 | + if (!L) { |
| 65 | + Add(f[i + 1][j][D][0], v * frac34 % mod); |
| 66 | + Add(f[i + 1][j][D][1], v * frac14 % mod); |
| 67 | + } else if (!U) { |
| 68 | + Add(f[i][j + 1][0][R], v * frac34 % mod); |
| 69 | + Add(f[i][j + 1][1][R], v * frac14 % mod); |
| 70 | + } else |
| 71 | + Add(g[i][j], v); |
| 72 | + } |
| 73 | + |
| 74 | + for (int i = 1; i < maxn; i++) |
| 75 | + for (int j = 1; j < maxn; j++) Add(g[i][j], g[i][j - 1]); |
| 76 | + for (int i = 1; i < maxn; i++) |
| 77 | + for (int j = 1; j < maxn; j++) Add(g[i][j], g[i - 1][j]); |
| 78 | + |
| 79 | + int _ = 1; |
| 80 | + cin >> _; |
| 81 | + while (_--) solve(); |
| 82 | + |
| 83 | +#ifdef LOCAL |
| 84 | + cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl; |
| 85 | +#endif |
| 86 | + return 0; |
| 87 | +} |
0 commit comments