Number of Relations that are both Irreflexive and Antisymmetric on a Set | Java program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count relations that are irreflexive and antisymmetric in a set consisting of N elements ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( long x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( int ) ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int numberOfRelations ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( numberOfRelations ( N ) ) ; } }