Problem:
Score : 900 points

Problem Statement

Let N be an even number.

There is a tree with N vertices.
The vertices are numbered 1, 2, ..., N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Snuke would like to decorate the tree with ribbons, as follows.

First, he will divide the N vertices into N / 2 pairs.
Here, each vertex must belong to exactly one pair.
Then, for each pair (u, v), put a ribbon through all the edges contained in the shortest path between u and v.

Snuke is trying to divide the vertices into pairs so that the following condition is satisfied: "for every edge, there is at least one ribbon going through it."
How many ways are there to divide the vertices into pairs, satisfying this condition?
Find the count modulo 10^9 + 7.
Here, two ways to divide the vertices into pairs are considered different when there is a pair that is contained in one of the two ways but not in the other.

Constraints

N is an even number.

2 \leq N \leq 5000

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print the number of the ways to divide the vertices into pairs, satisfying the condition, modulo 10^9 + 7.

Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2

There are three possible ways to divide the vertices into pairs, as shown below, and two satisfy the condition: the middle one and the right one.

Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

3

There are three possible ways to divide the vertices into pairs, as shown below, and all of them satisfy the condition.

Sample Input 3

6
1 2
1 3
3 4
1 5
5 6

Sample Output 3

10

Sample Input 4

10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

672

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::io::{self, BufRead};

#[allow(unused)]
macro_rules! input_at_once {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner_for_input_at_once!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner_for_input_at_once!{iter, $($r)*}
    };
}

#[allow(unused)]
macro_rules! input_inner_for_input_at_once {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value_for_input_at_once!($iter, $t);
        input_inner_for_input_at_once!{$iter $($r)*}
    };
}

#[allow(unused)]
macro_rules! read_value_for_input_at_once {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value_for_input_at_once!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value_for_input_at_once!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value_for_input_at_once!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value_for_input_at_once!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

const P: usize = 1_000_000_000 + 7;

fn dfs_size(
  g: &Vec<Vec<usize>>,
  size: &mut Vec<usize>,
  i: usize,
  used: &mut Vec<bool>,
) {
  used[i] = true;
  size[i] = 1;
  for v in g[i].iter() {
    if used[*v] {
      continue;
    }
    dfs_size(g, size, *v, used);
    size[i] += size[*v];
  }
}

fn dfs_solve(
  g: &Vec<Vec<usize>>,
  used: &mut Vec<bool>,
  size: &Vec<usize>,
  i: usize,
  pair_count: &Vec<usize>,
) -> Vec<[usize; 2]> {
  used[i] = true;
  let mut ret = vec![[0; 2]; 2];
  ret[1][0] = 1;
  let mut current_size = 1;
  for v in g[i].iter() {
    if used[*v] {
      continue;
    }
    let c = dfs_solve(g, used, size, *v, pair_count);
    let mut tmp = vec![[0; 2]; current_size + size[*v] + 1];
    for x in 1..current_size + 1 {
      for y in 1..size[*v] + 1 {
        for m1 in 0..2 {
          for m2 in 0..2 {
            tmp[x + y][(m1 + m2) % 2] += (ret[x][m1] * c[y][m2]) % P;
            tmp[x + y][(m1 + m2) % 2] %= P;
            tmp[x][(m1 + m2 + 1) % 2] +=
              (((ret[x][m1] * pair_count[y]) % P) * c[y][m2]) % P;
            tmp[x][(m1 + m2 + 1) % 2] %= P;
          }
        }
      }
    }
    ret = tmp;
    current_size += size[*v];
  }
  ret
}

fn main() {
  // pray for AC
  input!{
    n: usize,
    xy: [[usize; 2]; n-1],
  };
  let xy: Vec<_> = xy
    .into_iter()
    .map(|xyi| vec![xyi[0] - 1, xyi[1] - 1])
    .collect();
  let mut g = vec![vec![]; n];
  for xyi in xy.iter() {
    let x = xyi[0];
    let y = xyi[1];
    g[x].push(y);
    g[y].push(x);
  }

  let mut size = vec![0; n];
  dfs_size(&g, &mut size, 0, &mut vec![false; n]);

  //let mut dp = vec![vec![vec![0; 2]; n + 1]; n];
  let mut pair_count = vec![0; 5001];
  pair_count[0] = 1;
  for i in 2..5001 {
    if i % 2 == 1 {
      continue;
    }
    pair_count[i] = (pair_count[i - 2] * (i - 1)) % P;
  }
  let dp = dfs_solve(&g, &mut vec![false; n], &size, 0, &pair_count);

  let mut ans = 0;
  for s in 1..size[0] + 1 {
    ans += ((dp[s][0] * pair_count[s]) % P
      + (P - ((dp[s][1] * pair_count[s]) % P)))
      % P;
    ans %= P;
  }
  println!("{}", ans);
}