Problem p03405 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
#define MAXN 1000
#define MOD 1000000007
#define ll long long
#define rint register int
#define gc() getchar()
inline ll read(ll r = 0, int s = 0, int c = gc()) {
  for (; c < 48 || c > 57; s = c, c = gc())
    ;
  for (; c >= 48 && c <= 57; (r *= 10) += c - 48, c = gc())
    ;
  return s ^ '-' ? r : -r;
}
inline int fxp(int s, int n = MOD - 2) {
  int a = 1;
  for (; n; n & 1 ? a = 1ll * a * s % MOD : 0, s = 1ll * s * s % MOD, n >>= 1)
    ;
  return a;
}
struct Edge {
  int u, v, w;
} e[MAXN + 5];
inline bool operator<(Edge a, Edge b) { return a.w < b.w; }
int fa[MAXN + 5], n, m, L, M, R;
ll X, W, C, del[MAXN + 5];
inline int getf(int x) { return x ^ fa[x] ? fa[x] = getf(fa[x]) : x; }
inline bool Union(int x, int y) {
  return (x = getf(x)) ^ (y = getf(y)) ? fa[x] = y, true : false;
}
ll Kruskal(int c = n - 1) {
  for (rint i = 1; i <= m && Union(e[i].u, e[i].v) ? C += e[i].w, --c : true;
       i++)
    ;
  return C;
}

int main() {
  n = read(), m = read(), X = read();
  for (rint i = 1; i <= m;
       e[i].u = read(), e[i].v = read(), e[i].w = read(), i++)
    ;
  sort(e + 1, e + m + 1);
  for (rint i = 1; i <= n; fa[i] = i, i++)
    ;
  W = Kruskal();
  if (W > X)
    return puts("0"), 0;
  for (rint i = 1, j; i <= m;
       Union(e[i].u, e[i].v), C = e[i].w, Kruskal(n - 2), del[i] = C - W, i++)
    for (j = 1; j <= n; fa[j] = j, j++)
      ;
  for (rint i = 1; i <= m; L += del[i] < X - W, M += del[i] == X - W, i++)
    ;
  R = m - L - M;
  printf("%d\n", (1ll + !!L) * fxp(2, R) * (fxp(2, M) + MOD - 1 - !L) % MOD);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>

using namespace std;
#define MAXN 2000
#define MOD 1000000007
#define ll long long
#define rint register int
#define gc() getchar()
inline ll read(ll r = 0, int s = 0, int c = gc()) {
  for (; c < 48 || c > 57; s = c, c = gc())
    ;
  for (; c >= 48 && c <= 57; (r *= 10) += c - 48, c = gc())
    ;
  return s ^ '-' ? r : -r;
}
inline int fxp(int s, int n = MOD - 2) {
  int a = 1;
  for (; n; n & 1 ? a = 1ll * a * s % MOD : 0, s = 1ll * s * s % MOD, n >>= 1)
    ;
  return a;
}
struct Edge {
  int u, v, w;
} e[MAXN + 5];
inline bool operator<(Edge a, Edge b) { return a.w < b.w; }
int fa[MAXN + 5], n, m, L, M, R;
ll X, W, C, del[MAXN + 5];
inline int getf(int x) { return x ^ fa[x] ? fa[x] = getf(fa[x]) : x; }
inline bool Union(int x, int y) {
  return (x = getf(x)) ^ (y = getf(y)) ? fa[x] = y, true : false;
}
ll Kruskal(int c = n - 1) {
  for (rint i = 1; i <= m && Union(e[i].u, e[i].v) ? C += e[i].w, --c : true;
       i++)
    ;
  return C;
}

int main() {
  n = read(), m = read(), X = read();
  for (rint i = 1; i <= m;
       e[i].u = read(), e[i].v = read(), e[i].w = read(), i++)
    ;
  sort(e + 1, e + m + 1);
  for (rint i = 1; i <= n; fa[i] = i, i++)
    ;
  W = Kruskal();
  if (W > X)
    return puts("0"), 0;
  for (rint i = 1, j; i <= m;
       Union(e[i].u, e[i].v), C = e[i].w, Kruskal(n - 2), del[i] = C - W, i++)
    for (j = 1; j <= n; fa[j] = j, j++)
      ;
  for (rint i = 1; i <= m; L += del[i] < X - W, M += del[i] == X - W, i++)
    ;
  R = m - L - M;
  printf("%d\n", (1ll + !!L) * fxp(2, R) * (fxp(2, M) + MOD - 1 - !L) % MOD);
  return 0;
}