intmain() { std::cin >> n >> x >> m; for (int i = 1; i <= m; ++ i) scanf("%d %d", &a[i].l, &a[i].r); std::sort(a + 1, a + m + 1, [&](Interval &v1, Interval &v2) { return v1.r < v2.r; }); int top = 0; for (int i = 1; i <= m; ++ i) { bool flag = true; for (int j = 1; j <= m && flag; ++ j) flag &= !(a[i].r >= a[j].r && a[i].l <= a[j].l) || (a[i].l == a[j].l && a[i].r == a[j].r); if (flag) tmp[++ top] = a[i]; } std::swap(a, tmp), m = top;
for (int i = 1; i <= m; ++ i) g[a[i].r].push_back(i); f[0][0] = pre1[0][0] = pre2[0][0] = Mod - 1; for (int r = 1; r <= n; ++ r) { for (int id : g[r]) { int l = a[id].l; for (int len = r - l + 1, ls = r - l + 1; len <= r; ++ len) f[r][len] = (f[r][len] - (LL) pre1[l - 1][len - ls] - (pre2[r - 1][r - len] - pre2[l - 1][r - len]) - f[r][len] + 4LL * Mod) % Mod; } for (int len = 0; len <= r; ++ len) adj(pre1[r][len] = pre1[r - 1][len] + f[r][len] - Mod), adj(pre2[r][r - len] = pre2[r - 1][r - len] + f[r][len] - Mod); } int res = 0; for (int val = 1; val <= x; ++ val) { int ak = qpow(x) * (LL) (x - val) % Mod, p = ak, &cur = pro[val]; for (int j = 1; j <= n; ++ j, p = (LL) p * ak % Mod) cur = (cur + (LL) p * pre1[n][j]) % Mod; adj(cur = 1 - cur); // std::cout << val << ' ' << cur << '\n'; adj(cur -= pro[val - 1]), res = (res + (LL) cur * val) % Mod; // std::cout << val << ' ' << cur << '\n'; adj(cur += pro[val - 1] - Mod); } std::cout << res << '\n'; return0; }