#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chkmin(T& a, T b) {
  return a > b ? a = b, true : false;
}
template <class T>
bool chkmax(T& a, T b) {
  return a < b ? a = b, true : false;
}
template <class T>
void read(T& a) {
  char c = getchar_unlocked();
  T f = 1;
  a = 0;
  for (; !isdigit(c); c = getchar_unlocked())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar_unlocked()) a = a * 10 + c - '0';
  a *= f;
}
const int maxN = 1e5 + 5, mo = 1e9 + 7;
int pow_mod(int x, int t) {
  int ret = 1;
  while (t) {
    if (t & 1) ret = (long long)ret * x % mo;
    x = (long long)x * x % mo;
    t >>= 1;
  }
  return ret;
}
int a[maxN], N, M, tp, ans;
int cnt[4];
int pre;
int len[maxN], tot[maxN], lala[maxN], pos[maxN];
bool appear[maxN], vis[maxN];
vector<int> dp[maxN], invdp[maxN], chain;
void upd(int& a, int b) {
  a += b;
  if (a < 0) a += mo;
  if (a >= mo) a -= mo;
}
void prework() {
  for (int i = (1), _ = (N); i <= _; ++i) {
    dp[i].resize(N / i + 5);
    invdp[i].resize(N / i + 5);
    dp[i][0] = invdp[i][0] = 1;
    for (int j = (1), _ = (N / i + 2); j <= _; ++j) {
      dp[i][j] = (long long)dp[i][j - 1] * i % mo;
      if (j >= 2)
        upd(dp[i][j], (long long)dp[i][j - 2] * (j - 1) % mo * i % mo);
      invdp[i][j] = pow_mod(dp[i][j], mo - 2);
    }
  }
}
void calc(int haha) {
  int pre1 = (long long)pre * haha % mo;
  for (int i = (1), _ = (tp); i <= _; ++i) {
    pre1 = (long long)pre1 * invdp[len[i]][lala[len[i]]] % mo;
    cnt[min(len[i], 3)]++;
    pre1 = (long long)pre1 * dp[len[i]][++lala[len[i]]] % mo;
  }
  if (cnt[3] == 0) pre1 = haha;
  upd(ans, pre1);
  for (int i = (1), _ = (tp); i <= _; ++i) {
    lala[len[i]]--;
    cnt[min(len[i], 3)]--;
  }
}
void dfs(int x, int haha) {
  if (x >= (int)chain.size()) {
    calc(haha);
    return;
  }
  tot[++tp] = 1;
  len[tp] = chain[x];
  dfs(x + 1, haha);
  --tp;
  for (int i = (1), _ = (tp); i <= _; ++i) {
    tot[i]++;
    len[i] += chain[x];
    dfs(x + 1, (long long)haha * (tot[i] - 1) % mo);
    len[i] -= chain[x];
    tot[i]--;
  }
}
int main() {
  read(N);
  read(M);
  prework();
  for (int i = (1), _ = (N); i <= _; ++i) {
    read(a[i]);
    pos[a[i]] = i;
  }
  for (int i = (1), _ = (N); i <= _; ++i)
    if (a[i] == 0) {
      int len = 0;
      for (int x = i; x; x = pos[x]) vis[x] = 1, ++len;
      chain.push_back(len);
    }
  for (int i = (1), _ = (N); i <= _; ++i)
    if (vis[i] == 0) {
      int len = 0;
      for (int x = i; vis[x] == 0; x = pos[x]) vis[x] = 1, ++len;
      cnt[min(len, 3)]++;
      lala[len]++;
    }
  pre = 1;
  for (int i = (1), _ = (N); i <= _; ++i)
    if (lala[i]) pre = (long long)pre * dp[i][lala[i]] % mo;
  dfs(0, 1);
  cout << ans << endl;
  return 0;
}
