#include <bits/stdc++.h>
using namespace std;
const int f[6] = {1, 1, 2, 6, 24, 120};
int g[120][120], k;
int cexp(vector<int> a) {
  int res = 0;
  for (int i = 0; i != k - 1; ++i)
    for (int j = i + 1; j != k; ++j)
      if (a[i] > a[j]) res += f[k - 1 - i];
  return res;
}
vector<int> icexp(int x) {
  vector<int> res(k);
  vector<int> v(k);
  iota(v.begin(), v.end(), 0);
  for (int i = 0; i != k; ++i) {
    int t = f[k - i - 1];
    res[i] = v[x / t];
    v.erase(v.begin() + x / t);
    x %= t;
  }
  return res;
}
vector<int> operator*(const vector<int>& p1, const vector<int>& p2) {
  vector<int> r(p1.size());
  for (int i = 0; i != k; ++i) r[i] = p1[p2[i]];
  return r;
}
unordered_map<bitset<120>, int> ans;
void gen() {
  for (int i = 0; i != f[k]; ++i)
    for (int j = 0; j != f[k]; ++j) g[i][j] = cexp(icexp(i) * icexp(j));
}
int q[120], qh, qt;
bitset<120> add(bitset<120> b, int t) {
  int c = b.count();
  if (b[t] || c == f[k]) return b;
  qh = qt = 0;
  b[t] = 1;
  for (int i = 0; i != f[k]; ++i)
    if (b[i]) q[qt++] = i;
  while (qh != qt) {
    int u = q[qh++];
    for (int i = 0; i != qh; ++i) {
      int v = g[u][q[i]];
      if (!b[v]) {
        b[v] = 1;
        ++c;
        q[qt++] = v;
        if (c > f[k] / 2) {
          for (int i = 0; i != f[k]; ++i) b[i] = 1;
          break;
        }
      }
    }
  }
  return b;
}
bitset<120> e() {
  bitset<120> b;
  b[0] = 1;
  return b;
}
unordered_map<bitset<120>, int> w[200001];
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n >> k;
  gen();
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    vector<int> a(k);
    for (int j = 0; j != k; ++j) cin >> a[j], a[j]--;
    int id = cexp(a);
    for (auto p : w[i - 1]) w[i][add(p.first, id)] += p.second;
    w[i][add(e(), id)]++;
    for (auto p : w[i]) {
      ans += p.second * p.first.count();
    }
  }
  cout << ans << endl;
  return 0;
}
