// 演算法 / Algorithm:
// 1) 用 BFS 從根節點 1 求最大深度 d。 BFS from root to find max depth d.
// 2) 回傳 2^(d-1) mod 1e9+7。 Return 2^(d-1) mod 1e9+7.

class Solution {
    static const long MOD = 1000000007L;  // 取模常數 / the modulus 10^9 + 7

    // 快速冪 / fast exponentiation: base^e mod MOD in O(log e)
    long power(long base, long e) {
        long result = 1;                   // 累積乘積 / running product
        base %= MOD;
        while (e > 0) {                    // 逐位處理指數 / process exponent bit by bit
            if (e & 1) result = result * base % MOD;  // 此位為 1 就乘入 / fold in when bit set
            base = base * base % MOD;       // base 平方 / square the base
            e >>= 1;                        // 指數右移 / shift exponent
        }
        return result;
    }

public:
    int assignEdgeWeights(vector<vector<int>>& edges) {
        int n = edges.size() + 1;          // 節點數 = 邊數 + 1 / nodes = edges + 1

        // 鄰接表：adj[u] 是 vector，存 u 的所有鄰居。
        // Adjacency list: adj[u] is a vector holding u's neighbors.
        vector<vector<int>> adj(n + 1);    // 索引 0 不用，1..n / index 0 unused
        for (auto& e : edges) {            // range-for：依序取出每條邊 / range-for over edges
            adj[e[0]].push_back(e[1]);     // push_back：往 vector 尾端加元素 / append neighbor
            adj[e[1]].push_back(e[0]);     // 無向邊兩端都要加 / undirected: both directions
        }

        // BFS 求最大深度 / BFS to find max depth
        vector<int> depth(n + 1, 0);       // 每個節點的深度，初始 0 / depths, all 0
        vector<bool> visited(n + 1, false);// 走訪標記 / visited flags
        queue<int> q;                      // STL 佇列，先進先出 / FIFO queue
        q.push(1);                         // 從根節點 1 出發 / start at root
        visited[1] = true;
        int maxDepth = 0;

        while (!q.empty()) {               // 佇列非空就繼續 / until queue drains
            int u = q.front(); q.pop();    // front 取隊首、pop 移除 / read and remove front
            maxDepth = max(maxDepth, depth[u]);  // 更新最大深度 / track the deepest level
            for (int w : adj[u]) {         // 走訪鄰居 / visit each neighbor
                if (!visited[w]) {         // 跳過已走訪（即父節點）/ skip the parent
                    visited[w] = true;
                    depth[w] = depth[u] + 1;  // 子深度 = 父深度 + 1 / child = parent + 1
                    q.push(w);             // 入列 / enqueue
                }
            }
        }

        // 答案 / answer = 2^(maxDepth-1) mod 1e9+7
        return (int)power(2, maxDepth - 1);
    }
};
