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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <set>

using namespace std;

long long MOD = 1000000007;
long long zlicz = 0;
int n;

vector<vector<short int>> ulubione;
set<pair<int,int>> secik;

int hasz(vector<short int> &tab,int pot)
{
    long long r=0;

    long long m = 1;

    for(long long x : tab)
    {
        r = (r +(x*m)%MOD)%MOD;
        m = (m*pot)%MOD;
    }

    return r;
}

long long merge(vector<short int>& tabela, int l, int p, int s)
{
    vector<int> tab2;
 
    int i = l, j = s+1;
    long long inv = 0;
 
    while (i <= s && j <= p)
    {
        if (tabela[i] <= tabela[j])
        {
            tab2.push_back(tabela[i]);
            i++;
        }
        else
        {
            tab2.push_back(tabela[j]);
            inv += s - i + 1;
            j++;
        }
    }
 
    while(i <= s)
    {
        tab2.push_back(tabela[i]);
        i++;
    }
 
    while (j <= p)
    {
        tab2.push_back(tabela[j]);
        j++;
    }
 
 
    for (int i = l; i <= p; i++)
        tabela[i] = tab2[i - l];
    return inv;
}
 
long long merge_sort(vector<short int>& tabela,int l,int p)
{
    if (l == p)
        return 0;
 
    int s = (l + p) / 2;
    long long inv = 0;
 
    inv += merge_sort(tabela, l, s);
    inv += merge_sort(tabela, s+1, p);
    inv += merge(tabela, l, p, s);
    return inv;
}

vector<short int> zloz(vector<short int> &a,vector<short int> &b)
{
    vector<short int> r;

    for(int i = 0;i<n;i++)
        r.push_back(a[b[i]-1]);

    return r;
}

void zrob(vector<short int> &tab)
{
    vector<short int> kopia = tab;

    zlicz = (zlicz + merge_sort(kopia,0,n-1))%MOD;

    kopia.clear();

    for(vector<short int> &x : ulubione)
    {
        vector<short int> nowe = zloz(tab,x);

        int h1 = hasz(nowe,3001),h2 = hasz(nowe,3011);

        if(secik.find({h1,h2}) == secik.end())
        {
            secik.insert({h1,h2});
            zrob(nowe);
        }
    }
}

long long potegowanie(long long liczba, long long pot1)
{
    if (pot1 == 0)
        return 1;

    long long pot2 = potegowanie(liczba, pot1 / 2);

    if (pot1 % 2 == 0)
        return (pot2 * pot2) % MOD;
        
    return (((liczba * pot2) % MOD) * pot2) % MOD;
}

int main()
{
    int k;
    cin >> n >> k;

    for(int i = 0;i<k;i++)
    {
        vector<short int> tab;

        for(int i = 0;i<n;i++)
        {
            int x;
            cin >> x;
            tab.push_back(x);
        }

        ulubione.push_back(tab);
    }

    for(vector<short int> &tab : ulubione)
        if(secik.find({hasz(tab,3001),hasz(tab,3011)}) == secik.end())
        {
            secik.insert({hasz(tab,3001),hasz(tab,3011)});
            zrob(tab);
        }
    
    long long dzielnik = secik.size();

    cout << (potegowanie(dzielnik,MOD-2)*zlicz)%MOD;
}