#define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include using namespace std; const int MOD=1e9+7; vector D(1000010); vector> C(1000010); void rec(ll u, ll p){ D[u]=p; if(!C[u].empty()){ for(auto x:C[u]) rec(x,p+1); } } int main() { int n; cin>>n; vector P(1000010); int a,b; rep(i,n){ cin>>a>>b; P[b]=a; C[a].push_back(b); } ll root; for(ll i=1;i<=n;i++){ if(P[i]==0){ root=i; break; } } rec(root,0); ll ans=0; for(ll i=1;i<=n;i++){ ans=(ans+(D[i]*(D[i]+1))%MOD/2)%MOD; } cout<