Problem p03114 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

int n, q, l, r, hv = 1, par[105], id[105], at[105], x[105], fm, im;
vector<int> gp;
pair<int, int> p[105];
int u;
long long inv9, ans, mul[105], mu, smu, dp[10][1 << 16], sum;

int find(int a) { return par[a] == a ? a : par[a] = find(par[a]); }

long long po(long long b, int pp) {
  long long re = 1;
  while (pp) {
    if (pp & 1)
      re = re * b % MOD;
    b = b * b % MOD;
    pp >>= 1;
  }
  return re;
}

int main() {
  inv9 = po(9, MOD - 2);
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &l, &r);
    p[i * 2] = {l - 1, i * 2};
    p[i * 2 + 1] = {r, i * 2 + 1};
  }
  sort(p, p + q * 2 + 1);
  for (int i = 1; i <= q * 2; i++) {
    if (p[i].first != p[i - 1].first) {
      x[hv] = p[i].first;
      par[hv] = hv;
      hv++;
    }
    at[p[i].second] = hv - 1;
  }
  for (int i = 0; i < q; i++)
    if (find(at[i * 2]) != find(at[i * 2 + 1]))
      par[par[at[i * 2]]] = par[at[i * 2 + 1]];
  memset(id, -1, sizeof(id[0]) * hv);
  gp.reserve(hv);
  for (int i = 0; i < hv; i++) {
    if (id[find(i)] == -1) {
      id[par[i]] = gp.size();
      gp.push_back(0);
    }
    gp[id[par[i]]] |= 1 << i;
  }
  ans = po(10, n - x[hv - 1]);
  for (int i = 1; i < hv; i++) {
    mul[i] = (po(10, x[i] - x[i - 1]) + MOD - 1) * inv9 % MOD;
    ans = ans * mul[i] % MOD;
    mul[i] = (mul[i] + 1) * po(mul[i], MOD - 2) % MOD;
  }
  dp[9][0] = 1;
  fm = (1 << gp.size()) - 1;
  for (int i = 1; i <= fm; i++) {
    mu = 1;
    u = 0;
    for (int j = 0; j < gp.size(); j++)
      if (i & (1 << j))
        u |= gp[j];
    for (int j = 1, bm = 3; j < hv; j++, bm <<= 1)
      if ((u & bm) == bm)
        mu = mu * mul[j] % MOD;
    im = i ^ fm;
    for (int j = 1; j <= 9; j++) {
      smu = mu * (j == 9 ? 1 : j) % MOD;
      if (dp[j][0])
        dp[j - 1][i] = (dp[j - 1][i] + dp[j][0] * smu) % MOD;
      for (int k = im; k; k = (k - 1) & im)
        if (dp[j][k])
          dp[j - 1][i | k] = (dp[j - 1][i | k] + dp[j][k] * smu) % MOD;
    }
  }
  for (int i = 0; i < 9; i++)
    sum += dp[i][fm];
  printf("%lld\\n", ans * sum % MOD);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MOD 1000000007

int n, q, l, r, hv = 1, par[31], id[31], at[31], x[31], fm, im;
vector<int> gp;
pair<int, int> p[31];
int u;
long long inv9, ans, mul[31], mu, smu, dp[10][1 << 15], sum;

int find(int a) { return par[a] == a ? a : par[a] = find(par[a]); }

long long po(long long b, int pp) {
  long long re = 1;
  while (pp) {
    if (pp & 1)
      re = re * b % MOD;
    b = b * b % MOD;
    pp >>= 1;
  }
  return re;
}

int main() {
  inv9 = po(9, MOD - 2);
  scanf("%d%d", &n, &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &l, &r);
    p[i * 2] = {l - 1, i * 2};
    p[i * 2 + 1] = {r, i * 2 + 1};
  }
  sort(p, p + q * 2 + 1);
  for (int i = 1; i <= q * 2; i++) {
    if (p[i].first != p[i - 1].first) {
      x[hv] = p[i].first;
      par[hv] = hv;
      hv++;
    }
    at[p[i].second] = hv - 1;
  }
  for (int i = 0; i < q; i++)
    if (find(at[i * 2]) != find(at[i * 2 + 1]))
      par[par[at[i * 2]]] = par[at[i * 2 + 1]];
  memset(id, -1, sizeof(id[0]) * hv);
  gp.reserve(hv);
  for (int i = 0; i < hv; i++) {
    if (id[find(i)] == -1) {
      id[par[i]] = gp.size();
      gp.push_back(0);
    }
    gp[id[par[i]]] |= 1 << i;
  }
  ans = po(10, n - x[hv - 1]);
  for (int i = 1; i < hv; i++) {
    mul[i] = (po(10, x[i] - x[i - 1]) + MOD - 1) * inv9 % MOD;
    ans = ans * mul[i] % MOD;
    mul[i] = (mul[i] + 1) * po(mul[i], MOD - 2) % MOD;
  }
  dp[9][0] = 1;
  fm = (1 << gp.size()) - 1;
  for (int i = 1; i <= fm; i++) {
    mu = 1;
    u = 0;
    for (int j = 0; j < gp.size(); j++)
      if (i & (1 << j))
        u |= gp[j];
    for (int j = 1, bm = 3; j < hv; j++, bm <<= 1)
      if ((u & bm) == bm)
        mu = mu * mul[j] % MOD;
    im = i ^ fm;
    for (int j = 1; j <= 9; j++) {
      smu = mu * (j == 9 ? 1 : j) % MOD;
      if (dp[j][0])
        dp[j - 1][i] = (dp[j - 1][i] + dp[j][0] * smu) % MOD;
      for (int k = im; k; k = (k - 1) & im)
        if (dp[j][k])
          dp[j - 1][i | k] = (dp[j - 1][i | k] + dp[j][k] * smu) % MOD;
    }
  }
  for (int i = 0; i < 9; i++)
    sum += dp[i][fm];
  printf("%lld\\n", ans * sum % MOD);
}