#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int ex) {
  if (ex == 0) return 1;
  long long int p = power(a, ex / 2) % 1000000007;
  p = (p * p) % 1000000007;
  if (ex % 2) p = (p * a) % 1000000007;
  return p;
}
long long int modInverse(long long int a) {
  long long int ans = power(a, 1000000007 - 2);
  return ans;
}
int main() {
  long long int n, m, prev = 1, onebym, onebytwo, ans = 0, temp;
  cin >> n >> m;
  int a[n], b[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> b[i];
  onebym = modInverse(m);
  onebytwo = modInverse(2);
  for (int i = 0; i < n; i++) {
    if (a[i] == 0 && b[i] == 0) {
      temp = (prev * (m - 1)) % 1000000007;
      temp = (temp * onebym) % 1000000007;
      temp = (temp * onebytwo) % 1000000007;
      ans += temp;
      ans += 1000000007;
      ans = ans % 1000000007;
      prev = (prev * onebym) % 1000000007;
    } else if (a[i] == 0) {
      temp = (prev * (m - b[i])) % 1000000007;
      temp = (temp * onebym) % 1000000007;
      ans += temp;
      ans += 1000000007;
      ans = ans % 1000000007;
      prev = (prev * onebym) % 1000000007;
    } else if (b[i] == 0) {
      temp = (prev * (a[i] - 1)) % 1000000007;
      temp = (temp * onebym) % 1000000007;
      ans += temp;
      ans += 1000000007;
      ans = ans % 1000000007;
      prev = (prev * onebym) % 1000000007;
    } else if (a[i] > b[i]) {
      ans += prev;
      ans += 1000000007;
      ans = ans % 1000000007;
      break;
    } else if (a[i] < b[i])
      break;
  }
  cout << ans;
}
