#include <bits/stdc++.h>
#define N 500020
#define mod 1000000007
#define ll long long
using namespace std;
inline int read(){
int x=0,f=0;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return f?-x:x;
}
struct node{
int to, nxt;
}e[N<<1];
struct edge{
int val, id;
}a[N];
int head[N], cnt;
ll sz[N], vis[N], ans, val[N];
void ins(int x, int y){
e[++cnt] = (node){y, head[x]}; head[x] = cnt;
e[++cnt] = (node){x, head[y]}; head[y] = cnt;
}
bool cmp1(edge a, edge b){
return a.val < b.val;
}
bool cmp2(edge a, edge b){
return a.val > b.val;
}
void dfs(int x, int v){
sz[x] = 0; ll sum = 0;
for(int i = head[x]; i; i = e[i].nxt){
if(!vis[e[i].to]) continue;
int y = e[i].to;
sz[x] += sz[y];
sum = (sum+sz[y]*sz[y])%mod;
}
ans = (ans+(sz[x]*2+sz[x]*sz[x]-sum)%mod*v)%mod;
sz[x]++; vis[x] = 1;
}
int main(){
int n = read();
for(int i = 1; i <= n; i++) a[a[i].id = i].val = read();
for(int i = 1; i < n; i++) ins(read(), read());
sort(a+1, a+n+1, cmp1);
for(int i = 1; i <= n; i++)
dfs(a[i].id, a[i].val);
ll ans1 = ans; ans = 0;
memset(vis, 0, sizeof vis);
sort(a+1, a+n+1, cmp2);
for(int i = 1; i <= n; i++)
dfs(a[i].id, a[i].val);
printf("%d\n", ((ans1-ans)%mod+mod)%mod);
}