#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long mod = 1000000007LL) {
  long long res = 1LL;
  long long base = a;
  while (b > 0) {
    if (b & 1) res = (res * base) % mod;
    base = (base * base) % mod;
    b >>= 1;
  }
  return res;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int arr1[n], arr2[n];
  int zeros = 0;
  for (int i = 0; i < (n); i++) scanf("%d", &arr1[i]), zeros += arr1[i] == 0;
  for (int i = 0; i < (n); i++) scanf("%d", &arr2[i]), zeros += arr2[i] == 0;
  long long num = 0LL, mod = 1000000007LL, M = (1LL * m * (m - 1) / 2) % mod,
            prev = 1;
  long long den = power(m, zeros * (mod - 2));
  for (int i = 0; i < (n); i++) {
    if (arr1[i] == 0 && arr2[i] == 0) {
      zeros -= 2;
      num = (num + (((prev * M) % mod) * power(m, zeros)) % mod) % mod;
      prev = (prev * m) % mod;
    } else if (arr1[i] == 0) {
      zeros--;
      num = (num + (((prev * (m - arr2[i])) % mod) * power(m, zeros)) % mod) %
            mod;
    } else if (arr2[i] == 0) {
      zeros--;
      num = (num + (((prev * (arr1[i] - 1)) % mod) * power(m, zeros)) % mod) %
            mod;
    } else if (arr1[i] > arr2[i]) {
      num = (num + prev * power(m, zeros)) % mod;
      break;
    } else if (arr1[i] < arr2[i]) {
      break;
    }
  }
  cout << (num * den) % mod << endl;
  return 0;
}
