#include <bits/stdc++.h>
using namespace std;
int S1[100000 + 2];
int S2[100000 + 2];
long long modpow(long long x, long long y) {
  long long xs = x;
  long long answer = 1;
  while (y) {
    if (y & 1) {
      answer = (answer * xs) % 1000000007;
    }
    y >>= 1;
    xs = (xs * xs) % 1000000007;
  }
  return answer;
}
long long dp[100000 + 2];
long long Solve(int n, int m) {
  long long m_inv = modpow(m, 1000000007 - 2);
  long long two_inv = modpow(2, 1000000007 - 2);
  for (int pos = n; pos >= 0; pos--) {
    if (pos == n) {
      dp[pos] = 0;
      continue;
    }
    if (S1[pos] != 0 && S2[pos] != 0) {
      if (S1[pos] > S2[pos]) dp[pos] = 1;
      if (S1[pos] < S2[pos]) dp[pos] = 0;
      if (S1[pos] == S2[pos]) dp[pos] = dp[pos + 1];
    }
    if (S1[pos] != 0 && S2[pos] == 0) {
      long long first_term = m_inv * dp[pos + 1] % 1000000007;
      long long second_term = m_inv * (S1[pos] - 1) % 1000000007;
      dp[pos] = (first_term + second_term) % 1000000007;
    }
    if (S1[pos] == 0 && S2[pos] != 0) {
      long long first_term = m_inv * dp[pos + 1] % 1000000007;
      long long second_term = m_inv * (m - S2[pos]) % 1000000007;
      dp[pos] = (first_term + second_term) % 1000000007;
    }
    if (S1[pos] == 0 && S2[pos] == 0) {
      long long first_term = m_inv * dp[pos + 1] % 1000000007;
      long long second_term =
          m_inv * two_inv % 1000000007 * (m - 1) % 1000000007;
      dp[pos] = (first_term + second_term) % 1000000007;
    }
  }
  return dp[0];
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> S1[i];
  for (int i = 0; i < n; i++) cin >> S2[i];
  cout << Solve(n, m) << endl;
  return 0;
}
