1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//泥の分際で私だけの大切を奪おうだなん
#include<bits/stdc++.h>
using namespace std;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
const int p=1e9+7;
int qp(int x,int y)
{
	int res=1;
	for(int t=x; y; y>>=1,t=1ll*t*t%p)
		if(y&1) res=1ll*res*t%p;
	return res;
}
int a[3003][3003];
bool f[3003][3003];
signed main()
{
	int n=read(),m=read();
	for(int i=1; i<=m; ++i)
		for(int j=1; j<=n; ++j)
			a[i][j]=read();
	queue<pair<int,int>> q;
	int ans=0;
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=n; ++j) if(i!=j)
			if(!f[i][j])
			{
				f[i][j]=1;q.push({i,j});
				int A=0,B=0;
				while(!q.empty())
				{
					auto [x,y]=q.front();q.pop();
					if(x>y) ++A;
					++B;
					for(int k=1; k<=m; ++k)
						if(!f[a[k][x]][a[k][y]])
							f[a[k][x]][a[k][y]]=1,
							q.push({a[k][x],a[k][y]});
				}
				ans=(ans+1ll*A*(B-A)%p*qp(B,p-2))%p;
			}
	printf("%d\n",ans);
	return 0;
}