#define CPP17 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef CPP17 #include #endif // Yay!! #define endl codeforces // macros for iterator #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) // alias using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; // variadic min/max template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } // variadic chmin/chmax template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } // multi demension array template struct multi_dim_array { using type = std::array::type, Head>; }; template struct multi_dim_array { using type = std::array; }; template using mdarray = typename multi_dim_array::type; #ifdef CPP17 // fill container template void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } } #endif // make multi dimension vector template vec make_v(ssize_t sz) { return vec(sz); } template auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v(std::forward(ts)...)); return vec(hs, v); } // init namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } namespace math { template constexpr T pow(const T &n, ll k) { T ret = n.mul_id_ele(); T cur = n; while (k) { if (k & 1) ret *= cur; cur *= cur; k /= 2; } return ret; } } namespace math { template struct Modint { constexpr Modint(ll x) noexcept : x((Mod + x % Mod) % Mod) { } constexpr Modint() noexcept : Modint(0) { } constexpr Modint add_id_ele() const noexcept { return Modint(0); } constexpr Modint mul_id_ele() const noexcept { return Modint(1); } constexpr ll& value() noexcept { return x; } constexpr ll value() const noexcept { return x; } constexpr Modint& operator +=(const Modint &oth) noexcept { x += oth.value(); if (Mod <= x) x -= Mod; return *this; } constexpr Modint& operator -=(const Modint &oth) noexcept { x += Mod - oth.value(); if (Mod <= x) x -= Mod; return *this; } constexpr Modint& operator *=(const Modint &oth) noexcept { x *= oth.value(); x %= Mod; return *this; } constexpr Modint& operator /=(const Modint &oth) noexcept { x *= oth.inv().value(); x %= Mod; return *this; } constexpr Modint operator +(const Modint &oth) const noexcept { return Modint(x) += oth; } constexpr Modint operator -(const Modint &oth) const noexcept { return Modint(x) -= oth; } constexpr Modint operator *(const Modint &oth) const noexcept { return Modint(x) *= oth; } constexpr Modint operator /(const Modint &oth) const noexcept { return Modint(x) /= oth; } constexpr Modint operator -() const noexcept { return Modint((x != 0) * (Mod - x)); } template constexpr typename std::enable_if::value, const Modint&>::type operator =(T t) noexcept { (*this) = Modint(std::forward(t)); return *this; } constexpr Modint inv() const noexcept { return ::math::pow(*this, Mod - 2); } constexpr ll mod() const noexcept { return Mod; } private: ll x; }; } namespace graph { using Node = ll; using Weight = ll; using Edge = std::pair; template struct Graph : public vvec { using vvec::vvec; void add_edge(Node f, Node t, Weight w = 1) { (*this)[f].emplace_back(t, w); if (!Directed) (*this)[t].emplace_back(f, w); } Graph build_inv() const { Graph ret(this->size()); for (Node i = 0; i < this->size(); i++) { for (const Edge &e : (*this)[i]) { Node j; Weight w; std::tie(j, w) = e; if (!Directed && j < i) continue; ret.add_edge(j, i, w); } } return ret; } }; template class dst_iterator { Iterator ite; public: dst_iterator(Iterator ite) : ite(ite) { } bool operator ==(const dst_iterator &oth) const { return ite == oth.ite; } bool operator !=(const dst_iterator &oth) const { return !(*this == oth); } bool operator <(const dst_iterator &oth) const { return ite < oth.ite; } bool operator >(const dst_iterator &oth) const { return ite > oth.ite; } bool operator <=(const dst_iterator &oth) const { return ite <= oth.ite; } bool operator >=(const dst_iterator &oth) const { return ite >= oth.ite; } const Node& operator *() { return ite->first; } const Node& operator *() const { return ite->first; } dst_iterator operator ++() { ++ite; return ite; } }; class dst_iteration { using ite_type = vec::const_iterator; const vec &edges; public: dst_iteration(const vec &edges) : edges(edges) { } auto begin() const { return dst_iterator(edges.cbegin()); } auto end() const { return dst_iterator(edges.cend()); } }; class dst_reverse_iteration { using ite_type = vec::const_reverse_iterator; const vec &edges; public: dst_reverse_iteration(const vec &edges) : edges(edges) { } auto begin() const { return dst_iterator(edges.crbegin()); } auto end() const { return dst_iterator(edges.crend()); } }; dst_iteration dst(const vec &edges) { return dst_iteration(edges); } dst_reverse_iteration rdst(const vec &edges) { return dst_reverse_iteration(edges); } } constexpr ll mod = 1e9 + 7; using mint = math::Modint; struct Solver { graph::Graph tree; ll k; vec par; vvec dp, rdp, dp_sum; vec> lsum, rsum; ll root = 0; Solver(ll n, ll k) : par(n), tree(n), k(k), dp(make_v(n, k + 1)), rdp(dp), dp_sum(make_v(n, k + 2)), lsum(n), rsum(n) { for (ll i = 1; i < n; i++) { ll a, b; std::cin >> a >> b; tree.add_edge(a - 1, b - 1); } } void dfs(ll cur, ll pre) { par[cur] = pre; dp[cur][0] = 0; ll cnt = 0; fill_seq(dp[cur], [](ll i) { return mint(!!i); }); for (ll nxt : graph::dst(tree[cur])) if (pre != nxt) { dfs(nxt, cur); cnt++; for (ll i = 1; i <= k; i++) dp[cur][i] *= dp_sum[nxt][i + 1]; } lsum[cur] = make_v(cnt + 1, k + 1); rsum[cur] = make_v(cnt + 1, k + 1); for (ll loop = 0; loop < 2; loop++) { auto &sum = (loop == 0 ? lsum[cur] : rsum[cur]); std::fill(ALL(sum[0]), mint(1)); ll idx = 0; fill_seq(sum[0], [](int) { return mint(1); }); auto update = [&](ll nxt) { if (nxt == pre) return; for (ll i = 1; i <= k; i++) { sum[idx + 1][i] += sum[idx][i] * dp_sum[nxt][i + 1]; sum[idx + 1][i] -= sum[idx][i - 1] * dp_sum[nxt][i]; } idx++; }; if (loop == 0) { for (ll nxt : graph::dst(tree[cur])) update(nxt); } else { for (ll nxt : graph::rdst(tree[cur])) update(nxt); } } for (ll i = 1; i <= k; i++) dp_sum[cur][i + 1] = dp_sum[cur][i] + dp[cur][i]; } void calc_rdp(ll cur, ll pidx) { if (cur == root) { fill_seq(rdp[cur], [](ll i) { return mint(!!i); }); } else { mint ls = 0, rs = 0, psum = 0; ll p = par[cur]; ll ch = lsum[p].size() - 1; if (pidx == 0) ls = 1; if (pidx == ch - 1) rs = 1; for (ll i = 1; i <= k; i++) { if (pidx != 0) ls += lsum[p][pidx][i]; if (ch - pidx - 1 != 0) rs += rsum[p][ch - pidx - 1][i]; psum += ls * rs * rdp[p][i]; rdp[cur][i] = psum; } } ll idx = 0; for (ll nxt : graph::dst(tree[cur])) if (nxt != par[cur]) calc_rdp(nxt, idx++); } mint solve() { dfs(root, -1); calc_rdp(root, -1); mint ans = 0; for (ll n = 0; n < tree.size(); n++) for (ll i = 1; i <= k; i++) { if (n == root) ans += dp[n][i]; else ans += dp[n][i] * rdp[n][i - 1]; } return ans; } }; int main() { ll n, k; std::cin >> n >> k; Solver solver(n, k); std::cout << solver.solve().value() << "\n"; return 0; }