#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100, K = 5, M = 120;
int n, k, last[M], a[N], par[M], go[M][M];
long long ans;
vector<vector<int>> all;
vector<int> vec;
int g_par(int v) { return (v == par[v] ? v : par[v] = g_par(par[v])); }
bool merge(int u, int v) {
  bool b = g_par(u) != g_par(v);
  par[par[u]] = par[v];
  return b;
}
void pre_pro() {
  vector<int> vec;
  for (int i = 0; i < k; i++) vec.push_back(i);
  do {
    all.push_back(vec);
  } while (next_permutation(vec.begin(), vec.end()));
}
int read() {
  vector<int> vec;
  for (int i = 0; i < k; i++) {
    int x;
    cin >> x;
    vec.push_back(--x);
  }
  return lower_bound(all.begin(), all.end(), vec) - all.begin();
}
int f(int a, int b) {
  vector<int> vec;
  for (int i = 0; i < k; i++) vec.push_back(all[a][all[b][i]]);
  return lower_bound(all.begin(), all.end(), vec) - all.begin();
}
void input_ready() {
  cin >> n >> k;
  pre_pro();
  for (int i = 0; i < n; i++) a[i] = read();
  for (int i = 0; i < all.size(); i++)
    for (int j = 0; j < all.size(); j++) go[i][j] = f(i, j);
}
void act(int dex) {
  if (last[a[dex]] == -1)
    vec.push_back(last[a[dex]] = dex);
  else {
    vector<int> help;
    for (int x : vec)
      if (a[x] != a[dex]) help.push_back(x);
    help.push_back(last[a[dex]] = dex);
    vec = help;
  }
}
int cnt;
void run(int x) {
  for (int i = 0; i < all.size(); i++) cnt -= merge(i, go[i][x]);
}
void calc(int dex) {
  int last = dex;
  cnt = all.size();
  iota(par, par + M, 0);
  for (int i = vec.size() - 1; ~i; i--) {
    ans += (last - vec[i]) * (all.size() / cnt);
    last = vec[i];
    if (g_par(0) != g_par(a[vec[i]])) run(a[vec[i]]);
  }
  ans += (last + 1) * (all.size() / cnt);
}
void solve() {
  fill(last, last + M, -1);
  for (int i = 0; i < n; i++) {
    act(i);
    calc(i);
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  input_ready();
  solve();
  cout << ans;
  return 0;
}
