#include <bits/stdc++.h>
using namespace std;
long long int inf = 1e14;
long long int mod = 998244353;
char en = '\n';
long long int abs1(long long int x) {
  if (x < 0) return -x;
  return x;
}
long long int power(long long int x, long long int n, long long int mod) {
  long long int res = 1;
  while (n) {
    if (n & 1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}
long long int fact[1000005], ifact[1000005];
long long int nCr(long long int n, long long int r) {
  return fact[n] * ifact[r] % mod * ifact[n - r] % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  fact[0] = ifact[0] = 1;
  for (long long int i = 1; i < 1000005; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
    ifact[i] = power(fact[i], mod - 2, mod);
  }
  long long int res = 0;
  for (long long int i = 1; i <= n; i++) {
    long long int sign = -1;
    if (i & 1) sign *= -1;
    long long int val = nCr(n, i);
    long long int val2 =
        3 * power((power(3, n - i, mod) - 1 + mod) % mod, n, mod);
    val2 %= mod;
    val2 += (power(3, i, mod) - 3) * (power(3, n * (n - i), mod)) % mod;
    val2 %= mod;
    val *= val2;
    val %= mod;
    res += sign * val;
    res = (res % mod + mod) % mod;
  }
  res = (res % mod + mod) % mod;
  cout << (power(3, n * n, mod) + res -
           power((power(3, n, mod) - 3 + mod) % mod, n, mod) % mod + mod) %
              mod
       << en;
  return 0;
}
