Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define sz(s) (int)(s).size()
- #define all(s) s.begin(),s.end()
- void Speed() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- }
- struct DSU {
- int n;
- vector<int> par, siz;
- int cnt;
- DSU(int n) {
- this->n = n;
- par = siz = vector<int>(n + 5);
- for (int g = 1; g <= n; g++)par[g] = g, siz[g] = 1;
- cnt = n;
- }
- int find(int i) {
- if (i == par[i])return i;
- return par[i] = find(par[i]);
- }
- void merge(int a, int b) {
- a = find(a);
- b = find(b);
- if (a == b)return;
- if (siz[a] < siz[b])
- swap(a, b);
- par[b] = a;
- siz[a] += siz[b];
- cnt--;
- }
- };
- void solve() {
- int n;
- cin >> n;
- DSU dsu(n);
- for (int g = 1; g <= n; g++)
- for (int i = 1; i <= n; i++) {
- int x;
- cin >> x;
- if (x > 0)
- dsu.merge(g, i);
- }
- int comp = dsu.cnt;
- assert(comp <= 26);
- const int mod = 1e9 + 7;
- ll ans = 1;
- for (int g = 1; g <= comp; g++)
- ans = ans * (27ll - g) % mod;
- cout << ans << "\n";
- }
- int main() {
- Speed();
- int tc = 1;
- cin >> tc;
- while (tc--) {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment