Problem p03969 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mod = 1e9 + 7, MaxN = 105;

int n, m, k, ans = 1, pk[MaxN], c[MaxN][MaxN];
vector<int> edge[MaxN];

void preprocess() {
  int n = MaxN - 1;
  for (int i = 0; i <= n; i++) {
    pk[i] = i ? (ll)pk[i - 1] * k % mod : 1;
    for (int j = c[i][0] = 1; j <= i; j++)
      c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
  }
}

int s[MaxN], s_n;
int dfn[MaxN], low[MaxN], bel[MaxN], dfs_n, c_n;

void exgcd(int a, int b, int &x, int &y) {
  if (!b)
    x = 1, y = 0;
  else
    exgcd(b, a % b, y, x), y -= (a / b) * x;
}
int mod_inv(int a) {
  int x, y;
  exgcd(a, mod, x, y);
  return (x % mod + mod) % mod;
}
int cal(const vector<int> &v) {
  int total_edge = 0, n = v.size();
  for (int x : v)
    for (int y : edge[x])
      if (bel[y] == c_n)
        ++total_edge;
  total_edge >>= 1;
  if (total_edge == 1)
    return k;
  if (total_edge == n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      sum = (sum + pk[__gcd(i, n)]) % mod;
    }
    sum = (ll)sum * mod_inv(n) % mod;
    return sum;
  }
  return c[total_edge + k - 1][k - 1];
}

void dfs(int x, int fa) {
  s[++s_n] = x;
  dfn[x] = low[x] = ++dfs_n;
  for (int y : edge[x]) {
    if (y == fa)
      continue;
    if (dfn[y]) {
      low[x] = min(low[x], dfn[y]);
    } else {
      dfs(y, x);
      if (low[y] >= dfn[x]) {
        vector<int> tmp = {x};
        bel[x] = ++c_n;
        int t = -1;
        while (t != y)
          bel[t = s[s_n--]] = c_n, tmp.push_back(t);
        ans = (ll)ans * cal(tmp) % mod;
      } else
        low[x] = min(low[x], low[y]);
    }
  }
}
int main() {
  cin >> n >> m >> k;
  preprocess();

  for (int i = 1, u, v; i <= m; i++) {
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }

  for (int i = 1; i <= n; i++)
    if (!dfn[i])
      dfs(i, 0);

  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mod = 1e9 + 7, MaxN = 205;

int n, m, k, ans = 1, pk[MaxN], c[MaxN][MaxN];
vector<int> edge[MaxN];

void preprocess() {
  int n = MaxN - 1;
  for (int i = 0; i <= n; i++) {
    pk[i] = i ? (ll)pk[i - 1] * k % mod : 1;
    for (int j = c[i][0] = 1; j <= i; j++)
      c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
  }
}

int s[MaxN], s_n;
int dfn[MaxN], low[MaxN], bel[MaxN], dfs_n, c_n;

void exgcd(int a, int b, int &x, int &y) {
  if (!b)
    x = 1, y = 0;
  else
    exgcd(b, a % b, y, x), y -= (a / b) * x;
}
int mod_inv(int a) {
  int x, y;
  exgcd(a, mod, x, y);
  return (x % mod + mod) % mod;
}
int cal(const vector<int> &v) {
  int total_edge = 0, n = v.size();
  for (int x : v)
    for (int y : edge[x])
      if (bel[y] == c_n)
        ++total_edge;
  total_edge >>= 1;
  if (total_edge == 1)
    return k;
  if (total_edge == n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      sum = (sum + pk[__gcd(i, n)]) % mod;
    }
    sum = (ll)sum * mod_inv(n) % mod;
    return sum;
  }
  return c[total_edge + k - 1][k - 1];
}

void dfs(int x, int fa) {
  s[++s_n] = x;
  dfn[x] = low[x] = ++dfs_n;
  for (int y : edge[x]) {
    if (y == fa)
      continue;
    if (dfn[y]) {
      low[x] = min(low[x], dfn[y]);
    } else {
      dfs(y, x);
      if (low[y] >= dfn[x]) {
        vector<int> tmp = {x};
        bel[x] = ++c_n;
        int t = -1;
        while (t != y)
          bel[t = s[s_n--]] = c_n, tmp.push_back(t);
        ans = (ll)ans * cal(tmp) % mod;
      } else
        low[x] = min(low[x], low[y]);
    }
  }
}
int main() {
  cin >> n >> m >> k;
  preprocess();

  for (int i = 1, u, v; i <= m; i++) {
    cin >> u >> v;
    edge[u].push_back(v);
    edge[v].push_back(u);
  }

  for (int i = 1; i <= n; i++)
    if (!dfn[i])
      dfs(i, 0);

  cout << ans << "\n";
  return 0;
}