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;
int const INF = (int)1e9 + 1e3;
long long const INFL = (long long)1e18 + 1e6;
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
uniform_int_distribution<long long> ll_distr;
long long rnd(long long a, long long b) {
  return ll_distr(tw) % (b - a + 1) + a;
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> order(k);
  iota(order.begin(), order.end(), 0);
  vector<vector<int>> perms;
  map<vector<int>, int> perm_ind;
  do {
    perms.push_back(order);
    perm_ind[order] = ((int)perms.size()) - 1;
  } while (next_permutation(order.begin(), order.end()));
  vector<vector<int>> go(((int)perms.size()), vector<int>(((int)perms.size())));
  vector<int> res(k);
  for (int i = 0; i < ((int)perms.size()); ++i) {
    for (int j = 0; j < ((int)perms.size()); ++j) {
      for (int q = 0; q < k; ++q) {
        res[perms[j][q]] = perms[i][q];
      }
      go[i][j] = perm_ind[res];
    }
  }
  vector<int> arr;
  for (int i = 0; i < n; ++i) {
    vector<int> cur;
    for (int j = 0; j < k; ++j) {
      int num;
      cin >> num;
      cur.push_back(num - 1);
    }
    arr.push_back(perm_ind[cur]);
  }
  vector<bool> used(((int)perms.size()));
  vector<int> last(((int)perms.size()), n);
  vector<int> order_perms(((int)perms.size()));
  iota(order_perms.begin(), order_perms.end(), 0);
  long long ans = 0;
  vector<int> added;
  vector<int> was;
  vector<int> tmp;
  vector<int> add_perms;
  for (int i = n - 1; i >= 0; --i) {
    last[arr[i]] = i;
    fill(used.begin(), used.end(), 0);
    sort(order_perms.begin(), order_perms.end(),
         [&](int a, int b) { return last[a] < last[b]; });
    int from = i;
    int cnt = 1;
    used[0] = true;
    was = {0};
    add_perms.clear();
    for (int p : order_perms) {
      if (last[p] == n) {
        break;
      }
      if (used[go[0][p]]) {
        continue;
      }
      add_perms.push_back(p);
      ans += (last[p] - from) * cnt;
      while (true) {
        bool changed = false;
        for (int i = 0; i < ((int)perms.size()); ++i) {
          if (!used[i]) {
            continue;
          }
          for (int q : add_perms) {
            if (!used[go[i][q]]) {
              used[go[i][q]] = true;
              ++cnt;
              changed = true;
            }
          }
        }
        if (!changed) {
          break;
        }
      }
      from = last[p];
    }
    ans += (n - from) * cnt;
  }
  cout << ans << "\n";
}
int main() {
  cout << setprecision(15) << fixed;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int test_count = 1;
  for (int test = 1; test <= test_count; ++test) {
    solve();
  }
}
