Valid Pairings

You are given an integer N and an N * N binary matrix comp. The matrix represents the compatibility between N elements of Set A and N elements of Set B. Specifically, comp[i][j] = 1 indicates that the ith element of Set A is compatible with the jth element of Set B, and 0 indicates they are not compatible.

Find the number of valid ways to form exactly N mutually exclusive pairs between Set A and Set B such that every pair is compatible.

Since the answer can be large, return it modulo 109 + 7.

Example 1:

Input: N = 3 , comp = [[0, 1, 1], [1, 0, 1], [1, 1, 1]]

Output: 3

Explanation : Valid matchings:

(1,2), (2,1), (3,3)
(1,2), (2,3), (3,1)
(1,3), (2,1), (3,2)

Example 2:

Input: N = 4 , comp = [[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 0]]

Output: 1

Now Your Turn!

Pick the correct output for the given input

Input: N = 4 , comps = [[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 1], [0, 0, 1, 1]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ N ≤ 21
  • a[i][j] ∈ {0,1}

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int validPairings(vector<vector<int>>& comp) {
// Write your code here
return 0;
}
};
 
Test Case

Input:

Comp