#define ll long long #define N 1000010 const ll p = 1e9 + 7; ll n, m, k, inv2, invk; ll f[N], g[N], fac[N], inv[N];
ll qmod(ll base, ll n){ base %= p; ll res = 1; while (n) { if (n & 1) { res = res * base % p; } base = base * base % p; n >>= 1; } return res; }
voidadd(ll &x, ll y){ x += y; if (x >= p) x -= p; }
#define rep(i, a, n) for (int i=a;i<n;i++) #define pb push_back #define SZ(x) ((int)(x).size()) typedefvector<int> VI; typedefpair<int, int> PII; const ll mod = 1000000007; // head
int _; namespace linear_seq { ll res[N], base[N], _c[N], _md[N];
vector<int> Md;
voidmul(ll *a, ll *b, int k){ rep(i, 0, k + k) _c[i] = 0; rep(i, 0, k) if (a[i]) rep(j, 0, k) _c[i + j] = (_c[i + j] + a[i] * b[j]) % mod; for (int i = k + k - 1; i >= k; i--) if (_c[i]) rep(j, 0, SZ(Md)) _c[i - k + Md[j]] = (_c[i - k + Md[j]] - _c[i] * _md[Md[j]]) % mod; rep(i, 0, k) a[i] = _c[i]; }
intsolve(ll n, VI a, VI b){ // a 系数 b 初值 b[n+1]=a[0]*b[n]+... // printf("%d\n",SZ(b)); ll ans = 0, pnt = 0; int k = SZ(a); assert(SZ(a) == SZ(b)); rep(i, 0, k) _md[k - 1 - i] = -a[i]; _md[k] = 1; Md.clear(); rep(i, 0, k) if (_md[i] != 0) Md.push_back(i); rep(i, 0, k) res[i] = base[i] = 0; res[0] = 1; while ((1ll << pnt) <= n) pnt++; for (int p = pnt; p >= 0; p--) { mul(res, res, k); if ((n >> p) & 1) { for (int i = k - 1; i >= 0; i--) res[i + 1] = res[i]; res[0] = 0; rep(j, 0, SZ(Md)) res[Md[j]] = (res[Md[j]] - res[k] * _md[Md[j]]) % mod; } } rep(i, 0, k) ans = (ans + res[i] * b[i]) % mod; if (ans < 0) ans += mod; return ans; }
VI BM(VI s){ VI C(1, 1), B(1, 1); int L = 0, m = 1, b = 1; rep(n, 0, SZ(s)) { ll d = 0; rep(i, 0, L + 1) d = (d + (ll) C[i] * s[n - i]) % mod; if (d == 0) ++m; elseif (2 * L <= n) { VI T = C; ll c = mod - d * qmod(b, mod - 2) % mod; while (SZ(C) < SZ(B) + m) C.pb(0); rep(i, 0, SZ(B)) C[i + m] = (C[i + m] + c * B[i]) % mod; L = n + 1 - L; B = T; b = d; m = 1; } else { ll c = mod - d * qmod(b, mod - 2) % mod; while (SZ(C) < SZ(B) + m) C.pb(0); rep(i, 0, SZ(B)) C[i + m] = (C[i + m] + c * B[i]) % mod; ++m; } } return C; }
intgao(VI a, ll n){ VI c = BM(a); c.erase(c.begin()); rep(i, 0, SZ(c)) c[i] = (mod - c[i]) % mod; return solve(n, c, VI(a.begin(), a.begin() + SZ(c))); } };
intmain(){ int T; scanf("%d", &T); while (T--) { scanf("%lld%lld", &k, &n); invk = qmod(k, p - 2); if (n == -1) { printf("%lld\n", 2ll * qmod(k + 1, p - 2) % p); continue; } for (int i = 1; i <= m; ++i) f[i] = 0; f[0] = 1; g[0] = 1; for (int i = 1; i <= 2 * k; ++i) { if (i > k) { add(f[i], invk * (g[i - 1] - g[i - k - 1] + p) % p); } else { add(f[i], invk * g[i - 1] % p); } g[i] = (g[i - 1] + f[i]) % p; } vector<int> vec; for (int i = 0; i <= 2 * k; ++i) vec.push_back(f[i]); printf("%d\n", linear_seq::gao(vec, n)); } return0; }
int n, m; int v[N][N]; ll ans, tnow; int arr[N], brr[N];
inlinevoidcalc(){ ll res = 0; for (registerint i = 1; i <= m; ++i) { for (registerint j = 1; j <= m; ++j) { res += v[arr[i]][brr[j]]; } } ans = max(ans, res); }
voidDFS(int pos, int cnt, ll now){ // 1 if (cnt == n - pos + 1) { int tmp = arr[0]; tnow = now; for (registerint i = pos; i <= n; ++i) { arr[++arr[0]] = i; for (int j = 1; j <= m; ++j) { tnow += v[i][brr[j]]; } } ans = max(ans, tnow); arr[0] = tmp; return; } if (cnt) { arr[++arr[0]] = pos; tnow = now; for (int j = 1; j <= brr[0]; ++j) { tnow += v[pos][brr[j]]; } if (pos < n) { DFS(pos + 1, cnt - 1, tnow); } else { ans = max(ans, tnow); } --arr[0]; } //0 brr[++brr[0]] = pos; tnow = now; for (int i = 1; i <= arr[0]; ++i) { tnow += v[arr[i]][pos]; } if (pos < n) { DFS(pos + 1, cnt, tnow); } else { ans = max(ans, tnow); } --brr[0]; }
intmain(){ scanf("%d", &n); m = n; n <<= 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { scanf("%d", &v[i][j]); } } DFS(1, m, 0ll); printf("%lld\n", ans); return0; }