#include #include #include #include #include using namespace std; const int MOD = 1000000007; long long get_seg_pairs(int a, int b) { long long c = b - a + 1; return c * (c - 1) / 2; } int solve() { // Input. int N, M; cin >> N >> M; long long curr = (long long)N * (N - 1) / 2; set> S; for (int i = 1; i <= N; i++) { curr += get_seg_pairs(i, i); S.emplace(i, i); } int ans = 1; // Process partnerships. for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; if (a > b) { swap(a, b); } // Find segments containing a and b. auto it = prev(S.lower_bound({a + 1, 0})); a = it->first; b = prev(S.lower_bound({b + 1, 0}))->second; // Erase all segments between those, inclusive. while (it != S.end() && it->second <= b) { curr -= get_seg_pairs(it->first, it->second); it = S.erase(it); } // Insert new merged segment. curr += get_seg_pairs(a, b); S.emplace(a, b); // Update answer. ans = ans * (curr % MOD) % MOD; } return ans; } int main() { int T; cin >> T; for (int t = 1; t <= T; t++) { cout << "Case #" << t << ": " << solve() << endl; } return 0; }