Problem p03969 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 55, M = 210, p = 1e9 + 7;
int n, m, k, ans = 1, dclk, dfn[N], fac[M], inv[M], fa[N];
int cnt, rt[N], si[N], cn[N], col[N];
int tot, fi[N], a[M], ne[M];
inline int Qmi(int x, int y) {
  int ans = 1;
  for (; y; x = 1ll * x * x % p, y >>= 1)
    if (y & 1)
      ans = 1ll * ans * x % p;
  return ans;
}
inline void Add(int x, int y) {
  a[++tot] = y;
  ne[tot] = fi[x];
  fi[x] = tot;
}
inline int Ask(int x) { return rt[x] == x ? x : rt[x] = Ask(rt[x]); }
inline void A(int &x, int y) {
  x += y;
  if (x >= p)
    x -= p;
}
inline int C(int x, int y) {
  return 1ll * fac[x] * inv[y] % p * inv[x - y] % p;
}
inline void Dfs(int x) {
  dfn[x] = ++dclk;
  for (int i = fi[x]; i; i = ne[i])
    if (!dfn[a[i]]) {
      fa[a[i]] = x;
      Dfs(a[i]);
    } else {
      if (a[i] == fa[x] || dfn[a[i]] > dfn[x])
        continue;
      ++cnt;
      rt[cnt] = cnt;
      si[cnt] = 1;
      for (int t = x; t != a[i]; t = fa[t])
        if (col[t]) {
          int u = Ask(col[t]), v = Ask(cnt);
          if (u == v)
            continue;
          rt[u] = v;
          si[v] += si[u];
        } else
          col[t] = cnt, ++si[Ask(cnt)];
    }
}
int main() {
  fac[0] = 1;
  for (int i = 1; i < M; ++i)
    fac[i] = 1ll * fac[i - 1] * i % p;
  inv[0] = inv[1] = 1;
  for (int i = 2; i < M; ++i)
    inv[i] = 1ll * inv[p % i] * (p - p / i) % p;
  for (int i = 2; i < M; ++i)
    inv[i] = 1ll * inv[i - 1] * inv[i] % p;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1, x, y; i <= m; ++i)
    scanf("%d%d", &x, &y), Add(x, y), Add(y, x);
  for (int i = 1; i <= n; ++i)
    if (!dfn[i])
      Dfs(i), col[i] = -1;
  for (int i = 1; i <= cnt; ++i)
    ++cn[Ask(i)];
  for (int i = 1; i <= n; ++i)
    if (!col[i])
      ans = 1ll * ans * k % p;
  for (int i = 1; i <= cnt; ++i)
    if (cn[i] == 1) {
      int sm = 0;
      for (int j = 0; j < si[i]; ++j)
        A(sm, Qmi(k, __gcd(j, si[i])));
      sm = 1ll * sm * Qmi(si[i], p - 2) % p;
      ans = 1ll * ans * sm % p;
    } else if (cn[i] > 1) {
      ans = 1ll * ans * C(si[i] + k - 1, k - 1) % p;
    }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 550, M = 5500, p = 1e9 + 7;
int n, m, k, ans = 1, dclk, dfn[N], fac[M], inv[M], fa[N];
int cnt, rt[N], si[N], cn[N], col[N];
int tot, fi[N], a[M], ne[M];
inline int Qmi(int x, int y) {
  int ans = 1;
  for (; y; x = 1ll * x * x % p, y >>= 1)
    if (y & 1)
      ans = 1ll * ans * x % p;
  return ans;
}
inline void Add(int x, int y) {
  a[++tot] = y;
  ne[tot] = fi[x];
  fi[x] = tot;
}
inline int Ask(int x) { return rt[x] == x ? x : rt[x] = Ask(rt[x]); }
inline void A(int &x, int y) {
  x += y;
  if (x >= p)
    x -= p;
}
inline int C(int x, int y) {
  return 1ll * fac[x] * inv[y] % p * inv[x - y] % p;
}
inline void Dfs(int x) {
  dfn[x] = ++dclk;
  for (int i = fi[x]; i; i = ne[i])
    if (!dfn[a[i]]) {
      fa[a[i]] = x;
      Dfs(a[i]);
    } else {
      if (a[i] == fa[x] || dfn[a[i]] > dfn[x])
        continue;
      ++cnt;
      rt[cnt] = cnt;
      si[cnt] = 1;
      for (int t = x; t != a[i]; t = fa[t])
        if (col[t]) {
          int u = Ask(col[t]), v = Ask(cnt);
          if (u == v)
            continue;
          rt[u] = v;
          si[v] += si[u];
        } else
          col[t] = cnt, ++si[Ask(cnt)];
    }
}
int main() {
  fac[0] = 1;
  for (int i = 1; i < M; ++i)
    fac[i] = 1ll * fac[i - 1] * i % p;
  inv[0] = inv[1] = 1;
  for (int i = 2; i < M; ++i)
    inv[i] = 1ll * inv[p % i] * (p - p / i) % p;
  for (int i = 2; i < M; ++i)
    inv[i] = 1ll * inv[i - 1] * inv[i] % p;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1, x, y; i <= m; ++i)
    scanf("%d%d", &x, &y), Add(x, y), Add(y, x);
  for (int i = 1; i <= n; ++i)
    if (!dfn[i])
      Dfs(i), col[i] = -1;
  for (int i = 1; i <= cnt; ++i)
    ++cn[Ask(i)];
  for (int i = 1; i <= n; ++i)
    if (!col[i])
      ans = 1ll * ans * k % p;
  for (int i = 1; i <= cnt; ++i)
    if (cn[i] == 1) {
      int sm = 0;
      for (int j = 0; j < si[i]; ++j)
        A(sm, Qmi(k, __gcd(j, si[i])));
      sm = 1ll * sm * Qmi(si[i], p - 2) % p;
      ans = 1ll * ans * sm % p;
    } else if (cn[i] > 1) {
      ans = 1ll * ans * C(si[i] + k - 1, k - 1) % p;
    }
  printf("%d\n", ans);
  return 0;
}