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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <vector>
#include <set>
#include <numeric>
#include <unordered_set>
constexpr long long MOD=1000000007;
struct vec_hash{
    long long operator()(const std::vector<char>& A)const{
        long long res=0, P=1;
        for (auto a:A){
            res+=a*P;
            P*=2137;
        }
        return res;
    }
};

std::vector<char> combine(const std::vector<char>& A, const std::vector<char>& B){
    std::vector<char> res(A.size());
    for (size_t i=0; i<A.size(); ++i)res[i]=A[B[i]];
    return res;
}

int inv(const std::vector<char>& A){
    int res=0;
    for (size_t i=0; i<A.size(); ++i){
        for (size_t j=i+1; j<A.size(); ++j){
            if (A[i]>A[j])++res;
        }
    }
    return res;
}

void print(std::vector<char> A){
    for (auto a:A)std::cout<<a<<' ';
    std::cout<<'\n';
}

long long fast_pow(long long a, long long b){
    long long res=1;
    while (b!=0){
        if (b%2==1){
            res*=a;
            res%=MOD;
        }
        a*=a;
        a%=MOD;
        b/=2;
    }
    return res;
}

void solve1(int n, int k){
    std::unordered_set<std::vector<char>,vec_hash > s, s2, base, all;
    {
        std::vector<char> tmp;
        for (size_t i=0; i<n; ++i)tmp.push_back(i);
        s.insert(tmp);
    }
    for (size_t x=0; x<k; ++x){
        std::vector<char> tmp;
        for (size_t i=0; i<n; ++i){
            short int a;
            std::cin>>a;
            tmp.push_back(a-1);
        }
        if (all.find(tmp)==all.end()){
            s.insert(tmp);
            base.insert(tmp);
            all.insert(tmp);
            while (!s.empty()){
                for (const auto &a:s){
                    for (const auto& b:base){
                        auto tmp2=combine(a,b);
                        if (all.find(tmp2)==all.end()){
                            all.insert(tmp2);
                            s2.insert(tmp2);
                        }
                   }
                }
                s.clear();
                std::swap(s,s2);
                //std::cout<<all.size()<<std::endl;
            }
        }
    }

    long long res=0;
    for (const auto& a:all)res+=inv(a);
    //std::cout<<res<<'\n';
    std::cout<<(res*fast_pow(all.size(),MOD-2))%MOD;
}

void solve2(int n, int k){
    long long all=1;
    long long res=0;
    int t[n];
    for (size_t i=0; i<n; ++i){
        std::cin>>t[i];
        --t[i];
    }
    int cycle_len[n];
    std::vector<int> cycle[n];
    for (size_t i=0; i<n; ++i){
        int v=i;
        long long cnt=1;
        cycle[i].push_back(v);
        while (t[v]!=i){
            v=t[v];
            cycle[i].push_back(v);
            ++cnt;
        }
        all=std::lcm(all,cnt);
        all%=MOD;
        cycle_len[i]=cnt;
    }
    for (int i=0; i<n; ++i){
        for (int j=0; j<n; ++j){
            if (t[i]>=t[j])continue;
            int tmp=std::lcm(cycle[i].size(),cycle[j].size());
            long long val=(all*fast_pow(tmp,MOD-2))%MOD;
            long long sum=0;
            for (int k=0; k<tmp; ++k){
                if (cycle[i][k%cycle[i].size()]>cycle[j][k%cycle[j].size()])++sum;
            }
            res+=sum*val;
        }
    }
    all%=MOD;
    res%=MOD;
    std::cout<<(res*fast_pow(all,MOD-2))%MOD<<'\n';
}
int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    int n, k;
    std::cin>>n>>k;
    if (k==1){
        solve2(n,k);
        return 0;
    }
    solve1(n,k);
}