#include using namespace std; vector adj[1000001]; int indegree[1000001]; int n,a,b; int cnt[1000001]; long long int dp[1000001]; const long long int MOD = 1e9 + 7; int dfs1(int now) { if(cnt[now]!=-1) { return cnt[now]; } cnt[now] = 1; for(auto next : adj[now]) { cnt[now] += dfs1(next); } return cnt[now]; } long long int dfs2(int now) { if(dp[now]!=-1) { return dp[now]; } dp[now] = 0; for(auto next : adj[now]) { dp[now] += (dfs2(next) + cnt[next]); } return dp[now]; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for(int i=0;i> a >> b; adj[a].push_back(b); indegree[b]++; } int root = -1; for(int i=1;i<=n;i++) { if(indegree[i]==0) { root = i; break; } } memset(cnt,-1,sizeof(cnt)); memset(dp,-1,sizeof(dp)); dfs1(root); dfs2(root); long long int res = 0; for(int i=1;i<=n;i++) { //cout << i << ' ' << cnt[i] << ' ' << dp[i] << '\n'; res += dp[i]; res%=MOD; } cout << res << '\n'; return 0; }