import sys from collections import defaultdict, deque, Counter import math # import copy from bisect import bisect_left, bisect_right import heapq sys.setrecursionlimit(10000000) # input aliases input = sys.stdin.readline getS = lambda: input().strip() getN = lambda: int(input()) getList = lambda: list(map(int, input().split())) getZList = lambda: [int(x) - 1 for x in input().split()] INF = 10 ** 20 MOD = 10 ** 9 + 7 divide = lambda x: pow(x, MOD - 2, MOD) ans = 0 def nck(n, k, kaijyo): return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD def npk(n, k, kaijyo): if k == 0 or k == n: return n % MOD return (kaijyo[n] * divide(kaijyo[n - k])) % MOD def kaijyo(n): ret = [1] for i in range(1, n + 1): ret.append((ret[-1] * i)% MOD) return ret def kaijyo_kai(n, k): ret = [1] for i in range(1, n + 1): ret.append((ret[-1] * (k+i))% MOD) return ret def dfs(tree, cur, d): global ans ans += int(d * (d+1) // 2) for nx in tree[cur]: dfs(tree, nx, d + 1) def solve(): global ans n = getN() tree = [[] for i in range(n)] par = [0 for i in range(n)] for i in range(n-1): a, b = getZList() tree[a].append(b) par[b] = 1 for i, tr in enumerate(par): if not tr: root = i break dfs(tree, root, 0) print(ans % MOD) def main(): n = getN() for _ in range(n): solve() if __name__ == "__main__": # main() solve()