Problem p03175 (C++)

Buggy submission (status: Runtime Error):
/*
    Written by Nitrogens
    Desire for getting accepted!!
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 1e5 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2)                                                           \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3)                                                       \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3        \
       << " = " << x3 << endl

int head[maxn], cnt, n;

struct edge {
  int v, nxt;
} Edge[maxn];

void init() {
  for (int i = 0; i <= n; i++)
    head[i] = -1;
  cnt = 0;
}

void addedge(int u, int v) {
  Edge[cnt].v = v;
  Edge[cnt].nxt = head[u];
  head[u] = cnt++;
}

ll dp[maxn][2];

void dfs(int id, int fa) {
  dp[id][0] = dp[id][1] = 1;
  for (int i = head[id]; i != -1; i = Edge[i].nxt) {
    int v = Edge[i].v;
    if (v == fa)
      continue;
    dfs(v, id);
    dp[id][0] = (dp[id][0] * dp[v][1]) % Mod;
    dp[id][1] = (dp[id][1] * ((dp[v][0] + dp[v][1]) % Mod)) % Mod;
  }
  /*
  if(!dp[id][0])  dp[id][0] = 1;
  if(!dp[id][1])  dp[id][1] = 1;
  */
}

int main() {
  // ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);

  int u, v;
  scanf("%d", &n);
  init();

  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }

  dfs(1, -1);

  printf("%lld\n", (dp[1][0] + dp[1][1]) % Mod);

  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}


Fixed submission:
/*
    Written by Nitrogens
    Desire for getting accepted!!
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 1e5 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2)                                                           \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3)                                                       \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3        \
       << " = " << x3 << endl

int head[maxn], cnt, n;

struct edge {
  int v, nxt;
} Edge[2 * maxn];

void init() {
  for (int i = 0; i <= n; i++)
    head[i] = -1;
  cnt = 0;
}

void addedge(int u, int v) {
  Edge[cnt].v = v;
  Edge[cnt].nxt = head[u];
  head[u] = cnt++;
}

ll dp[maxn][2];

void dfs(int id, int fa) {
  dp[id][0] = dp[id][1] = 1;
  for (int i = head[id]; i != -1; i = Edge[i].nxt) {
    int v = Edge[i].v;
    if (v == fa)
      continue;
    dfs(v, id);
    dp[id][0] = (dp[id][0] * dp[v][1]) % Mod;
    dp[id][1] = (dp[id][1] * ((dp[v][0] + dp[v][1]) % Mod)) % Mod;
  }
  /*
  if(!dp[id][0])  dp[id][0] = 1;
  if(!dp[id][1])  dp[id][1] = 1;
  */
}

int main() {
  // ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);

  int u, v;
  scanf("%d", &n);
  init();

  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }

  dfs(1, -1);

  printf("%lld\n", (dp[1][0] + dp[1][1]) % Mod);

  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}