Problem:
Problem A: Alien's Counting

Natsuki and her friends were taken to the space by an alien and made friends with a lot of aliens. During the space travel, she discovered that aliens’ hands were often very different from humans’. Generally speaking, in a kind of aliens, there are N fingers and M bend rules on a hand. Each bend rule describes that a finger A always bends when a finger B bends. However, this rule does not always imply that the finger B bends when the finger A bends.

When she were counting numbers with the fingers, she was anxious how many numbers her alien friends can count with the fingers. However, because some friends had too complicated rule sets, she could not calculate those. Would you write a program for her?

Input

N M

S1 D1

S2 D2

.

.

.

SM DM

The first line contains two integers N and M (1 ≤ N ≤ 1000, 0 ≤ M ≤ 1000) in this order. The following M lines mean bend rules. Each line contains two integers Si and Di in this order, which mean that the finger Di always bends when the finger Si bends. Any finger appears at most once in S.

Output

Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo 1000000007 in a line.

Sample Input 1

5 4
2 3
3 4
4 3
5 4

Output for the Sample Input 1

10

Sample Input 2

5 5
1 2
2 3
3 4
4 5
5 1

Output for the Sample Input 2

2

Sample Input 3

5 0

Output for the Sample Input 3

32

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

//Alien's Counting
public class Main{

	long MOD = 1000000007, res;
	int n, m, ID;
	List<Integer>[] e, rev, vs;
	List<Integer> visit;
	boolean[] u;
	int[] scc, out;
	
	void dfs(int k){
		if(u[k])return;
		u[k] = true;
		for(int x:e[k])dfs(x);
//		System.out.println("ORDER:"+k);
		visit.add(k);
	}
	void rdfs(int k){
		if(u[k])return;
		u[k] = true;
		scc[k] = ID;
		vs[ID].add(k);
		for(int x:rev[k])rdfs(x);
	}
	long modpow(long x, long n, long MOD){
		long res = 1, v = x;
		while(n>0){
			if((n&1)>0)res = res*v%MOD;
			v = v*v%MOD;
			n>>=1;
		}
		return res;
	}
	
	long f(int k){
		u[k] = true;
		long r = 2;
		List<Integer> l = new ArrayList<Integer>();
		for(int x:vs[k])for(int y:rev[x]){
			if(u[scc[y]])continue;
			u[scc[y]] = true;
			l.add(scc[y]);
		}
		while(!l.isEmpty()){
			r+=((modpow(2, l.size(), MOD)-1+MOD)%MOD);
			r%=MOD;
			List<Integer> next = new ArrayList<Integer>();
			for(int v:l){
				for(int x:vs[v])for(int y:rev[x]){
					if(u[scc[y]])continue;
					u[scc[y]] = true;
					next.add(scc[y]);
				}
			}
			l = next;
		}
//		System.out.println("K:"+k+" r:"+r);
		return r;
	}
	
	@SuppressWarnings("unchecked")
	void run(){
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt(); m = sc.nextInt();
		e = new List[n]; rev = new List[n]; vs = new List[n];
		for(int i=0;i<n;i++){
			e[i] = new ArrayList<Integer>(); rev[i] = new ArrayList<Integer>(); vs[i] = new ArrayList<Integer>();
		}
		while(m--!=0){
			int s =  sc.nextInt()-1, d = sc.nextInt()-1;
			e[s].add(d); rev[d].add(s);
		}
		visit = new ArrayList<Integer>();
		u = new boolean[n];
		for(int i=0;i<n;i++)dfs(i);
		scc = new int[n];
		ID = 0;
		Arrays.fill(u, false);
		for(int i=n-1;i>=0;i--){
			int x = visit.get(i);
//			System.out.println("X:"+x);
			if(u[x])continue;
			rdfs(x);
			ID++;
		}
//		System.out.println(ID);
		res = 1;
		out = new int[ID];
		for(int i=0;i<ID;i++)for(int x:vs[i])for(int y:e[x])if(scc[x]!=scc[y])out[i]++;
//		debug(scc); debug(out);
		Arrays.fill(u, false);
		for(int i=0;i<ID;i++)if(out[i]==0)res=res*f(i)%MOD;
		System.out.println(res);
	}
	
	void debug(Object...o){
		System.out.println(Arrays.deepToString(o));
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
}