#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int inf = 0x3f3f3f3f;
const int neginf = 0xc0c0c0c0;
const int mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
  }
  return res;
}
long long invmod(long long a) { return powmod(a, mod - 2) % mod; }
long long doit(long long a, long long b) { return (a * invmod(b)) % mod; }
long long n, m;
long long prob = 1, ans = 0;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  vector<long long> a(n), b(n);
  for (int(i) = (0); (i) < (n); ++(i)) cin >> a[i];
  for (int(i) = (0); (i) < (n); ++(i)) cin >> b[i];
  long long t1 = invmod(m);
  long long t2 = doit(m, (m * m) % mod);
  long long invM = invmod(m);
  for (int(i) = (0); (i) < (n); ++(i)) {
    if (!a[i] && !b[i]) {
      ans += (prob * doit((m * (m - 1) / 2) % mod, m * m)) % mod;
      prob = (prob * t2) % mod;
    } else if (!a[i] && b[i]) {
      ans += ((prob * (m - b[i]) % mod) * invM) % mod;
      prob = (prob * t1) % mod;
    } else if (!b[i] && a[i]) {
      ans += ((prob * (a[i] - 1) % mod) * invM) % mod;
      prob = (prob * t1) % mod;
    }
    if (a[i] && b[i]) {
      if (a[i] > b[i]) {
        ans += prob;
        break;
      } else if (a[i] < b[i])
        break;
    }
    prob %= mod;
    ans %= mod;
  }
  cout << (ans % mod) << endl;
  return 0;
}
