Problem:
Score : 1600 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1 through N, and the edges are numbered 1 through M.
Edge i is directed from x_i to y_i.
Here, x_i < y_i holds.
Also, there are no multiple edges in G.

Consider selecting a subset of the set of the M edges in G, and removing these edges from G to obtain another graph G'.
There are 2^M different possible graphs as G'.

Alice and Bob play against each other in the following game played on G'.
First, place two pieces on vertices 1 and 2, one on each.
Then, starting from Alice, Alice and Bob alternately perform the following operation:

Select an edge i such that there is a piece placed on vertex x_i, and move the piece to vertex y_i (if there are two pieces on vertex x_i, only move one). The two pieces are allowed to be placed on the same vertex.

The player loses when he/she becomes unable to perform the operation.
We assume that both players play optimally.

Among the 2^M different possible graphs as G', how many lead to Alice's victory?
Find the count modulo 10^9+7.

Constraints

2 ≤ N ≤ 15

1 ≤ M ≤ N(N-1)/2

1 ≤ x_i < y_i ≤ N

All (x_i,\ y_i) are distinct.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the number of G' that lead to Alice's victory, modulo 10^9+7.

Sample Input 1

2 1
1 2

Sample Output 1

1

The figure below shows the two possible graphs as G'.
A graph marked with ○ leads to Alice's victory, and a graph marked with × leads to Bob's victory.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

6

The figure below shows the eight possible graphs as G'.

Sample Input 3

4 2
1 3
2 4

Sample Output 3

2

Sample Input 4

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

Sample Output 4

816

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;

int main() {
    //ifstream cin("f.in");

    int n, m; cin >> n >> m; 
    
    vector<vector<int>> mat(n, vector<int> (n, 0));
    vector<int> b(n, 0);

    for(int i = 0; i < m; ++i) {
        int x, y; cin >> x >> y;
        x -= 1, y -= 1;
        mat[x][y] = 1;
        b[x] |= (1 << y);
    }
    
    auto add = [&] (int &a, int b) {
        a += b;
        while(a >= MOD) {
            a -= MOD;
        }
        while(a < 0) {
            a += MOD;
        }
    };

    vector<int> dp((1 << n), 0);
    vector<int> set_bits((1 << 15), 0);
    
    for(int i = 1; i < (1 << 15); ++i) {
        set_bits[i] = set_bits[i >> 1] + (i & 1);
    }

    dp[0] = 1;

    for(int mask = 0; mask < (1 << n); ++mask) {
        for(int sub = mask; sub > 0; sub = ((sub - 1) & mask)) {
            if((sub & 1) and (sub & 2))
                continue;
            if(dp[mask - sub] == 0)
                continue;

            int ways = 1;
            for(int i = 0; i < n; ++i) {
                if((1 << i) & mask)
                    continue;
                int who = set_bits[(b[i] & sub)];
                ways = 1LL * ways * ((1 << who) - 1) % MOD;
                if(ways == 0) {
                    break;
                }
            }
            
            if(ways == 0) {
                continue;
            }

            for(int i = 0; i < n; ++i) {
                if((1 << i) & sub) {
                    int many = set_bits[b[i] & ((1 << n) - mask)];
                    ways = 1LL * ways * (1 << many) % MOD;
                }
            }

            int prv = dp[mask - sub];
            add(dp[mask], 1LL * prv * ways % MOD);
        }
    }

    int ans = dp[(1 << n) - 1];

    cout << ans << "\n";
}