Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 1,251 Bytes
d3f4f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <set>
#include <utility>
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<pair<int, int>> 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;
}