#include <bits/stdc++.h>   using namespace std;   template <typename T1, typename T2>   istream &operator>>(istream &in, pair<T1, T2> &a) {     in >> a.first >> a.second;     return in;   }   template <typename T1, typename T2>   ostream &operator<<(ostream &out, pair<T1, T2> a) {     out << a.first <<     << a.second;     return out;   }   template <typename T, typename T1>   T maxs(T &a, T1 b) {     if (b > a) a = b;     return a;   }   template <typename T, typename T1>   T mins(T &a, T1 b) {     if (b < a) a = b;     return a;   }   const long long MOD = 1000000007;   struct mod_int {     long long val;     mod_int(long long v = 0) {       if (v < 0) v = v % MOD + MOD;       if (v >= MOD) v %= MOD;       val = v;     }     static long long mod_inv(long long a, long long m = MOD) {       long long g = m, r = a, first = 0, second = 1;       while (r != 0) {         long long q = g / r;         g %= r;         swap(g, r);         first -= q * second;         swap(first, second);       }       return first < 0 ? first + m : first;     }     explicit operator long long() const { return val; }     mod_int &operator+=(const mod_int &other) {       val += other.val;       if (val >= MOD) val -= MOD;       return *this;     }     mod_int &operator-=(const mod_int &other) {       val -= other.val;       if (val < 0) val += MOD;       return *this;     }     static unsigned fast_mod(uint64_t first, unsigned m = MOD) {       return first % m;       unsigned x_high = first >> 32, x_low = (unsigned)first;       unsigned quot, rem;       asm( divl %4 n  :  =a (quot),  =d (rem) :  d (x_high),  a (x_low),  r (m));       return rem;     }     mod_int &operator*=(const mod_int &other) {       val = fast_mod((uint64_t)val * other.val);       return *this;     }     mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); }     friend mod_int operator+(const mod_int &a, const mod_int &b) {       return mod_int(a) += b;     }     friend mod_int operator-(const mod_int &a, const mod_int &b) {       return mod_int(a) -= b;     }     friend mod_int operator*(const mod_int &a, const mod_int &b) {       return mod_int(a) *= b;     }     friend mod_int operator/(const mod_int &a, const mod_int &b) {       return mod_int(a) /= b;     }     mod_int &operator++() {       val = val == MOD - 1 ? 0 : val + 1;       return *this;     }     mod_int &operator--() {       val = val == 0 ? MOD - 1 : val - 1;       return *this;     }     mod_int operator++(int32_t) {       mod_int before = *this;       ++*this;       return before;     }     mod_int operator--(int32_t) {       mod_int before = *this;       --*this;       return before;     }     mod_int operator-() const { return val == 0 ? 0 : MOD - val; }     bool operator==(const mod_int &other) const { return val == other.val; }     bool operator!=(const mod_int &other) const { return val != other.val; }     mod_int inv() const { return mod_inv(val); }     mod_int pow(long long p) const {       assert(p >= 0);       mod_int a = *this, result = 1;       while (p > 0) {         if (p & 1) result *= a;         a *= a;         p >>= 1;       }       return result;     }     friend ostream &operator<<(ostream &stream, const mod_int &m) {       return stream << m.val;     }     friend istream &operator>>(istream &stream, mod_int &m) {       return stream >> m.val;     }   };   const long long N = 1e5 + 5;   vector<long long> g[N], in[N], out[N];   mod_int p[N];   pair<long long, long long> dp[4 * N][22];   long long te = 0, temp = 0;   long long tin[N], LOG[4 * N], dep[N];   mod_int last = 1;   void dfs(long long u, long long p) {     dep[u] = dep[p] + 1;     te++;     tin[u] = te;     dp[te][0] = {dep[u], u};     for (auto i : g[u]) {       if (i != p) {         dfs(i, u);         te++;         dp[te][0] = {dep[u], u};       }     }   }   long long lca(long long u, long long v) {     long long l = min(tin[u], tin[v]);     long long r = max(tin[u], tin[v]);     long long j = LOG[r - l + 1];     pair<long long, long long> p = min(dp[l][j], dp[r - (1 << j) + 1][j]);     return p.second;   }   long long dis(long long u, long long v) {     return dep[u] + dep[v] - 2 * dep[lca(u, v)];   }   long long ctree[N];   long long sz[N];   long long vis[N];   mod_int dp1[N], dp2[N];   mod_int dp3[N], dp4[N];   void dfs1(long long u, long long par) {     sz[u] = 1;     temp++;     for (auto i : g[u]) {       if (i != par && !vis[i]) {         dfs1(i, u);         sz[u] += sz[i];       }     }   }   long long dfs2(long long u, long long par) {     for (auto i : g[u]) {       if (i != par && sz[i] > temp / 2 && !vis[i]) {         return dfs2(i, u);       }     }     return u;   }   void decompose(long long u, long long par) {     temp = 0;     dfs1(u, u);     long long centroid = dfs2(u, u);     ctree[centroid] = par;     vis[centroid] = 1;     for (auto i : g[centroid]) {       if (vis[i]) continue;       decompose(i, centroid);     }   }   mod_int t = 0;   void insert(long long u) {     t += p[u] * dp1[u];     dp2[u] += p[u];     long long i = u;     while (ctree[i] > 0) {       long long par = ctree[i];       mod_int d = dis(u, par);       t = t + d * p[u] * (dp2[par] - dp4[i]) + p[u] * (dp1[par] - dp3[i]);       dp2[par] += p[u], dp4[i] += p[u];       dp1[par] += p[u] * d, dp3[i] += p[u] * d;       i = ctree[i];     }   }   void remove(long long u) {     long long i = u;     t -= p[u] * dp1[u];     dp2[u] -= p[u];     while (ctree[i] > 0) {       long long par = ctree[i];       mod_int d = dis(u, par);       dp2[par] -= p[u];       dp4[i] -= p[u];       dp1[par] -= p[u] * d;       dp3[i] -= p[u] * d;       t = t - d * p[u] * (dp2[par] - dp4[i]) - p[u] * (dp1[par] - dp3[i]);       i = ctree[i];     }   }   long long solve() {     long long n;     cin >> n;     long long mx = 0;     for (long long i = 1; i < n + 1; i++) {       long long l;       long long r;       cin >> l >> r;       in[l].push_back(i), out[r].push_back(i);       mod_int val = r - l + 1;       p[i] = val.inv();       last *= val;       maxs(mx, r);     }     for (long long i = 0; i < n - 1; i++) {       long long u, v;       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 1);     mod_int ans = 0;     for (long long j = 1; j <= 20; j++) {       for (long long i = 1; i + (1 << (j - 1)) <= te; i++) {         dp[i][j] = min(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);       }     }     decompose(1, 0);     for (long long i = 1; i <= mx; i++) {       for (auto j : in[i]) {         insert(j);       }       ans += t;       for (auto j : out[i]) {         remove(j);       }     }     cout << ans * last <<   n ;     return 0;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     for (long long i = 2; i < 4 * N; i++) {       LOG[i] = 1 + LOG[i / 2];     }     long long t = 1;     while (t--) {       solve();     }     return 0;   }