#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int mo = 1000000007;
inline int IN() {
  char ch;
  (ch = getchar());
  int f = 0, x = 0;
  for (; ((ch) == '\n' || (ch) == '\r' || (ch) == '\t' || (ch) == ' ');
       (ch = getchar()))
    ;
  if (ch == '-') f = 1, (ch = getchar());
  for (; !((ch) == '\n' || (ch) == '\r' || (ch) == '\t' || (ch) == ' ');
       (ch = getchar()))
    x = x * 10 + ch - '0';
  return (f) ? (-x) : (x);
}
int Pow(int x, int y, int p) {
  int A = 1;
  for (; y; y >>= 1, x = (long long)x * x % p)
    if (y & 1) A = (long long)A * x % p;
  return A;
}
vector<int> f[N], g[N];
int n;
int A, B, tot, c[N], len[N], num[N], ans[N], res, cnt, vis[N], lanlanlu[N];
int a[N];
void upd(int &x, int y) {
  if ((x += y) >= mo) x -= mo;
}
void mul(int x, int y) {
  if (!x)
    B += y;
  else
    A = 1ll * A * x % mo;
}
void dfs(int x, int an) {
  if (x > cnt) {
    int aa = A, bb = B;
    for (int i = (int)1; i <= (int)tot; i++) {
      int y = c[i];
      mul(g[y][ans[y]], -1);
      ans[y]++;
      mul(f[y][ans[y]], 1);
    }
    if (ans[1] + ans[2] * 2 == n) {
      ++res;
      res %= mo;
    } else {
      int tmp = (B) ? (0) : (A);
      res = (res + 1ll * tmp * an % mo) % mo;
    }
    for (int i = (int)1; i <= (int)tot; i++) ans[c[i]]--;
    A = aa;
    B = bb;
    return;
  }
  ++tot;
  c[tot] = len[x];
  num[tot] = 1;
  dfs(x + 1, an);
  --tot;
  for (int i = (int)1; i <= (int)tot; i++) {
    c[i] += len[x];
    num[i]++;
    dfs(x + 1, 1ll * an * (num[i] - 1) % mo);
    c[i] -= len[x];
    num[i]--;
  }
}
pair<int, int> dfs(int x) {
  vis[x] = 1;
  if (!a[x]) return make_pair(1, 0);
  if (vis[a[x]]) return make_pair(1, x);
  pair<int, int> tmp = dfs(a[x]);
  ++tmp.first;
  return tmp;
}
int main() {
  scanf("%d%*d", &n);
  for (int i = (int)1; i <= (int)n; i++) scanf("%d", a + i), lanlanlu[a[i]] = 1;
  f[0].push_back(1);
  g[0].push_back(1);
  for (int i = (int)1; i <= (int)n; i++) {
    f[i].push_back(1);
    g[i].push_back(1);
    for (int j = (int)1; j <= (int)n / i; j++) {
      f[i].push_back(0);
      g[i].push_back(0);
      if (j >= 1) upd(f[i][j], 1ll * f[i][j - 1] * i % mo);
      if (j >= 2) upd(f[i][j], 1ll * f[i][j - 2] * i % mo * (j - 1) % mo);
      g[i][j] = Pow(f[i][j], mo - 2, mo);
    }
  }
  for (int i = (int)1; i <= (int)n; i++) {
    if (a[i] == 0) continue;
    if (vis[i]) continue;
    if (lanlanlu[i]) continue;
    pair<int, int> tmp = dfs(i);
    len[++cnt] = tmp.first;
  }
  for (int i = (int)1; i <= (int)n; i++)
    if (!vis[i] && a[i] == 0) len[++cnt] = 1;
  for (int i = (int)1; i <= (int)n; i++)
    if (!vis[i] && a[i] != 0) {
      ans[dfs(i).first]++;
    }
  A = 1;
  B = 0;
  for (int i = (int)1; i <= (int)n; i++) mul(f[i][ans[i]], 1);
  dfs(1, 1);
  printf("%d\n", res);
  return 0;
}
