d6b00c700687e4e710588d660218edce432657b4211900c246ae63c586e891b3
// https://szkopul.edu.pl/problemset/problem/lbADmW7d353d0F0iw4kXTjsl/site/?key=statement
#include <bits/stdc++.h>
// #define GARY_DBG
#define GARY_LIB
constexpr int sizik = 1000 * 1001;
#define ar std::array
typedef std::vector<std::vector<int>> _kra;
constexpr int mod = 1e9 + 7;
void solve() {
int m, n;
std::cin >> m >> n;
std::vector<std::pair<int, int>> prz(n);
std::vector<int> coords;
coords.reserve(2 * n);
int max_a = 0;
for (auto& [a, b] : prz) {
std::cin >> a >> b;
coords.push_back(a);
coords.push_back(b);
max_a = std::max(max_a, a);
}
coords.push_back(0);
std::sort(coords.begin(), coords.end());
coords.erase(std::unique(coords.begin(), coords.end()), coords.end());
auto get_compr = [&coords](int x) -> int { return std::distance(coords.begin(), std::lower_bound(coords.begin(), coords.end(), x)); };
int t = coords.size() + 1;
std::vector<int> dp(t), cnt(t), pref_cnt(t), max_left_coord(t);
for (const auto& [a, b] : prz) {
int idx = get_compr(b);
max_left_coord[idx] = std::max(max_left_coord[idx], a);
}
for (int i = 1; i < t; i++) {
max_left_coord[i] = std::max(max_left_coord[i], max_left_coord[i - 1]);
}
cnt[0] = 1;
pref_cnt[0] = 1;
dp[0] = 0;
for (int i = 1; i < t - 2; i++) {
int L = 0;
if (max_left_coord[i] > 0) {
L = std::distance(coords.begin(), std::lower_bound(coords.begin(), coords.end(), max_left_coord[i]));
}
dp[i] = 1 + dp[L];
int R = i - 1;
if (dp[i - 1] == dp[L]) {
} else {
R = std::distance(dp.begin(), std::upper_bound(dp.begin(), dp.begin() + i, dp[L])) - 1;
}
int len = coords[i + 1] - coords[i];
int sum = (pref_cnt[R] - (L > 0 ? pref_cnt[L - 1] : 0) + mod) % mod;
cnt[i] = ((int64_t)len * (int64_t)(sum)) % mod;
pref_cnt[i] = (pref_cnt[i - 1] + cnt[i]) % mod;
}
int k_min = mod;
int max_a_coord = get_compr(max_a);
for (int i = max_a_coord; i < t - 2; i++) {
k_min = std::min(k_min, dp[i]);
}
int ans = 0;
for (int i = max_a_coord; i < t - 2; i++) {
if (dp[i] == k_min) {
ans += cnt[i];
if (ans >= mod) ans -= mod;
}
}
std::cout << k_min << " " << ans << '\n';
}
int32_t main() {
#ifndef GARY_DBG
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
#endif
int t = 1;
std::cin >> t;
for (; t > 0; t--) {
solve();
}
return 0;
}