#include <bits/stdc++.h>
const int N = 1000005;
const int mo = 1000000007;
using namespace std;
int n, m, a[N];
bool bz[N], bp[N];
vector<int> f[N], nf[N];
template <class I>
void read(I &x) {
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
long long ans, cnt, ap[20], sum;
long long d[20][2], js[20], mx, cd[N];
int num;
long long ksm(long long k, long long n) {
  long long s = 1;
  for (; n; n >>= 1, k = k * k % mo)
    if (n & 1) s = s * k % mo;
  return s;
}
void dfs(int k) {
  if (k > ap[0]) {
    long long v = 1, c = 0, mp = 0;
    for (int i = 1; i <= num; ++i) mp = max(mp, d[i][0]);
    if (max(mp, mx) > 2) {
      long long sv = ans;
      for (int i = 1; i <= num; ++i) {
        v = v * js[d[i][1] - 1] % mo;
        sv = sv * nf[d[i][0]][cd[d[i][0]]] % mo;
        cd[d[i][0]]++;
        sv = sv * (long long)f[d[i][0]][cd[d[i][0]]] % mo;
      }
      sum = (sum + sv * v) % mo;
      for (int i = 1; i <= num; ++i) cd[d[i][0]]--;
    } else {
      for (int i = 1; i <= num; ++i) v = v * js[d[i][1] - 1] % mo;
      sum = (sum + v) % mo;
    }
    return;
  }
  for (int i = 1; i <= num; ++i) {
    d[i][0] += ap[k];
    d[i][1]++;
    dfs(k + 1);
    d[i][1]--;
    d[i][0] -= ap[k];
  }
  d[++num][0] = ap[k];
  d[num][1] = 1;
  dfs(k + 1);
  d[num][1] = d[num][0] = 0;
  num--;
}
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), bz[a[i]] = 1;
  mx = 0;
  for (int i = 1; i <= n; ++i) {
    f[i].resize(n / i + 1);
    nf[i].resize(n / i + 1);
    long long v = i;
    f[i][0] = 1, f[i][1] = i;
    for (int j = 2; i * j <= n; j++)
      f[i][j] = (long long)i *
                ((long long)(j - 1) * f[i][j - 2] % mo + f[i][j - 1]) % mo,
      v = v * (long long)f[i][j] % mo;
    v = ksm(v, mo - 2);
    nf[i][0] = nf[i][1] = v;
    v = v * i % mo;
    for (int j = 2; i * j <= n; j++) nf[i][j] = v, v = v * f[i][j] % mo;
    v = 1;
    for (int j = n / i; j >= 0; --j)
      nf[i][j] = (long long)nf[i][j] * v % mo, v = v * f[i][j] % mo;
  }
  ans = 1, cnt = 0;
  js[0] = 1;
  for (int i = 1; i <= 19; ++i) js[i] = js[i - 1] * i % mo;
  sum = 0;
  f[0].push_back(0);
  for (int i = 1; i <= n; ++i) {
    if (!bz[i]) {
      bp[i] = 1;
      int c = 1, p = a[i];
      while (p != i && p != 0) bp[p] = 1, c++, p = a[p];
      ap[++ap[0]] = c;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (!bp[i]) {
      bp[i] = 1;
      int c = 1, p = a[i];
      while (p != i && p != 0) bp[p] = 1, c++, p = a[p];
      mx = max(mx, (long long)c);
      cd[c]++;
    }
  }
  for (int i = 1; i <= n; ++i) {
    ans = ans * f[i][cd[i]] % mo;
  }
  num = 0;
  dfs(1);
  printf("%I64d\n", sum);
}
