#include using namespace std; #define N 200007 #define p 1000000007 #define LL long long LL fac[N], inv[N]; LL C(int n, int m) { return fac[n] * inv[m] % p * inv[n - m] % p; } LL mpow(LL x, LL y) { LL ans = 1LL; while (y) { if (y & 1LL) (ans *= x) %= p; (x *= x) %= p; y >>= 1; } return ans; } int main() { fac[0] = inv[0] = 1; for (int i = 1; i < N; i++) { fac[i] = fac[i - 1] * i % p; inv[i] = mpow(fac[i], p - 2); } int T, n; scanf("%d", &T); while (T--) { scanf("%d", &n); LL ans = 1LL; int now = 0; int tmp = 0; int x, y; for (int i = 1; i <= n; i++) { scanf("%d%d", &x, &y); if (x != tmp) { (ans *= C(now, now >> 1)) %= p; now = 1; tmp = x; } else now++; if (x == y) now++; else { if (now & 1) { (ans *= C(now, now >> 1)) %= p; (ans *= 2) %= p; } else { (ans *= C(now, (now >> 1) - 1) + C(now, now >> 1)) %= p; } now = 1; tmp = y; } } (ans *= C(now, now >> 1)) %= p; printf("%I64d\n", ans); } return 0; }