#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
void fastIO() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long fact[1000007];
long long invFact[1000007];
long long n;
long long powM(long long base, long long pw) {
  long long res = 1;
  base %= 998244353;
  while (pw) {
    res = pw & 1 ? (res * base) % 998244353 : res;
    base = (base * base) % 998244353;
    pw >>= 1;
  }
  return res;
}
long long nck(long long n, long long k) {
  return ((fact[n] * invFact[k]) % 998244353 * invFact[n - k]) % 998244353;
}
void fillFactorials() {
  fact[0] = 1;
  invFact[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fact[i] = fact[i - 1] * i % 998244353;
    invFact[i] = powM(fact[i], 998244353 - 2);
  }
}
int main() {
  fastIO();
  cin >> n;
  fillFactorials();
  long long res = powM(powM(3, n) - 3, n);
  long long sign = 1;
  long long sameColor, differentColor;
  for (int i = 1; i <= n; i++) {
    sign = (i & 1 ? -1 : +1);
    sameColor =
        (3ll * powM(powM(3, n - i) - 1, n) % 998244353 * nck(n, i)) % 998244353;
    differentColor =
        (((powM(3, i) - 3) * nck(n, i)) % 998244353 * powM(powM(3, n - i), n)) %
        998244353;
    res = (res + (sign * (sameColor + differentColor)) % 998244353) % 998244353;
  }
  res = powM(3, n * n) - res;
  res = (res % 998244353 + 998244353) % 998244353;
  cout << res << "\n";
  return 0;
}
