1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| #include <bits/stdc++.h> using namespace std;
#ifdef Fread char buf[1 << 21], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread (buf, 1, 1 << 21, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; char c(getchar()); int f(1); for(; c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1; for(; '0' <= c && c <= '9';c = getchar()) x = (x * 10) + (c ^ 48); x *= f; }
template <typename T,typename... Args> inline void r1(T& t, Args&... args) { r1(t); r1(args...); }
#define Getmod
#ifdef Getmod const int mod = 1e9 + 7; template <int mod> struct typemod { int z; typemod(int a = 0) : z(a) {} inline int inc(int a,int b) const {return a += b - mod, a + ((a >> 31) & mod);} inline int dec(int a,int b) const {return a -= b, a + ((a >> 31) & mod);} inline int mul(int a,int b) const {return 1ll * a * b % mod;} typemod<mod> operator + (const typemod<mod> &x) const {return typemod(inc(z, x.z));} typemod<mod> operator - (const typemod<mod> &x) const {return typemod(dec(z, x.z));} typemod<mod> operator * (const typemod<mod> &x) const {return typemod(mul(z, x.z));} typemod<mod>& operator += (const typemod<mod> &x) {*this = *this + x; return *this;} typemod<mod>& operator -= (const typemod<mod> &x) {*this = *this - x; return *this;} typemod<mod>& operator *= (const typemod<mod> &x) {*this = *this * x; return *this;} int operator == (const typemod<mod> &x) const {return x.z == z;} int operator != (const typemod<mod> &x) const {return x.z != z;} }; typedef typemod<mod> Tm; #endif
const int maxn = 2e2 + 5; const int maxm = maxn << 1;
Tm c[maxn][maxn], pw2[maxn][maxn]; Tm ksm(Tm x,int mi) { Tm res(1); while(mi) { if(mi & 1) res = res * x; mi >>= 1; x = x * x; } return res; }
Tm pw[maxn * maxn];
void init() { c[0][0] = 1; int i, j;
for(i = 0; i < maxn; ++ i) { c[i][0] = 1; for(j = 1; j < maxn; ++ j) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
for(j = 0; j < maxn; ++ j) { pw2[i][j] = ksm(ksm(2, i) - 1, j); } } for(i = 0; i < maxn * maxn; ++ i) pw[i] = ksm(2, i); }
vector<int> vc[maxn]; int n, m;
void add(int u,int v) { vc[u].push_back(v); }
Tm f[maxn][maxn][maxn], g[maxn][maxn][maxn];
int dis[maxn];
int sum[maxn];
struct pii { int first, second; pii(int a = 0,int b = 0) : first(a), second(b) {} int operator < (const pii &b) const { if(first + second != b.first + b.second) return first + second < b.first + b.second; else return first < b.first; } int operator == (const pii &b) const { return first == b.first && second == b.second; } }; pii a[maxn];
#define mp pii
Tm F(int a,int x,int y) { Tm res(0); Tm vis[2] = {1, mod - 1}; for(int i = 0; i <= x; ++ i) res += vis[i & 1] * pw2[a - i][y] * c[x][i];
return res; }
void Solve() { int i, j; r1(n, m); Tm ans(1); for(i = 0; i <= n * 2; ++ i) vc[i].clear(), dis[i] = 0x3f3f3f3f; for(i = 0; i <= n * 2; ++ i) for(j = 0; j <= 2 * n; ++ j) for(int k = 0; k <= n * 2; ++ k) f[i][j][k] = g[i][j][k] = 0; for(i = 1; i <= m; ++ i) { int u, v; r1(u, v); add(u, v + n), add(v + n, u); add(u + n, v), add(v, u + n); }
static queue<int> q; while(!q.empty()) q.pop(); q.push(1); dis[1] = 0; while(!q.empty()) { int x = q.front(); q.pop(); for(i = 0; i < vc[x].size(); ++ i) { int to = vc[x][i]; if(dis[to] > dis[x] + 1) { dis[to] = dis[x] + 1; q.push(to); } } }
if(dis[1 + n] == 0x3f3f3f3f) {
for(i = 0; i <= n; ++ i) sum[i] = 0; for(i = 1; i <= n; ++ i) sum[min(dis[i], dis[i + n])] ++; for(i = 1; i <= n; ++ i) ans = ans * pw2[sum[i - 1]][sum[i]];
printf("%d\n", ans.z); return ; }
for(i = 1; i <= n; ++ i) a[i] = mp(min(dis[i], dis[i + n]), max(dis[i], dis[i + n])); sort(a + 1, a + n + 1); map<pii, int> ct; ans = 1; for(i = 1; i <= n; i = j + 1) { j = i - 1; while(j < n && a[j + 1] == a[i]) ++ j;
ct[a[i]] = j - i + 1; int nx = a[i].first, ny = a[i].second; int A = ct[mp(nx - 1, ny - 1)], B = ct[mp(nx - 1, ny + 1)], D = j - i + 1;
Tm vis[2] = {1, mod - 1}; if(!B) g[nx][ny][(i == 1 ? 0 : D)] = 1; else { for(int x = 0; x <= D; ++ x) { for(int y = 0; y <= B; ++ y) g[nx][ny][x] += f[nx - 1][ny + 1][y] * c[D][x] * F(B, y, D - x);
} }
for(int x = 0; x <= D; ++ x) { for(int y = 0; x + y <= D; ++ y) { f[nx][ny][x] += c[D - y][x] * pw2[A][D - x] * g[nx][ny][y]; }
} if(j == n || a[j + 1].first != nx + 1 || a[j + 1].second != ny - 1) { if(nx + 1 != ny) ans *= f[nx][ny][0]; else {
Tm tmp(0); for(int x = 0; x <= D; ++ x) { for(int y = 0; y <= x; ++ y) tmp += vis[y & 1] * c[x][y] * f[nx][ny][x] * pw[(D - y) * (D - y + 1) / 2]; }
ans *= tmp; } }
} printf("%d\n", ans.z); }
signed main() {
init(); int T; r1(T); while(T --) Solve(); return 0; }
|