N = int(input())
P = [[int(i) for i in input().split()] for  _ in range(N)]

import numpy as np
from collections import deque

Pmat = np.matrix(P)


badmat = Pmat@Pmat.T
badmat = np.where(badmat ==0)


graph = [deque([]) for _ in range(N + 1)]
for i in range(N):
    graph[i].extend(badmat[1][badmat[0]==i])


arrive_time = [0] * N # 到着時刻
visit = [-1]*(N)

# 深さ優先探索
def dfs(v):
    s = 1
    stack = [v]
    arrive_time[v] = 1
    while stack:
        v = stack[-1]
        if graph[v]:
            w = graph[v].popleft()
            if visit[w] == -1:
                visit[w] = 1
                stack.append(w)
        else:
            stack.pop()
    return s

ans=[]
# 孤立している頂点対策
for i in range(N):
    if visit[i] == -1:
        ans.append(dfs(i))

def power_func(a,n,p):
    bi = str(format(n,"b"))#2進表現に
    res = 1
    for i in range(len(bi)):
        res = (res*res) %p
        if bi[i] == "1":
            res = (res*a) %p
    return res


kotae = 0
for a in ans:
    kotae+=power_func(2,a,1000000007)
    kotae = kotae % 1000000007
print(kotae-1)
