#include <bits/stdc++.h>
using namespace std;
const int Mod = (1e9) + 7;
const int Maxn = 100005;
long long x, y;
int s1[20], s2[20];
int s[Maxn], jc[Maxn];
int pre[Maxn], nxt[Maxn];
int sum_chain, len_chain[20];
int sum_circle, len_circle[Maxn];
int n, k, i, j, cot1, cot2, base, ans, cnt;
bool flag, vis[Maxn];
vector<int> f[Maxn], g[Maxn];
int qck(int a, int b) {
  int ret = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1) ret = (long long)ret * a % Mod;
    a = (long long)a * a % Mod;
  }
  return ret;
}
void dfs(int x) {
  if (x > sum_chain) {
    int tmp = base;
    bool ff = flag;
    for (int i = 1; i <= cnt; i++) {
      int len = s2[i];
      tmp = (long long)tmp * g[len][s[len]] % Mod * f[len][s[len] + 1] % Mod;
      tmp = (long long)tmp * jc[s1[i] - 1] % Mod;
      s[len]++;
      if (s2[i] > 2) ff = 1;
    }
    if (ff == 1)
      ans += tmp;
    else
      ans++;
    if (ans > Mod) ans -= Mod;
    for (int i = 1; i <= cnt; i++) s[s2[i]]--;
    return;
  }
  for (int i = 1; i <= cnt; i++) {
    s1[i]++;
    s2[i] += len_chain[x];
    dfs(x + 1);
    s1[i]--;
    s2[i] -= len_chain[x];
  }
  cnt++;
  s1[cnt]++;
  s2[cnt] += len_chain[x];
  dfs(x + 1);
  s1[cnt]--;
  s2[cnt] -= len_chain[x];
  cnt--;
}
void exgcd(long long a, long long b) {
  if (b == 0) {
    x = 1;
    y = 0;
    return;
  }
  exgcd(b, a % b);
  long long tmp = x;
  x = y;
  y = tmp - (a / b) * y;
}
void init() {
  for (i = 1; i <= n; i++) {
    f[i].push_back(1);
    f[i].push_back(i);
    g[i].push_back(1);
    exgcd(i, Mod);
    x = x % Mod;
    if (x < 0) x += Mod;
    g[i].push_back(x);
    for (j = 2; i * j <= n; j++) {
      f[i].push_back(((long long)f[i][j - 1] * i % Mod +
                      (long long)(j - 1) * f[i][j - 2] % Mod * i % Mod) %
                     Mod);
      exgcd(f[i][j], Mod);
      x = x % Mod;
      if (x < 0) x += Mod;
      g[i].push_back(x);
    }
  }
  for (i = 1, jc[0] = 1; i <= n; i++) jc[i] = (long long)jc[i - 1] * i % Mod;
}
int main() {
  scanf("%d%d", &n, &k);
  init();
  for (i = 1; i <= n; i++) {
    scanf("%d", &nxt[i]);
    if (nxt[i]) pre[nxt[i]] = i;
  }
  for (i = 1; i <= n; i++)
    if (pre[i] == 0) {
      sum_chain++;
      for (j = i; j > 0; j = nxt[j]) {
        len_chain[sum_chain]++;
        vis[j] = 1;
      }
    }
  base = 1;
  for (i = 1; i <= n; i++)
    if (vis[i] == 0) {
      sum_circle++;
      for (j = i; vis[j] == 0; j = nxt[j]) {
        len_circle[sum_circle]++;
        vis[j] = 1;
      }
      int tmp = len_circle[sum_circle];
      base = (long long)base * qck(f[tmp][s[tmp]], Mod - 2) % Mod *
             f[tmp][s[tmp] + 1] % Mod;
      s[tmp]++;
      if (tmp > 2) flag = 1;
    }
  dfs(1);
  printf("%d\n", ans);
  return 0;
}
