Problem:
Score : 1800 points

Problem Statement

There is a tree T with N vertices, numbered 1 through N.
For each 1 ≤ i ≤ N - 1, the i-th edge connects vertices a_i and b_i.

Snuke is constructing a directed graph T' by arbitrarily assigning direction to each edge in T. (There are 2^{N - 1} different ways to construct T'.)

For a fixed T', we will define d(s,\ t) for each 1 ≤ s,\ t ≤ N, as follows:

d(s,\ t) = (The number of edges that must be traversed against the assigned direction when traveling from vertex s to vertex t)

In particular, d(s,\ s) = 0 for each 1 ≤ s ≤ N.
Also note that, in general, d(s,\ t) ≠ d(t,\ s).

We will further define D as the following:

Snuke is constructing T' so that D will be the minimum possible value.
How many different ways are there to construct T' so that D will be the minimum possible value, modulo 10^9 + 7?

Constraints

2 ≤ N ≤ 1000

1 ≤ a_i,\ b_i ≤ N

The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_{N - 1} b_{N - 1}

Output

Print the number of the different ways to construct T' so that D will be the minimum possible value, modulo 10^9 + 7.

Sample Input 1

4
1 2
1 3
1 4

Sample Output 1

2

The minimum possible value for D is 1.
There are two ways to construct T' to achieve this value, as shown in the following figure:

Sample Input 2

4
1 2
2 3
3 4

Sample Output 2

6

The minimum possible value for D is 2.
There are six ways to construct T' to achieve this value, as shown in the following figure:

Sample Input 3

6
1 2
1 3
1 4
2 5
2 6

Sample Output 3

14

Sample Input 4

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

Sample Output 4

102

Solution in C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Text;
using System.Collections.Generic;
class Solve{
    int N;
    List<int>[] T;
    bool[] b;
    long[,] DP;
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        N = int.Parse(Console.ReadLine());
        T = new List<int>[N];
        for(int i=0;i<N;i++){
            T[i] = new List<int>();
        }
        for(int i=0;i<N-1;i++){
            string[] str = Console.ReadLine().Split(' ');
            int a = int.Parse(str[0])-1;
            int b = int.Parse(str[1])-1;
            T[a].Add(b);
            T[b].Add(a);
        }
        int center1;
        int center2;
        int diamater;
        {
            Tree G = new Tree(T);
            G.Center();
            center1 = G.center1;
            center2 = G.center2;
            diamater = G.diamater;
        }
        if(center2 == -1){
            long count = 0;
            b = new bool[N];
            DP = new long[N,2*N];
            dfs(center1,diamater/2,N);
            for(int i=0;i<2*N;i++){
                count = (count + DP[center1,i])%Define.mod;
            }
            sb.Append(count+"\n");
        }
        else{
            long count = 0;
            b = new bool[N];
            DP = new long[N,2*N];
            b[N+1] = true;
            b[center2] = true;
            Console.WriteLine(center1+" "+center2);
            dfs(center1,diamater/2+1,N-1);
            dfs(center2,diamater/2,N-1);
            for(int i=1;i<2*N-1;i++){
                count = (count + DP[center1,i]*(DP[center2,i-1]+DP[center2,i+1]))%Define.mod;
            }
            b = new bool[N];
            DP = new long[N,2*N];
            b[center2] = true;
            dfs(center1,diamater/2,N);
            dfs(center2,diamater/2+1,N);
            for(int i=1;i<2*N-1;i++){
                count = (count + DP[center1,i]*(DP[center2,i-1]+DP[center2,i+1]))%Define.mod;
            }
            b = new bool[N];
            DP = new long[N,2*N];
            b[center2] = true;
            dfs(center1,diamater/2,N);
            dfs(center2,diamater/2,N+1);
            for(int i=1;i<2*N-1;i++){
                count = (count + 2*(Define.mod-DP[center1,i])*(DP[center2,i-1]+DP[center2,i+1]))%Define.mod;
            }
            sb.Append(count+"\n");
        }

    }
    void dfs(int v,int l,int Z){
        b[v] = true;
        for(int i=Z-l;i<=Z+l;i++){
            DP[v,i] = 1;
        }
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                dfs(t,l-1,Z);
                for(int j=Z-l;j<=Z+l;j++){
                    DP[v,j] = DP[v,j]*(DP[t,j+1]+DP[t,j-1])%Define.mod;
                }
            }
        }
        
    }
}
public class Tree{
    List<int>[] T;
    bool[] b;
    int N;
    int temp1;
    int temp2;
    public int center1;
    public int center2;
    public int diamater;
    public Tree(List<int>[] G){
        T = G;
        N = G.Length;
    } 
    void calcLongestPath(int v){
        b = new bool[N];
        temp1 = 0;
        temp2 = 0;
        dfs1(v,0);
    }
    public int LongestPathDistance(int v){
        calcLongestPath(v);
        return temp1;
    }
    public int LongestPath(int v){
        calcLongestPath(v);
        return temp2;
    }
    public void Diamater(){
        int x1 = LongestPath(0);
        int x2 = LongestPath(x1);
        diamater = temp1;
    }
    public void Center(){
        int x1 = LongestPath(0);
        int x2 = LongestPath(x1);
        diamater = temp1;
        if(diamater % 2 == 0){
            center1 = Search(x1,x2,diamater/2);
            center2 = -1;
        }
        else{
            center1 = Search(x1,x2,diamater/2);
            center2 = Search(x1,x2,diamater/2+1);
        }
    }
    public int Search(int s,int t,int l){
        b = new bool[N];
        dfs2(s,t,0,l);
        return temp1;
    }
    void dfs1(int v,int l){
        b[v] = true;
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                dfs1(t,l+1);
            }
        }
        if(l >= temp1){
            temp1 = l;
            temp2 = v;
        }
    }
    bool dfs2(int v,int o,int l,int d){
        b[v] = true;
        bool x = v == o;
        for(int i=0;i<T[v].Count;i++){
            int t = T[v][i];
            if(!b[t]){
                x |= dfs2(t,o,l+1,d);
            }
        }
        if(x && l == d){
            temp1 = v;
        }
        return true;
    }
}
public static class Define{
    public const long mod = 1000000007;
}