#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
long long mod = 1e9 + 7;
long long pp, qq, ans;
long long n, m;
long long s1[maxn];
long long s2[maxn];
long long gcd(long long a, long long b) {
  if (b) return gcd(b, a % b);
  return a;
}
long long qpow(long long aa, long long bb) {
  long long ans = 1;
  long long a = aa, b = bb;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1) ans = (ans * a) % mod;
  return ans % mod;
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%lld", &s1[i]);
  for (int i = 0; i < n; ++i) scanf("%lld", &s2[i]);
  long long k1 = 0, k2 = 0, cnt = 0;
  long long num = (1LL * m * m % mod - m + mod) % mod * qpow(2, mod - 2) % mod;
  for (int i = 0; i < n; ++i) {
    if (s1[i] == 0) k1++;
    if (s2[i] == 0) k2++;
  }
  long long t1 = k1, t2 = k2;
  for (int i = 0; i < n; ++i) {
    if (s1[i] == 0) {
      t1--;
      if (s2[i] == 0) {
        t2--;
        pp = (pp + num * qpow(m, cnt + t1 + t2) % mod) % mod;
        cnt++;
      } else {
        pp = (pp + (m - (s2[i])) * qpow(m, cnt + t1 + t2) % mod) % mod;
      }
    } else {
      if (s2[i] == 0) {
        t2--;
        pp = (pp + (s1[i] - 1) * qpow(m, cnt + t1 + t2) % mod) % mod;
      } else {
        if (s1[i] > s2[i]) {
          pp = (pp + qpow(m, cnt + t1 + t2)) % mod;
        }
        if (s1[i] != s2[i]) {
          qq = qpow(m, k1 + k2);
          ans = (pp * qpow(qq, mod - 2) % mod) % mod;
          printf("%lld\n", ans);
          return 0;
        }
      }
    }
  }
  qq = qpow(m, k1 + k2);
  long long g = gcd(pp, qq);
  pp /= g;
  qq /= g;
  ans = (pp * qpow(qq, mod - 2) % mod) % mod;
  printf("%lld\n", ans);
  return 0;
}
