Problem:
Score : 1200 points

Problem Statement

You are given a simple connected undirected graph G consisting of N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.

Edge i connects Vertex a_i and b_i bidirectionally.
It is guaranteed that the subgraph consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a spanning tree of G.

An allocation of weights to the edges is called a good allocation when the tree consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a minimum spanning tree of G.

There are M! ways to allocate the edges distinct integer weights between 1 and M.
For each good allocation among those, find the total weight of the edges in the minimum spanning tree, and print the sum of those total weights modulo 10^{9}+7.

Constraints

All values in input are integers.

2 \leq N \leq 20

N-1 \leq M \leq N(N-1)/2

1 \leq a_i, b_i \leq N

G does not have self-loops or multiple edges.

The subgraph consisting of Vertex 1,2,\ldots,N and Edge 1,2,\ldots,N-1 is a spanning tree of G.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
\vdots
a_M b_M

Output

Print the answer.

Sample Input 1

3 3
1 2
2 3
1 3

Sample Output 1

6

An allocation is good only if Edge 3 has the weight 3. For these good allocations, the total weight of the edges in the minimum spanning tree is 3, and there are two good allocations, so the answer is 6.

Sample Input 2

4 4
1 2
3 2
3 4
1 3

Sample Output 2

50

Sample Input 3

15 28
10 7
5 9
2 13
2 14
6 1
5 12
2 10
3 9
10 15
11 12
12 6
2 12
12 8
4 10
15 3
13 14
1 15
15 12
4 14
1 7
5 11
7 13
9 10
2 7
1 9
5 6
12 14
5 2

Sample Output 3

657573092

Print the sum of those total weights modulo 10^{9}+7.

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#define nn 20
#define ll long long
#define mod 1000000007
#define pii pair<int,int>
#define mp make_pair

int n,m;
pii link[nn*nn];

int cnt[1<<nn];int dep[nn];int topa[nn],pa[nn];
vector<int> v[nn],c[nn];
void dfs(int x,int p)
{
	dep[x]=dep[p]+1;pa[x]=p;
	for(int i=0;i<v[x].size();i++)
	{
		int y=v[x][i];
		if(y!=p)topa[y]=c[x][i],dfs(y,x);
	}
}


int solve(int x,int y)
{
	int mask=0;
	while(x!=y)
	{
		if(dep[x]<dep[y]) swap(x,y);
		mask|=1<<topa[x],x=pa[x];
	}
	return mask;
}


ll dp_tms[2][1<<nn],dp_val[2][1<<nn];
void add(ll &x,ll y) {x=(x+y)%mod;}
int noway[1<<nn];
int main()
{
	
	
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) 
	{
		int x,y;scanf("%d%d",&x,&y);x--,y--;
		link[i]=mp(x,y);
		if(i<n) v[x].push_back(y),v[y].push_back(x),c[x].push_back(i-1),c[y].push_back(i-1);
	} 
	
	dfs(0,-1);
	
	for(int i=n;i<=m;i++) cnt[solve(link[i].first,link[i].second)]++;
	
	for(int i=1;i<(1<<nn);i<<=1)
		for(int j=0;j<(1<<nn);j+=i+i)
			for(int k=j;k<j+i;k++)
				cnt[k+i]+=cnt[k];
	
	int now=1;
	dp_val[now][0]=0;dp_tms[now][0]=1;
	
	int least=0;
	for(int i=1;i<=m;i++)
	{
		now^=1;
		for(int j=least;j<(1<<n-1);j++) dp_val[now][j]=dp_tms[now][j]=0;
		
		for(int j=least;j<(1<<n-1);j++)
		if(cnt[j]-i+__builtin_popcount(j)+1<0)noway[j]=1;
		else if(dp_tms[now^1][j])
		{
			int can=cnt[j],alr=__builtin_popcount(j);
			can-=(i-alr-1);
			if(can<0) noway[j]=1;
			if(can<0) continue;
			
			if(can) add(dp_tms[now][j],dp_tms[now^1][j]*can),
					add(dp_val[now][j],dp_val[now^1][j]*can);
			
			int s=((1<<n-1)-1)^j;
			while(s)
			{
				int k=s&-s;s-=k;
				dp_tms[now][j|k]+=dp_tms[now^1][j];
				if(dp_tms[now][j|k]>=mod) dp_tms[now][j|k]-=mod;
				dp_val[now][j|k]+=dp_val[now^1][j];
				add(dp_val[now][j|k],dp_tms[now^1][j]*i);
			}
		}
		
		while(least<(1<<nn) and noway[least]) least++; 
	}
	printf("%lld\n",dp_val[now][(1<<n-1)-1]);
	return 0;
}