#include #include #include #include #include #include using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; using LL = int64_t; int inv_t[maxn]; void inv_table(int n, int mod) { inv_t[1] = 1; for (int i = 2; i < n; ++i) inv_t[i] = (mod - 1LL * (mod / i) * inv_t[mod % i] % mod) % mod; } LL fac[maxn], ifac[maxn]; void init_fac(int n, int mod) { fac[0] = ifac[0] = 1; inv_table(n, mod); for (int i = 1; i < n; ++i) { fac[i] = fac[i - 1] * i % mod; ifac[i] = ifac[i - 1] * inv_t[i] % mod; } } LL C(int a, int b) { return (fac[a] * ifac[b] % mod) * ifac[a - b] % mod; } LL iC(int a, int b) { return (ifac[a] * fac[b] % mod) * fac[a - b] % mod; } void solve() { int n; cin >> n; int d = 0, c = 0, ca = 0, cb = 0; int64_t ma = 1, mb = 0; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; int na = 0, nb = 0; if (a != b) { if (a != d) { nb = (ma + mb) % mod; na = nb; } else { nb = (ma * iC(c, ca) % mod) * C(c + 1, ca + 1) % mod + (mb * iC(c, cb) % mod) * C(c + 1, cb + 1) % mod; nb %= mod; na = nb; } c = 1; ca = 1; cb = 0; d = b; ma = na, mb = nb; } else { if (a != d) { nb = (ma + mb) % mod; na = nb; c = 2; ca = cb = 1; } else { na = (ma * iC(c, ca) % mod) * C(c + 2, ca + 1) % mod; nb = (mb * iC(c, cb) % mod) * C(c + 2, cb + 1) % mod; c += 2; ca += 1, cb += 1; } d = b; ma = na, mb = nb; } // cout << i << ": " << a << ',' << b << ": " << ma << ", " << mb << endl; } cout << (ma + mb) % mod << endl; } int main() { init_fac(maxn, mod); ios::sync_with_stdio(false); int T; cin >> T; while (T-- > 0) { solve(); } return 0; }