#include <bits/stdc++.h>
using namespace std;
long long expo(long long base, long long exponent, long long mod) {
  long long ans = 1;
  while (exponent != 0) {
    if ((exponent & 1) == 1) {
      ans = ans * base;
      ans = ans % mod;
    }
    base = base * base;
    base %= mod;
    exponent >>= 1;
  }
  return ans % mod;
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long modInv(long long a, long long m) {
  long long gc = gcd(a, m);
  a /= gc;
  m /= gc;
  long long m0 = m, y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    long long q = a / m, t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    unsigned long long int n, m, z = 0, num = 0, den, temp = 1;
    cin >> n >> m;
    vector<long long> p(n), q(n);
    for (long long int i = 0; i < n; i++) {
      cin >> p[i];
      if (p[i] == 0) z++;
    }
    for (long long int i = 0; i < n; i++) {
      cin >> q[i];
      if (q[i] == 0) z++;
    }
    den = expo(m, z, 1000000007);
    for (long long int i = 0; i < n; i++) {
      if (!p[i] && q[i]) {
        z--;
        num += ((((m - q[i]) * expo(m, z, 1000000007)) % 1000000007) * temp) %
               1000000007;
        num = num % 1000000007;
      } else if (p[i] && !q[i]) {
        z--;
        num += ((((p[i] - 1) * expo(m, z, 1000000007)) % 1000000007) * temp) %
               1000000007;
        num = num % 1000000007;
      } else if (!p[i] && !q[i]) {
        z -= 2;
        unsigned long long int s = m * (m - 1);
        s /= 2;
        num +=
            (((s * expo(m, z, 1000000007)) % 1000000007) * temp) % 1000000007;
        num = num % 1000000007;
        temp *= m;
        temp = temp % 1000000007;
      } else {
        if (p[i] < q[i])
          break;
        else if (p[i] > q[i]) {
          num += expo(m, z, 1000000007) * temp;
          num = num % 1000000007;
          break;
        }
      }
    }
    unsigned long long int ans = (num * modInv(den, 1000000007)) % 1000000007;
    cout << ans;
  }
  return 0;
}
