#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long n, long long M) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * a) % M;
    a = (a * a) % M;
    n >>= 1;
  }
  return res;
}
long long rev(long long a, long long M) { return binpow(a, M - 2, M); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> s1(n), s2(n);
  for (int i = 0; i < n; i++) {
    cin >> s1[i];
  }
  long long p = 0;
  long long q = 1;
  long long M = 1e9 + 7;
  long long k = 1;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    if (temp == 0) {
      if (s1[i] == 0) {
        p = ((p * 2 * m) % M * k + q * (m - 1)) % M;
        q = (((q * 2 * m) % M) * k) % M;
        k = (k * m) % M;
      } else {
        p = (((p * m) % M) * k + q * (s1[i] - 1)) % M;
        q = (((q * m) % M) * k) % M;
        k = (k * m) % M;
      }
    } else {
      if (s1[i] == 0) {
        p = (((p * m) % M) * k + q * (m - temp)) % M;
        q = (((q * m) % M) * k) % M;
        k = (k * m) % M;
      } else {
        if (temp > s1[i]) {
          break;
        }
        if (temp == s1[i]) {
          continue;
        }
        p = (p * k + q) % M;
        q = (q * k) % M;
        break;
      }
    }
  }
  cout << (p * rev(q, M)) % M << endl;
  return 0;
}
