#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e5 + 4;
long long n, m, MOD = 1e9 + 7, nulls, dp[NMAX][2];
int a[NMAX], b[NMAX];
void get_data() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i], nulls += (a[i] == 0);
  for (int i = 1; i <= n; i++) cin >> b[i], nulls += (b[i] == 0);
}
long long f(int num, int fl) {
  if (fl) {
    if (a[num] == 0 && a[num] == b[num]) {
      return m * (m - 1) / 2;
    } else if (a[num] == 0) {
      return m - b[num];
    } else if (b[num] == 0) {
      return a[num] - 1;
    } else
      return a[num] > b[num];
  } else {
    if (a[num] == b[num] && a[num] == 0) return m;
    if (min(a[num], b[num]) == 0) return 1;
    return a[num] == b[num];
  }
}
long long binpow(int q, int t) {
  if (!t) return 1ll;
  if (t % 2)
    return (binpow(q, t - 1) * q) % MOD;
  else {
    long long w = (binpow(q, t / 2) % MOD);
    return (w * w) % MOD;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  get_data();
  long long P = 0;
  long long Q = binpow(m, nulls);
  long long Qm = binpow(Q, MOD - 2);
  dp[0][0] = dp[0][1] = 1;
  nulls -= (a[1] == 0) + (b[1] == 0);
  dp[1][0] = f(1, 0);
  dp[1][1] = f(1, 1);
  P = dp[1][1] * binpow(m, nulls);
  for (int i = 2; i <= n; i++) {
    nulls -= (a[i] == 0) + (b[i] == 0);
    dp[i][0] = (dp[i - 1][0] * f(i, 0)) % MOD;
    dp[i][1] = ((dp[i - 1][0] * f(i, 1)) % MOD * binpow(m, nulls)) % MOD;
    P = (P + dp[i][1]) % MOD;
  }
  cout << (P * Qm) % MOD;
  return 0;
}
