Wojtek has just won a maths competition in Byteland! The prize is admirable — a great book called 'Card Tricks for Everyone.' 'Great!' he thought, 'I can finally use this old, dusted deck of cards that's always been lying unused on my desk!'

The first chapter of the book is 'How to Shuffle k Cards in Any Order You Want.' It's basically a list of n intricate methods of shuffling the deck of k cards in a deterministic way. Specifically, the i-th recipe can be described as a permutation (P_{i,1}, P_{i,2}, ..., P_{i,k}) of integers from 1 to k. If we enumerate the cards in the deck from 1 to k from top to bottom, then P_{i,j} indicates the number of the j-th card from the top of the deck after the shuffle.

The day is short and Wojtek wants to learn only some of the tricks today. He will pick two integers l, r (1 ≤ l ≤ r ≤ n), and he will memorize each trick from the l-th to the r-th, inclusive. He will then take a sorted deck of k cards and repeatedly apply random memorized tricks until he gets bored. He still likes maths, so he started wondering: how many different decks can he have after he stops shuffling it?

Wojtek still didn't choose the integers l and r, but he is still curious. Therefore, he defined f(l, r) as the number of different decks he can get if he memorizes all the tricks between the l-th and the r-th, inclusive. What is the value of

$$$∑_{l=1}^n ∑_{r=l}^n f(l, r)?$$$

Input

The first line contains two integers n, k (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 5) — the number of tricks and the number of cards in Wojtek's deck.

Each of the following n lines describes a single trick and is described by k distinct integers P_{i,1}, P_{i,2}, ..., P_{i, k} (1 ≤ P_{i, j} ≤ k).

Output

Output the value of the sum described in the statement.

Examples

Input


3 3
2 1 3
3 1 2
1 3 2


Output


25


Input


2 4
4 1 3 2
4 3 1 2


Output


31

Note

Consider the first sample:

  * The first trick swaps two top cards. 
  * The second trick takes a card from the bottom and puts it on the top of the deck. 
  * The third trick swaps two bottom cards. 



The first or the third trick allow Wojtek to generate only two distinct decks (either the two cards are swapped or not). Therefore, f(1, 1) = f(3, 3) = 2.

The second trick allows him to shuffle the deck in a cyclic order. Therefore, f(2,2)=3.

It turns that two first tricks or two last tricks are enough to shuffle the deck in any way desired by Wojtek. Therefore, f(1,2) = f(2,3) = f(1,3) = 3! = 6.
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
vector<int> apply(vector<int> const &a, vector<int> const &b) {
  int n = (int)a.size();
  vector<int> res(n);
  for (int i = 0; i < n; i++) {
    res[i] = a[b[i]];
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int m, n;
  cin >> m >> n;
  map<vector<int>, int> ms;
  vector<vector<int>> ps;
  {
    vector<int> p(n);
    for (int i = 0; i < n; i++) p[i] = i;
    int c = 0;
    do {
      ps.push_back(p);
      ms[p] = c;
      c++;
    } while (next_permutation(p.begin(), p.end()));
  }
  int all = 1;
  for (int i = 2; i <= n; i++) all *= i;
  vector<vector<int>> d(all, vector<int>(all, -1));
  for (int i = 0; i < all; i++) {
    vector<int> p = ps[i];
    for (int j = 0; j < all; j++) {
      vector<int> q = ps[j];
      vector<int> r = apply(p, q);
      d[i][j] = ms[r];
    }
  }
  vector<int> a(m);
  for (int i = 0; i < m; i++) {
    vector<int> p(n);
    for (int j = 0; j < n; j++) {
      cin >> p[j];
      p[j]--;
    }
    a[i] = ms[p];
  }
  map<vector<int>, int> qh;
  vector<vector<int>> edges;
  vector<vector<int>> kl;
  {
    auto dor = [](vector<int> const &a, vector<int> const &b) {
      vector<int> ret(a.size());
      for (int i = 0; i < (int)a.size(); i++) {
        ret[i] = a[i] | b[i];
      }
      return ret;
    };
    auto make = [&](vector<int> a) {
      while (true) {
        bool changed = false;
        for (int i = 0; i < (int)a.size(); i++) {
          if (!a[i]) continue;
          for (int j = 0; j < (int)a.size(); j++) {
            if (!a[j]) continue;
            if (!a[d[i][j]]) {
              a[d[i][j]] = true;
              changed = true;
            }
          }
        }
        if (!changed) break;
      }
      return a;
    };
    auto add = [&](vector<int> const &a) {
      int sz = (int)qh.size();
      qh[a] = sz;
      kl.push_back(a);
      edges.push_back(vector<int>(all));
    };
    vector<int> start(all);
    start[0] = 1;
    add(start);
    for (int j = 0; j < (int)kl.size(); j++) {
      for (int i = 0; i < all; i++) {
        vector<int> cur = kl[j];
        cur[i] = 1;
        cur = make(cur);
        if (qh.find(cur) == qh.end()) {
          add(cur);
        }
        edges[j][i] = qh[cur];
      }
    }
  }
  vector<int> first(all, m);
  vector<int> z(all);
  for (int i = 0; i < all; i++) z[i] = i;
  long long ans = 0;
  for (int i = m - 1; i >= 0; i--) {
    first[a[i]] = i;
    vector<int> have(all);
    have[0] = 1;
    sort(z.begin(), z.end(), [&](int v, int u) { return first[v] < first[u]; });
    int curPos = i;
    for (int it = 0; it < all; it++) {
      int pr = z[it];
      if (have[pr]) continue;
      int pos = first[pr];
      int count = 0;
      for (int i : have) count += i;
      ans += (long long)(pos - curPos) * count;
      curPos = pos;
      if (pos == m) break;
      have = kl[edges[qh[have]][pr]];
    }
    int count = 0;
    for (int i : have) count += i;
    ans += (m - curPos) * count;
  }
  cout << ans << endl;
}
