#include<bits/stdc++.h>
//#define int long long
#define ll long long
using namespace std;
struct FastIO {
    static const int S = 4e6;
    int wpos;
    char wbuf[S];
    FastIO() : wpos(0) {}
    inline int xchar() {
        static char buf[S];
        static int len = 0, pos = 0;
        if (pos == len)
            pos = 0, len = fread(buf, 1, S, stdin);
        if (pos == len) exit(0);
        return buf[pos++];
    }
    inline int xuint() {
        int c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x;
    }
    inline int xint()
    {
        int s = 1, c = xchar(), x = 0;
        while (c <= 32) c = xchar();
        if (c == '-') s = -1, c = xchar();
        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
        return x * s;
    }
    inline void xstring(char *s)
    {
        int c = xchar();
        while (c <= 32) c = xchar();
        for (; c > 32; c = xchar()) * s++ = c;
        *s = 0;
    }
    inline void wchar(int x)
    {
        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
        wbuf[wpos++] = x;
    }
    inline void wint(int x)
    {
        if (x < 0) wchar('-'), x = -x;
        char s[24];
        int n = 0;
        while (x || !n) s[n++] = '0' + x % 10, x /= 10;
        while (n--) wchar(s[n]);
        wchar('\n');
    }
    inline void wstring(const char *s)
    {
        while (*s) wchar(*s++);
    }
    ~FastIO()
    {
        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
    }
} io;
const int maxn=2000+10;
const int mod=1e9+7;
int a[maxn][maxn];
int num[maxn*maxn];
int id[maxn];
ll p[maxn];
vector<int> vs;
ll vp[maxn*maxn];
int k[maxn*maxn];
int32_t main()
{
    //ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n,m; n=io.xint(); m=io.xint();  p[0]=1;for(int i=1;i<=m;i++) { p[i]=p[i-1]*n; p[i]=p[i]%mod;}
    for(int i=1;i<=m;i++){
         for(int j=1;j<=n;j++){
           a[i][j]=io.xint();
             vs.push_back(a[i][j]);
         }
    }
    sort(vs.begin(),vs.end());
    vs.erase(unique(vs.begin(),vs.end()),vs.end());
    for(int i=0;i<vs.size();i++) vp[i]=1;
    int tot=p[m];
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)  id[j]=lower_bound(vs.begin(),vs.end(),a[i][j])-vs.begin();
        for(int j=1;j<=n;j++)  num[id[j]]++;
        for(int j=1;j<=n;j++)
        {
            if(num[id[j]]==0) continue;
            vp[id[j]]*=(n-num[id[j]]); vp[id[j]]%=mod; k[id[j]]++;
            num[id[j]]=0;
        }
    }
    ll ans=0;
    for(int i=0;i<vs.size();i++)
    {
        vp[i]*=p[m-k[i]]; vp[i]%=mod;
        ans+=(tot-vp[i]+mod)%mod*vs[i];
        ans=ans%mod;
    }
    printf("%lld\n",ans);
}