#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 template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; const long double PI = acos((long double)(-1)); using namespace std; typedef unsigned long long ull; typedef long long ll; struct mint { long long x; mint(long long x = 0) :x((x% mod + mod) % mod) {} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; int n, k; vector> g; vector> dp; mint res; vector in, out; void dfs_eur(int cur, int pre, int& idx) { in[cur] = idx++; for (auto next : g[cur]) { if (next == pre) continue; dfs_eur(next, cur, idx); } out[cur] = idx; } void dfs1(int cur, int pre) { for (int i = 0; i < k; i++) dp[cur][i] = 1; for (auto next : g[cur]) { if (next == pre) continue; dfs1(next, cur); mint sum = 0; for (int i = 0; i < k; i++) { sum += dp[next][i]; dp[cur][i] *= sum; } } } void dfs2(int cur, int pre) { { vector ans(k, 1); for (auto next : g[cur]) { mint sum = 0; if (in[cur] < in[next]) { for (int i = 0; i < k; i++) { sum += dp[next][i]; ans[i] *= sum; } } else { ans[0] = 0; for (int i = 1; i < k; i++) { sum += dp[next][i - 1]; ans[i] *= sum; } } } for (int i = 0; i < k; i++) res += ans[i]; } vector> left(k, vector(g[cur].size() + 1, 1)), right(k, vector(g[cur].size() + 1, 1)); for (int i = 0; i < g[cur].size(); i++) { int next = g[cur][i]; mint sum = 0; for (int j = 0; j < k; j++) { sum += dp[next][j]; left[j][i + 1] = left[j][i] * sum; } } for (int i = (int)g[cur].size() - 1; i >= 0; i--) { int next = g[cur][i]; mint sum = 0; for (int j = 0; j < k; j++) { sum += dp[next][j]; right[j][i] = right[j][i + 1] * sum; } } for (int i = 0; i < g[cur].size(); i++) { int next = g[cur][i]; if (next == pre) continue; auto tmp1 = dp[cur]; auto tmp2 = dp[next]; for (int j = 0; j < k; j++) { dp[cur][j] = left[j][i] * right[j][i + 1]; } mint sum = 0; for (int j = 0; j < k; j++) { sum += dp[cur][j]; dp[next][j] *= sum; } dfs2(next, cur); dp[cur] = tmp1; dp[next] = tmp2; } } int main() { /* cin.tie(nullptr); ios::sync_with_stdio(false); */ scanf("%d %d", &n, &k); g.resize(n); dp = vector>(n, vector(k)); for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; g[u].emplace_back(v); g[v].emplace_back(u); } { in.resize(n), out.resize(n); int idx = 0; dfs_eur(0, -1, idx); } dfs1(0, -1); dfs2(0, -1); cout << res.x << "\n"; return 0; }