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

#include <algorithm>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

const ll M = 1000000007;

ll dfs(int u, const vector<int> *T) {
  if (T[u].empty())
    return 2;
  ll res = 1;
  rep(i, T[u].size()) {
    int v = T[u][i];
    res = res * dfs(v, T) % M;
  }
  return (res + 1) % M;
}

// ツ出ツ篠淞青板つェツ債つ々 1 ツづ?つ「ツつ、ツ青ァツ姪アツつゥツづァ,
// ツ連ツ個仰青ャツ閉ェツづ債δ仰ーツプツづ可姪伉つェツつュツづ?づつつ「ツつスツ形ツづ可づ按づゥ
// ツつアツづ個連ツ個仰青ャツ閉ェツづ可づつつ「ツづ?づ個嘉ーツづーツ仰?づ淞づゥ
ll solve(int n, const int *G) {
  vector<int>
      G2[1000]; // G
                // ツづ個陛督づ個古シツつォツづーツ逆ツ転ツつオツつスツグツδ可フ
  rep(u, n) if (G[u] != -1) G2[G[u]].push_back(u);

  // ツδ仰ーツプツ個淞出
  rep(u, n) {
    int v = u;
    bool vis[1000] = {};
    while (G[v] != -1 && !vis[v]) {
      vis[v] = true;
      v = G[v];
    }
    if (v == u) { // ツδ仰ーツプorツ債ェツつェツ個ゥツづつつゥツづ?つス
      ll res = 1;
      int v = u;
      do {
        // ツδ仰ーツプツ湘」ツづ個各ツ陳クツ点ツづーツ債ェツづ?つキツづゥツ姪伉づ可づつつ「ツづ?督堋つヲツづーツ仰?づ淞づ?
        // ツつサツづェツづァツづーツつゥツつッツつ?づュツつケツづゥ
        rep(i, G2[v].size()) {
          int w = G2[v][i];
          if (!vis[w])
            res = res * dfs(w, G2) % M;
        }
        v = G[v];
      } while (v != u && v != -1);
      return (res + 1) %
             M; // ツづ?づェツづ?選ツづ篠づ按つ「ツづ?つ「ツつ、ツ嘉ーツつェツつ?づゥツづ個づ?+1
    }
  }
  for (;;)
    ;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int to[1000];
  rep(u, n) to[u] = -1;
  rep(i, m) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    to[u] = v;
  }
  vector<int> G[1000]; // ツ鳴ウツ古シツグツδ可フ
  rep(u, n) if (to[u] != -1) {
    int v = to[u];
    G[u].push_back(v);
    G[v].push_back(u);
  }

  ll ans = 1;
  bool vis[1000] = {};
  rep(u, n) if (!vis[u]) {
    vis[u] = true;

    vector<int> cc;
    queue<int> Q;
    Q.push(u);
    while (
        !Q.empty()) { // ツ偲」ツ連ツ個仰青ャツ閉ェツつイツづ?づ可陳イツづ猟づゥ
      int u = Q.front();
      Q.pop();
      cc.push_back(u);
      rep(i, G[u].size()) {
        int v = G[u][i];
        if (!vis[v])
          vis[v] = true, Q.push(v);
      }
    }

    int N = cc.size();
    int D[1000]; // ツ偲」ツ連ツ個仰づ按有ツ古シツグツδ可フ
    rep(i, N) D[i] = -1;
    rep(i, N) rep(j, N) {
      int u = cc[i], v = cc[j];
      if (to[u] == v)
        D[i] = j;
    }
    ans = ans * solve(N, D) % M;
  }

  printf("%lld\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;

const ll M = 1000000007;

ll dfs(int u, const vector<int> *T) {
  if (T[u].empty())
    return 2;
  ll res = 1;
  rep(i, T[u].size()) {
    int v = T[u][i];
    res = res * dfs(v, T) % M;
  }
  return (res + 1) % M;
}

// ツ出ツ篠淞青板つェツ債つ々 1 ツづ?つ「ツつ、ツ青ァツ姪アツつゥツづァ,
// ツ連ツ個仰青ャツ閉ェツづ債δ仰ーツプツづ可姪伉つェツつュツづ?づつつ「ツつスツ形ツづ可づ按づゥ
// ツつアツづ個連ツ個仰青ャツ閉ェツづ可づつつ「ツづ?づ個嘉ーツづーツ仰?づ淞づゥ
ll solve(int n, const int *G) {
  vector<int>
      G2[1000]; // G
                // ツづ個陛督づ個古シツつォツづーツ逆ツ転ツつオツつスツグツδ可フ
  rep(u, n) if (G[u] != -1) G2[G[u]].push_back(u);

  // ツδ仰ーツプツ個淞出
  rep(u, n) {
    int v = u;
    bool vis[1000] = {};
    while (G[v] != -1 && !vis[v]) {
      vis[v] = true;
      v = G[v];
    }
    if (v == u) { // ツδ仰ーツプorツ債ェツつェツ個ゥツづつつゥツづ?つス
      ll res = 1;
      int v = u;
      do {
        // ツδ仰ーツプツ湘」ツづ個各ツ陳クツ点ツづーツ債ェツづ?つキツづゥツ姪伉づ可づつつ「ツづ?督堋つヲツづーツ仰?づ淞づ?
        // ツつサツづェツづァツづーツつゥツつッツつ?づュツつケツづゥ
        rep(i, G2[v].size()) {
          int w = G2[v][i];
          if (!vis[w])
            res = res * dfs(w, G2) % M;
        }
        v = G[v];
      } while (v != u);
      return (res + 1) %
             M; // ツづ?づェツづ?選ツづ篠づ按つ「ツづ?つ「ツつ、ツ嘉ーツつェツつ?づゥツづ個づ?+1
    }
  }
  for (;;)
    ;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (n == 0 || n > 1000 || m > 1000)
    for (;;)
      ;
  int to[1000];
  rep(u, n) to[u] = -1;
  rep(i, m) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    if (u < 0 || v < 0 || n <= u || n <= v)
      for (;;)
        ;
    to[u] = v;
  }
  vector<int> G[1000]; // ツ鳴ウツ古シツグツδ可フ
  rep(u, n) if (to[u] != -1) {
    int v = to[u];
    G[u].push_back(v);
    G[v].push_back(u);
  }

  ll ans = 1;
  bool vis[1000] = {};
  rep(u, n) if (!vis[u]) {
    vis[u] = true;

    vector<int> cc;
    queue<int> Q;
    Q.push(u);
    while (
        !Q.empty()) { // ツ偲」ツ連ツ個仰青ャツ閉ェツつイツづ?づ可陳イツづ猟づゥ
      int u = Q.front();
      Q.pop();
      cc.push_back(u);
      rep(i, G[u].size()) {
        int v = G[u][i];
        if (!vis[v])
          vis[v] = true, Q.push(v);
      }
    }

    int N = cc.size();
    int D[1000]; // ツ偲」ツ連ツ個仰づ按有ツ古シツグツδ可フ
    rep(i, N) D[i] = -1;
    rep(i, N) rep(j, N) {
      int u = cc[i], v = cc[j];
      if (to[u] == v)
        D[i] = j;
    }
    ans = ans * solve(N, D) % M;
  }

  printf("%lld\\n", ans);

  return 0;
}