#include <bits/stdc++.h>
using namespace std;
long long gcdExtended(long long a, long long b, long long *x, long long *y);
long long modInverse(long long a, long long m) {
  long long x, y;
  long long g = gcdExtended(a, m, &x, &y);
  if (g != 1)
    ;
  else {
    long long res = (x % m + m) % m;
    return res;
  }
}
long long gcdExtended(long long a, long long b, long long *x, long long *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
int main() {
  iostream::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  vector<long long> s1(n), s2(n);
  for (long long i = (0); i < (n); ++i) cin >> s1[i];
  for (long long i = (0); i < (n); ++i) cin >> s2[i];
  long long num = 1;
  long long ans;
  vector<long long> den;
  long long denm = 1;
  long long cnt1 = 0;
  bool flag = 0;
  long long cur = 0;
  long long finans = 0;
  for (long long i = (0); i < (n); ++i) {
    if (s1[i] != 0 && s2[i] > s1[i]) {
      break;
    }
    if (s2[i] != 0 && s1[i] > s2[i]) {
      finans += modInverse(denm, 1000000007);
      break;
    }
    if (s1[i] == 0 && s2[i] != 0) {
      denm *= m;
      finans += ((m - s2[i]) * (modInverse(denm % 1000000007, 1000000007)) %
                 1000000007) %
                1000000007;
      cnt1++;
      denm %= 1000000007;
    } else if (s2[i] == 0 && s1[i] != 0) {
      denm *= m;
      cur++;
      finans += ((s1[i] - 1) * (modInverse(denm % 1000000007, 1000000007)) %
                 1000000007) %
                1000000007;
      denm %= 1000000007;
    } else if (s1[i] == 0 && s2[i] == 0) {
      denm *= m;
      finans += ((m - 1) * (modInverse(2 * denm, 1000000007)) % 1000000007) %
                1000000007;
      denm = denm % 1000000007;
    }
    finans %= 1000000007;
  }
  cout << finans % 1000000007;
}
