#include <bits/stdc++.h>
using namespace std;
long long int modpow(long long int a, long long int b) {
  if (b == 0) {
    return 1;
  }
  long long int ans = modpow(a, b / 2);
  ans = (ans * ans) % 1000000007;
  if (b % 2) {
    ans = (ans * a) % 1000000007;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  vector<int> S1(n, 0);
  vector<int> S2(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> S1[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> S2[i];
  }
  long long int ansa = 0;
  long long int ansb = 1;
  long long int currb = 1;
  for (int i = 0; i < n; i++) {
    if (S1[i] == 0 && S2[i] == 0) {
      long long int goodcase = ((m * (m - 1)) / 2) % 1000000007;
      long long int allcase = (((m * m) % 1000000007) * currb) % 1000000007;
      ansa = (ansa * allcase + ansb * goodcase) % 1000000007;
      ansb = (ansb * allcase) % 1000000007;
      currb = (currb * m) % 1000000007;
    } else if (S1[i] == 0) {
      long long int goodcase = m - S2[i];
      long long int allcase = (m * currb) % 1000000007;
      ansa = (ansa * allcase + ansb * goodcase) % 1000000007;
      ansb = (ansb * allcase) % 1000000007;
      currb = (currb * m) % 1000000007;
    } else if (S2[i] == 0) {
      long long int goodcase = S1[i] - 1;
      long long int allcase = (m * currb) % 1000000007;
      ansa = (ansa * allcase + ansb * goodcase) % 1000000007;
      ansb = (ansb * allcase) % 1000000007;
      currb = (currb * m) % 1000000007;
    } else {
      if (S2[i] > S1[i]) {
        break;
      } else if (S1[i] > S2[i]) {
        long long int goodcase = 1;
        long long int allcase = currb;
        ansa = (ansa * allcase + ansb * goodcase) % 1000000007;
        ansb = (ansb * allcase) % 1000000007;
        break;
      }
    }
  }
  cout << (ansa * modpow(ansb, 1000000007 - 2)) % 1000000007;
  return 0;
}
